diff --git a/tests/Api/TestCase.php b/tests/Api/TestCase.php index d1eb9dd..83a8e02 100644 --- a/tests/Api/TestCase.php +++ b/tests/Api/TestCase.php @@ -11,6 +11,7 @@ namespace Mailgun\Tests\Api; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Mailgun\Api\Webhook; use Mailgun\Hydrator\ModelHydrator; use Mailgun\Mailgun; use Psr\Http\Message\ResponseInterface; @@ -100,7 +101,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase * This will return you a real API instance with mocked dependencies. * This will make use of the "setHydratedResponse" and "setRequestMethod" etc.. */ - protected function getApiInstance() + protected function getApiInstance($apiKey = null) { $httpClient = $this->getMockBuilder('Http\Client\HttpClient') ->setMethods(['sendRequest']) @@ -144,6 +145,10 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase $class = $this->getApiClass(); + if ($apiKey !== null) { + return new $class($httpClient, $requestClient, $hydrator, $apiKey); + } + return new $class($httpClient, $requestClient, $hydrator); } diff --git a/tests/Api/WebhookTest.php b/tests/Api/WebhookTest.php new file mode 100644 index 0000000..71d2c2c --- /dev/null +++ b/tests/Api/WebhookTest.php @@ -0,0 +1,112 @@ +getApiInstance('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); + + $timestamp = '1403645220'; + $token = '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6'; + $signature = '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33'; + + $this->assertTrue($api->verifyWebhookSignature($timestamp, $token, $signature)); + } + + public function testVerifyWebhookBad() + { + $api = $this->getApiInstance('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); + $timestamp = '1403645220'; + $token = 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr'; + $signature = '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33'; + + $this->assertFalse($api->verifyWebhookSignature($timestamp, $token, $signature)); + } + + public function testVerifyWebhookEmptyRequest() + { + $api = $this->getApiInstance('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); + + $this->assertFalse($api->verifyWebhookSignature('', '', '')); + } + + public function testIndex() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v3/domains/example.com/webhooks'); + $this->setHydrateClass(IndexResponse::class); + + $api = $this->getApiInstance('key'); + $api->index('example.com'); + } + + public function testShow() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v3/domains/example.com/webhooks/hook_1'); + $this->setHydrateClass(ShowResponse::class); + + $api = $this->getApiInstance('key'); + $api->show('example.com', 'hook_1'); + } + + public function testCreate() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v3/domains/example.com/webhooks'); + $this->setHydrateClass(CreateResponse::class); + $this->setRequestBody([ + 'id' => '4711', + 'url'=>'url', + ]); + + $api = $this->getApiInstance('key'); + $api->create('example.com', '4711', 'url'); + } + + public function testUpdate() + { + $this->setRequestMethod('PUT'); + $this->setRequestUri('/v3/domains/example.com/webhooks/4711'); + $this->setHydrateClass(UpdateResponse::class); + $this->setRequestBody([ + 'url'=>'url', + ]); + + $api = $this->getApiInstance('key'); + $api->update('example.com', '4711', 'url'); + } + + + public function testDelete() + { + $this->setRequestMethod('DELETE'); + $this->setRequestUri('/v3/domains/example.com/webhooks/4711'); + $this->setHydrateClass(DeleteResponse::class); + + $api = $this->getApiInstance('key'); + $api->delete('example.com', '4711'); + } +}