-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sendynl/add-webhooks-resource
Add the webhook resource
- Loading branch information
Showing
4 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |