Add tests

This commit is contained in:
Nyholm 2018-08-05 09:54:41 +02:00 committed by David Garcia
parent bdcca2db0d
commit 3d81db203e
2 changed files with 98 additions and 8 deletions

View File

@ -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'], <<<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('');
}
}

View File

@ -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);
}
}
}
}