From 3d81db203eae257c587ccc0d364b87708222f762 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 5 Aug 2018 09:54:41 +0200 Subject: [PATCH] Add tests --- tests/Api/EventTest.php | 62 ++++++++++++++++++++++++++++++++++++++++- tests/Api/TestCase.php | 44 ++++++++++++++++++++++++----- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/tests/Api/EventTest.php b/tests/Api/EventTest.php index 6b89546..eeb66f2 100644 --- a/tests/Api/EventTest.php +++ b/tests/Api/EventTest.php @@ -2,7 +2,10 @@ namespace Mailgun\Tests\Api; +use GuzzleHttp\Psr7\Response; use Mailgun\Api\Event; +use Mailgun\Exception\InvalidArgumentException; +use Mailgun\Model\Event\EventResponse; class EventTest extends TestCase { @@ -15,8 +18,65 @@ class EventTest extends TestCase { $this->setRequestMethod('GET'); $this->setRequestUri('/v3/example.com/events'); + $this->setHttpResponse(new Response(200, ['Content-Type'=>'application/json'], <<", + "subject": "Hello" + }, + "attachments": [], + "recipients": [ + "foo@example.com", + "baz@example.com", + "bar@example.com" + ], + "size": 69 + }, + "recipient": "baz@example.com", + "method": "http" + } + ], + "paging": { + "next": + "https://api.mailgun.net/v3/samples.mailgun.org/events/W3siY...", + "previous": + "https://api.mailgun.net/v3/samples.mailgun.org/events/Lkawm..." + } +} +JSON +)); $api = $this->getApiMock(); - $api->get('example.com'); + $event = $api->get('example.com'); + $this->assertInstanceOf(EventResponse::class, $event); + $this->assertCount(1, $event->getItems()); + $this->assertEquals('accepted', $event->getItems()[0]->getEvent()); + } + + public function testGetWithEmptyDomain() + { + $api = $this->getApiMock(); + $this->expectException(InvalidArgumentException::class); + $api->get(''); + } } diff --git a/tests/Api/TestCase.php b/tests/Api/TestCase.php index 31edc9b..fb30d6b 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\Hydrator\ModelHydrator; use Mailgun\Mailgun; use Mailgun\Model\ApiResponse; use Psr\Http\Message\ResponseInterface; @@ -86,6 +87,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase ->willReturn(new Request('GET', '/')); } + $hydrator = new ModelHydrator(); if (null === $hydrator && null === $this->httpResponse) { $hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator') ->setMethods(['hydrate']) @@ -112,26 +114,24 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase public function validateRequestMethod($method) { - return $this->requestMethod === null || $method === $this->requestMethod; + return $this->veriyProperty($this->requestMethod, $method); } public function validateRequestUri($uri) { - return $this->requestUri === null || $uri === $this->requestUri; + return $this->veriyProperty($this->requestUri, $uri); } public function validateRequestHeaders($headers) { - if (null === $this->requestHeaders) { - return true; - } + return $this->veriyProperty($this->requestHeaders, $headers); - return $this->requestHeaders == $headers; } public function validateRequestBody($body) { - return $this->requestMethod === null || $body === $this->requestBody; + return $this->veriyProperty($this->requestBody, $body); + } protected function getMailgunClient() @@ -210,5 +210,35 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase $this->hydrateClass = $hydrateClass; } + /** + * @param mixed|callable $property Example $this->requestMethod + * @param mixed $value The actual value from the user. + * @return bool + */ + private function veriyProperty($property, $value) + { + if ($property === null) { + return true; + } + return is_callable($property) ? ($property)($value) : $value === $property; + } + + + /** + * Make sure expectException always exists, even on PHPUnit 4 + * @param string $exception + * @param string|null $message + */ + public function expectException($exception, $message = null) + { + if (method_exists($this, 'setExpectedException')) { + $this->setExpectedException($exception, $message); + } else { + parent::expectException($exception); + if (null !== $message) { + $this->expectExceptionMessage($message); + } + } + } }