Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored and DavidGarciaCat committed Aug 8, 2018
1 parent bdcca2d commit 3d81db2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
62 changes: 61 additions & 1 deletion tests/Api/EventTest.php
Expand Up @@ -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
{
Expand All @@ -15,8 +18,65 @@ public function testGet()
{
$this->setRequestMethod('GET');
$this->setRequestUri('/v3/example.com/events');
$this->setHttpResponse(new Response(200, ['Content-Type'=>'application/json'], <<<JSON
{
"items": [
{
"tags": [],
"id": "czsjqFATSlC3QtAK-C80nw",
"timestamp": 1376325780.160809,
"envelope": {
"sender": "me@samples.mailgun.org",
"transport": ""
},
"event": "accepted",
"campaigns": [],
"user-variables": {},
"flags": {
"is-authenticated": true,
"is-test-mode": false
},
"message": {
"headers": {
"to": "foo@example.com",
"message-id": "20130812164300.28108.52546@samples.mailgun.org",
"from": "Excited User <me@samples.mailgun.org>",
"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('');

}
}
44 changes: 37 additions & 7 deletions tests/Api/TestCase.php
Expand Up @@ -11,6 +11,7 @@

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mailgun\Hydrator\ModelHydrator;
use Mailgun\Mailgun;
use Mailgun\Model\ApiResponse;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -86,6 +87,7 @@ protected function getApiMock($httpClient = null, $requestClient = null, $hydrat
->willReturn(new Request('GET', '/'));
}

$hydrator = new ModelHydrator();
if (null === $hydrator && null === $this->httpResponse) {
$hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator')
->setMethods(['hydrate'])
Expand All @@ -112,26 +114,24 @@ protected function getApiMock($httpClient = null, $requestClient = null, $hydrat

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()
Expand Down Expand Up @@ -210,5 +210,35 @@ public function setHydrateClass($hydrateClass)
$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);
}
}
}
}

0 comments on commit 3d81db2

Please sign in to comment.