mailgun-php/tests/Api/TestCase.php

215 lines
5.6 KiB
PHP
Raw Normal View History

<?php
2016-11-24 01:02:12 +03:00
/*
* Copyright (C) 2013 Mailgun
2016-11-24 01:02:12 +03:00
*
* This software may be modified and distributed under the terms
2016-12-06 21:12:52 +03:00
* of the MIT license. See the LICENSE file for details.
2016-11-24 01:02:12 +03:00
*/
namespace Mailgun\Tests\Api;
2018-08-05 01:45:03 +03:00
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mailgun\Mailgun;
2018-08-05 01:45:03 +03:00
use Mailgun\Model\ApiResponse;
use Psr\Http\Message\ResponseInterface;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
* @author Contributors of https://github.com/KnpLabs/php-github-api
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
2016-10-27 09:34:27 +03:00
/**
* Private Mailgun API key.
*
* @var string
*/
protected $apiPrivKey;
/**
* Public Mailgun API key.
*
* @var string
*/
protected $apiPubKey;
/**
* Domain used for API testing.
*
* @var string
*/
protected $testDomain;
2018-08-05 01:45:03 +03:00
private $requestMethod;
private $requestUri;
private $requestHeaders;
private $requestBody;
private $httpResponse;
private $hydratedResponse;
private $hydrateClass;
protected function setUp()
2016-10-27 09:34:27 +03:00
{
$this->apiPrivKey = getenv('MAILGUN_PRIV_KEY');
$this->apiPubKey = getenv('MAILGUN_PUB_KEY');
$this->testDomain = getenv('MAILGUN_DOMAIN');
2018-08-05 01:45:03 +03:00
$this->reset();
2016-10-27 09:34:27 +03:00
}
abstract protected function getApiClass();
protected function getApiMock($httpClient = null, $requestClient = null, $hydrator = null)
{
if (null === $httpClient) {
$httpClient = $this->getMockBuilder('Http\Client\HttpClient')
->setMethods(['sendRequest'])
->getMock();
$httpClient
2018-08-05 01:45:03 +03:00
->method('sendRequest')
->willReturn($this->httpResponse === null ? new Response() : $this->httpResponse);
}
if (null === $requestClient) {
$requestClient = $this->getMockBuilder('Mailgun\RequestBuilder')
->setMethods(['create'])
->getMock();
2018-08-05 01:45:03 +03:00
$requestClient->method('create')
->with(
$this->callback([$this, 'validateRequestMethod']),
$this->callback([$this, 'validateRequestUri']),
$this->callback([$this, 'validateRequestHeaders']),
$this->callback([$this, 'validateRequestBody'])
)
->willReturn(new Request('GET', '/'));
}
2018-08-05 01:45:03 +03:00
if (null === $hydrator && null === $this->httpResponse) {
$hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator')
->setMethods(['hydrate'])
->getMock();
2018-08-05 01:45:03 +03:00
$hydratorModelClass = $this->hydrateClass;
$hydrateMethod = $hydrator->method('hydrate')
->with(
$this->callback(function ($response) {
return $response instanceof ResponseInterface;
}),
$this->callback(function ($class) use ($hydratorModelClass) {
return $hydratorModelClass === null || $class === $hydratorModelClass;
}));
if (null !== $this->hydratedResponse) {
$hydrateMethod->willReturn($this->hydratedResponse);
}
}
$class = $this->getApiClass();
return new $class($httpClient, $requestClient, $hydrator);
}
public function validateRequestMethod($method)
{
return $this->requestMethod === null || $method === $this->requestMethod;
}
public function validateRequestUri($uri)
{
return $this->requestUri === null || $uri === $this->requestUri;
}
public function validateRequestHeaders($headers)
{
if (null === $this->requestHeaders) {
return true;
}
2018-08-05 01:45:03 +03:00
return $this->requestHeaders == $headers;
}
public function validateRequestBody($body)
{
return $this->requestMethod === null || $body === $this->requestBody;
}
2016-10-27 09:34:27 +03:00
protected function getMailgunClient()
{
return Mailgun::create($this->apiPrivKey);
2016-10-27 09:34:27 +03:00
}
2018-08-05 01:45:03 +03:00
protected function reset()
{
$this->httpResponse = null;
$this->hydratedResponse = null;
$this->requestMethod = null;
$this->requestUri = null;
$this->requestHeaders = null;
$this->requestBody = null;
$this->hydrateClass = null;
}
/**
* Set a response that you want to client to respond with.
*/
public function setHttpResponse(ResponseInterface $httpResponse)
{
$this->httpResponse = $httpResponse;
}
/**
* The data you want the hydrator to return.
*
* @param mixed $hydratedResponse
*/
public function setHydratedResponse($hydratedResponse)
{
$this->hydratedResponse = $hydratedResponse;
}
/**
* Set request http method
* @param string $httpMethod
*/
public function setRequestMethod($httpMethod)
{
$this->requestMethod = $httpMethod;
}
/**
* @param string $requestUri
*/
public function setRequestUri($requestUri)
{
$this->requestUri = $requestUri;
}
/**
* @param array $requestHeaders
*/
public function setRequestHeaders(array $requestHeaders)
{
$this->requestHeaders = $requestHeaders;
}
/**
* @param mixed $requestBody
*/
public function setRequestBody($requestBody)
{
$this->requestBody = $requestBody;
}
/**
* The class we should hydrate to
* @param string $hydrateClass
*/
public function setHydrateClass($hydrateClass)
{
$this->hydrateClass = $hydrateClass;
}
}