Skip to content

Commit 27a5f85

Browse files
authored
Merge pull request #6 from sendynl/add-webhooks-resource
Add the webhook resource
2 parents 2ffedc4 + 3de7591 commit 27a5f85

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
needs: [ install_dependencies ]
4040
strategy:
4141
matrix:
42-
php-versions: [ "7.4", "8.0", "8.1", "8.2", "8.3" ]
42+
php-versions: [ "7.4", "8.0", "8.1", "8.2", "8.3", "8.4" ]
4343

4444
runs-on: ubuntu-latest
4545

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"require-dev": {
3939
"phpunit/phpunit": "^9.0",
40-
"phpstan/phpstan": "^1.10",
40+
"phpstan/phpstan": "^1",
4141
"squizlabs/php_codesniffer": "^3.7",
4242
"mockery/mockery": "^1.5"
4343
},

src/Resources/Webhook.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Sendy\Api\Resources;
4+
5+
final class Webhook extends Resource
6+
{
7+
/**
8+
* List all webhooks
9+
*
10+
* @return array<string, mixed|array<string|mixed>>
11+
* @throws \GuzzleHttp\Exception\GuzzleException
12+
* @throws \Sendy\Api\ApiException
13+
* @see https://app.sendy.nl/api/docs#tag/Webhooks/operation/api.webhooks.index
14+
*/
15+
public function list(): array
16+
{
17+
return $this->connection->get('/webhooks');
18+
}
19+
20+
/**
21+
* Create a new webhook
22+
*
23+
* @param array<string, mixed|array<string|mixed>> $data
24+
* @return array<string, mixed|array<string|mixed>>
25+
* @throws \GuzzleHttp\Exception\GuzzleException
26+
* @throws \Sendy\Api\ApiException
27+
* @see https://app.sendy.nl/api/docs#tag/Webhooks/operation/api.webhooks.store
28+
*/
29+
public function create(array $data): array
30+
{
31+
return $this->connection->post('/webhooks', $data);
32+
}
33+
34+
/**
35+
* Delete a webhook
36+
*
37+
* @param string $id The ID of the webhook
38+
* @return array<empty>
39+
* @throws \GuzzleHttp\Exception\GuzzleException
40+
* @throws \Sendy\Api\ApiException
41+
*/
42+
public function delete(string $id): array
43+
{
44+
return $this->connection->delete("/webhooks/{$id}");
45+
}
46+
47+
/**
48+
* Update an existing webhook
49+
*
50+
* @param string $id The id of the webhook to be updated
51+
* @param array<string, mixed|array<string|mixed>> $data
52+
* @return array<string, mixed|array<string|mixed>>
53+
* @throws \GuzzleHttp\Exception\GuzzleException
54+
* @throws \Sendy\Api\ApiException
55+
*/
56+
public function update(string $id, array $data): array
57+
{
58+
return $this->connection->put("/webhooks/{$id}", $data);
59+
}
60+
}

tests/Resources/WebhookTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Sendy\Api\Tests\Resources;
4+
5+
use GuzzleHttp\Handler\MockHandler;
6+
use GuzzleHttp\Psr7\Response;
7+
use Sendy\Api\Resources\Webhook;
8+
use PHPUnit\Framework\TestCase;
9+
use Sendy\Api\Tests\TestsEndpoints;
10+
11+
class WebhookTest extends TestCase
12+
{
13+
use TestsEndpoints;
14+
15+
public function testList(): void
16+
{
17+
$handler = new MockHandler([
18+
new Response(200, [], json_encode([])),
19+
]);
20+
21+
$resource = new Webhook($this->buildConnectionWithMockHandler($handler));
22+
23+
$this->assertEquals([], $resource->list());
24+
25+
$this->assertEquals('/api/webhooks', (string)$handler->getLastRequest()->getUri());
26+
$this->assertEquals('GET', $handler->getLastRequest()->getMethod());
27+
}
28+
29+
public function testDelete(): void
30+
{
31+
$handler = new MockHandler([
32+
new Response(204),
33+
]);
34+
35+
$resource = new Webhook($this->buildConnectionWithMockHandler($handler));
36+
37+
$resource->delete('webhook-id');
38+
39+
$this->assertEquals('/api/webhooks/webhook-id', $handler->getLastRequest()->getUri());
40+
$this->assertEquals('DELETE', $handler->getLastRequest()->getMethod());
41+
}
42+
43+
public function testCreate(): void
44+
{
45+
$handler = new MockHandler([
46+
new Response(201, [], json_encode([
47+
'data' => [
48+
'id' => 'webhook-id',
49+
'url' => 'https://example.com/webhook',
50+
'events' => [
51+
'shipment.generated',
52+
]
53+
]
54+
])),
55+
]);
56+
57+
$resource = new Webhook($this->buildConnectionWithMockHandler($handler));
58+
59+
$resource->create([
60+
'url' => 'https://example.com/webhook',
61+
'events' => [
62+
'shipments.generated',
63+
],
64+
]);
65+
66+
$this->assertEquals('/api/webhooks', (string)$handler->getLastRequest()->getUri());
67+
$this->assertEquals('POST', $handler->getLastRequest()->getMethod());
68+
$this->assertEquals(
69+
'{"url":"https:\/\/example.com\/webhook","events":["shipments.generated"]}',
70+
$handler->getLastRequest()->getBody()->getContents()
71+
);
72+
}
73+
74+
public function testUpdate(): void
75+
{
76+
$handler = new MockHandler([
77+
new Response(201, [], json_encode([
78+
'data' => [
79+
'id' => 'webhook-id',
80+
'url' => 'https://example.com/updated-webhook',
81+
'events' => [
82+
'shipment.generated',
83+
]
84+
]
85+
])),
86+
]);
87+
88+
$resource = new Webhook($this->buildConnectionWithMockHandler($handler));
89+
90+
$resource->update('webhook-id', [
91+
'url' => 'https://example.com/updated-webhook',
92+
'events' => [
93+
'shipment.generated',
94+
],
95+
]);
96+
97+
$this->assertEquals('/api/webhooks/webhook-id', $handler->getLastRequest()->getUri());
98+
$this->assertEquals('PUT', $handler->getLastRequest()->getMethod());
99+
$this->assertEquals(
100+
'{"url":"https:\/\/example.com\/updated-webhook","events":["shipment.generated"]}',
101+
$handler->getLastRequest()->getBody()->getContents()
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)