diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f5c1d5d..b6ec473 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -39,7 +39,7 @@ jobs: needs: [ install_dependencies ] strategy: matrix: - php-versions: [ "7.4", "8.0", "8.1", "8.2", "8.3" ] + php-versions: [ "7.4", "8.0", "8.1", "8.2", "8.3", "8.4" ] runs-on: ubuntu-latest diff --git a/composer.json b/composer.json index 9ae9848..f353291 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.0", - "phpstan/phpstan": "^1.10", + "phpstan/phpstan": "^1", "squizlabs/php_codesniffer": "^3.7", "mockery/mockery": "^1.5" }, diff --git a/src/Resources/Webhook.php b/src/Resources/Webhook.php new file mode 100644 index 0000000..1de1dce --- /dev/null +++ b/src/Resources/Webhook.php @@ -0,0 +1,60 @@ +> + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Sendy\Api\ApiException + * @see https://app.sendy.nl/api/docs#tag/Webhooks/operation/api.webhooks.index + */ + public function list(): array + { + return $this->connection->get('/webhooks'); + } + + /** + * Create a new webhook + * + * @param array> $data + * @return array> + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Sendy\Api\ApiException + * @see https://app.sendy.nl/api/docs#tag/Webhooks/operation/api.webhooks.store + */ + public function create(array $data): array + { + return $this->connection->post('/webhooks', $data); + } + + /** + * Delete a webhook + * + * @param string $id The ID of the webhook + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Sendy\Api\ApiException + */ + public function delete(string $id): array + { + return $this->connection->delete("/webhooks/{$id}"); + } + + /** + * Update an existing webhook + * + * @param string $id The id of the webhook to be updated + * @param array> $data + * @return array> + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Sendy\Api\ApiException + */ + public function update(string $id, array $data): array + { + return $this->connection->put("/webhooks/{$id}", $data); + } +} diff --git a/tests/Resources/WebhookTest.php b/tests/Resources/WebhookTest.php new file mode 100644 index 0000000..0fc3e1e --- /dev/null +++ b/tests/Resources/WebhookTest.php @@ -0,0 +1,104 @@ +buildConnectionWithMockHandler($handler)); + + $this->assertEquals([], $resource->list()); + + $this->assertEquals('/api/webhooks', (string)$handler->getLastRequest()->getUri()); + $this->assertEquals('GET', $handler->getLastRequest()->getMethod()); + } + + public function testDelete(): void + { + $handler = new MockHandler([ + new Response(204), + ]); + + $resource = new Webhook($this->buildConnectionWithMockHandler($handler)); + + $resource->delete('webhook-id'); + + $this->assertEquals('/api/webhooks/webhook-id', $handler->getLastRequest()->getUri()); + $this->assertEquals('DELETE', $handler->getLastRequest()->getMethod()); + } + + public function testCreate(): void + { + $handler = new MockHandler([ + new Response(201, [], json_encode([ + 'data' => [ + 'id' => 'webhook-id', + 'url' => 'https://example.com/webhook', + 'events' => [ + 'shipment.generated', + ] + ] + ])), + ]); + + $resource = new Webhook($this->buildConnectionWithMockHandler($handler)); + + $resource->create([ + 'url' => 'https://example.com/webhook', + 'events' => [ + 'shipments.generated', + ], + ]); + + $this->assertEquals('/api/webhooks', (string)$handler->getLastRequest()->getUri()); + $this->assertEquals('POST', $handler->getLastRequest()->getMethod()); + $this->assertEquals( + '{"url":"https:\/\/example.com\/webhook","events":["shipments.generated"]}', + $handler->getLastRequest()->getBody()->getContents() + ); + } + + public function testUpdate(): void + { + $handler = new MockHandler([ + new Response(201, [], json_encode([ + 'data' => [ + 'id' => 'webhook-id', + 'url' => 'https://example.com/updated-webhook', + 'events' => [ + 'shipment.generated', + ] + ] + ])), + ]); + + $resource = new Webhook($this->buildConnectionWithMockHandler($handler)); + + $resource->update('webhook-id', [ + 'url' => 'https://example.com/updated-webhook', + 'events' => [ + 'shipment.generated', + ], + ]); + + $this->assertEquals('/api/webhooks/webhook-id', $handler->getLastRequest()->getUri()); + $this->assertEquals('PUT', $handler->getLastRequest()->getMethod()); + $this->assertEquals( + '{"url":"https:\/\/example.com\/updated-webhook","events":["shipment.generated"]}', + $handler->getLastRequest()->getBody()->getContents() + ); + } +}