From 42503f72ce508beaf7d11803b45474ab772de2c8 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 5 Aug 2018 11:00:43 +0200 Subject: [PATCH] Added test for Domain API --- tests/Api/DomainTest.php | 185 +++++++++++++++++++++++++++++++++++++++ tests/Api/TestCase.php | 32 +++++-- 2 files changed, 210 insertions(+), 7 deletions(-) create mode 100644 tests/Api/DomainTest.php diff --git a/tests/Api/DomainTest.php b/tests/Api/DomainTest.php new file mode 100644 index 0000000..bc2198e --- /dev/null +++ b/tests/Api/DomainTest.php @@ -0,0 +1,185 @@ +setRequestMethod('GET'); + $this->setRequestUri('/v3/domains?limit=100&skip=0'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "total_count": 1, + "items": [ + { + "created_at": "Wed, 10 Jul 2013 19:26:52 GMT", + "smtp_login": "postmaster@samples.mailgun.org", + "name": "samples.mailgun.org", + "smtp_password": "4rtqo4p6rrx9", + "wildcard": true, + "spam_action": "disabled", + "state": "active" + } + ] +} +JSON +)); + + $api = $this->getApiInstance(); + /** @var IndexResponse $response */ + $response = $api->index(); + $this->assertInstanceOf(IndexResponse::class, $response); + $this->assertEquals(1, $response->getTotalCount()); + $this->assertEquals('samples.mailgun.org', $response->getDomains()[0]->getName()); + } + + public function testShow() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v3/domains/example.com'); + $this->setHydrateClass(ShowResponse::class); + + $api = $this->getApiInstance(); + $api->show('example.com'); + } + + public function testCreate() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v3/domains'); + $this->setRequestBody([ + 'name'=>'example.com' + ]); + $this->setHydrateClass(CreateResponse::class); + + $api = $this->getApiInstance(); + $api->create('example.com'); + } + + public function testCreateWithPassword() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v3/domains'); + $this->setRequestBody([ + 'name'=>'example.com', + 'smtp_password'=>'foo', + 'spam_action'=>'bar', + ]); + $this->setHydrateClass(CreateResponse::class); + + $api = $this->getApiInstance(); + $api->create('example.com', 'foo', 'bar', true); + } + + public function testDelete() + { + $this->setRequestMethod('DELETE'); + $this->setRequestUri('/v3/domains/example.com'); + $this->setHydrateClass(DeleteResponse::class); + + $api = $this->getApiInstance(); + $api->delete('example.com'); + } + + public function testCreateCredential() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v3/domains/example.com/credentials'); + $this->setRequestBody([ + 'login' => 'foo', + 'password' => 'barbar', + ]); + $this->setHydrateClass(CreateCredentialResponse::class); + + $api = $this->getApiInstance(); + $api->createCredential('example.com', 'foo', 'barbar'); + } + + public function testUpdateCredential() + { + $this->setRequestMethod('PUT'); + $this->setRequestUri('/v3/domains/example.com/credentials/foo'); + $this->setRequestBody([ + 'password' => 'barbar', + ]); + $this->setHydrateClass(UpdateCredentialResponse::class); + + $api = $this->getApiInstance(); + $api->updateCredential('example.com', 'foo', 'barbar'); + } + public function testDeleteCredential() + { + $this->setRequestMethod('DELETE'); + $this->setRequestUri('/v3/domains/example.com/credentials/foo'); + $this->setHydrateClass(DeleteCredentialResponse::class); + + $api = $this->getApiInstance(); + $api->deleteCredential('example.com', 'foo', 'barbar'); + } + + public function testConnection() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v3/domains/example.com/connection'); + $this->setHydrateClass(ConnectionResponse::class); + + $api = $this->getApiInstance(); + $api->connection('example.com'); + } + + public function testUpdateConnection() + { + $this->setRequestMethod('PUT'); + $this->setRequestUri('/v3/domains/example.com/connection'); + $this->setRequestBody([ + 'require_tls' => 'true', + 'skip_verification' => 'false', + ]); + $this->setHydrateClass(UpdateConnectionResponse::class); + + $api = $this->getApiInstance(); + $api->updateConnection('example.com', true , false); + } + + + public function testVerify() + { + $this->setRequestMethod('PUT'); + $this->setRequestUri('/v3/domains/example.com/verify'); + $this->setHydrateClass(VerifyResponse::class); + + $api = $this->getApiInstance(); + $api->verify('example.com'); + } + + +} diff --git a/tests/Api/TestCase.php b/tests/Api/TestCase.php index bee480c..5796d7a 100644 --- a/tests/Api/TestCase.php +++ b/tests/Api/TestCase.php @@ -89,6 +89,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase ->setMethods(['hydrate']) ->getMock(); } + return $this->getMockBuilder($this->getApiClass()) ->setMethods(['httpGet', 'httpPost', 'httpPostRaw', 'httpDelete', 'httpPut']) ->setConstructorArgs([$httpClient, $requestClient, $hydrator]) @@ -101,7 +102,6 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase */ protected function getApiInstance() { - $httpClient = $this->getMockBuilder('Http\Client\HttpClient') ->setMethods(['sendRequest']) ->getMock(); @@ -121,7 +121,6 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase ) ->willReturn(new Request('GET', '/')); - $hydrator = new ModelHydrator(); if (null === $this->httpResponse) { $hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator') @@ -150,22 +149,41 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase public function validateRequestMethod($method) { - return $this->veriyProperty($this->requestMethod, $method); + return $this->verifyProperty($this->requestMethod, $method); } public function validateRequestUri($uri) { - return $this->veriyProperty($this->requestUri, $uri); + return $this->verifyProperty($this->requestUri, $uri); } public function validateRequestHeaders($headers) { - return $this->veriyProperty($this->requestHeaders, $headers); + return $this->verifyProperty($this->requestHeaders, $headers); } public function validateRequestBody($body) { - return $this->veriyProperty($this->requestBody, $body); + if ($this->verifyProperty($this->requestBody, $body)) { + return true; + } + + // Assert: $body is prepared for a "multipart stream". + + // Check length + if (count($this->requestBody) !== count($body)) { + return false; + } + + // Check every item in body. + foreach ($body as $item) { + if ($this->requestBody[$item['name']] !== $item['content']) { + return false; + } + } + + return true; + } protected function getMailgunClient() @@ -252,7 +270,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase * * @return bool */ - private function veriyProperty($property, $value) + private function verifyProperty($property, $value) { if (null === $property) { return true;