|
| 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