Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the webhook resource #6

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
60 changes: 60 additions & 0 deletions src/Resources/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Sendy\Api\Resources;

final class Webhook extends Resource
{
/**
* List all webhooks
*
* @return array<string, mixed|array<string|mixed>>
* @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<string, mixed|array<string|mixed>> $data
* @return array<string, mixed|array<string|mixed>>
* @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<empty>
* @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<string, mixed|array<string|mixed>> $data
* @return array<string, mixed|array<string|mixed>>
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Sendy\Api\ApiException
*/
public function update(string $id, array $data): array
{
return $this->connection->put("/webhooks/{$id}", $data);
}
}
104 changes: 104 additions & 0 deletions tests/Resources/WebhookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Sendy\Api\Tests\Resources;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Sendy\Api\Resources\Webhook;
use PHPUnit\Framework\TestCase;
use Sendy\Api\Tests\TestsEndpoints;

class WebhookTest extends TestCase
{
use TestsEndpoints;

public function testList(): void
{
$handler = new MockHandler([
new Response(200, [], json_encode([])),
]);

$resource = new Webhook($this->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()
);
}
}
Loading