We need both getApiMock and getApiInstance

This commit is contained in:
Nyholm 2018-08-05 10:34:38 +02:00 committed by David Garcia
parent 75bab3016c
commit afc8671eac
2 changed files with 45 additions and 14 deletions

View File

@ -72,7 +72,7 @@ class EventTest extends TestCase
JSON JSON
)); ));
$api = $this->getApiMock(); $api = $this->getApiInstance();
$event = $api->get('example.com'); $event = $api->get('example.com');
$this->assertInstanceOf(EventResponse::class, $event); $this->assertInstanceOf(EventResponse::class, $event);
$this->assertCount(1, $event->getItems()); $this->assertCount(1, $event->getItems());

View File

@ -66,6 +66,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
abstract protected function getApiClass(); abstract protected function getApiClass();
/**
* This will give you a mocked API. Optionally you can provide mocked dependencies.
*/
protected function getApiMock($httpClient = null, $requestClient = null, $hydrator = null) protected function getApiMock($httpClient = null, $requestClient = null, $hydrator = null)
{ {
if (null === $httpClient) { if (null === $httpClient) {
@ -73,11 +76,39 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
->setMethods(['sendRequest']) ->setMethods(['sendRequest'])
->getMock(); ->getMock();
$httpClient $httpClient
->method('sendRequest') ->expects($this->any())
->willReturn(null === $this->httpResponse ? new Response() : $this->httpResponse); ->method('sendRequest');
}
if (null === $requestClient) {
$requestClient = $this->getMockBuilder('Mailgun\RequestBuilder')
->setMethods(['create'])
->getMock();
}
if (null === $hydrator) {
$hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator')
->setMethods(['hydrate'])
->getMock();
}
return $this->getMockBuilder($this->getApiClass())
->setMethods(['httpGet', 'httpPost', 'httpPostRaw', 'httpDelete', 'httpPut'])
->setConstructorArgs([$httpClient, $requestClient, $hydrator])
->getMock();
} }
if (null === $requestClient) { /**
* This will return you a real API instance with mocked dependencies.
* This will make use of the "setHydratedResponse" and "setRequestMethod" etc..
*/
protected function getApiInstance()
{
$httpClient = $this->getMockBuilder('Http\Client\HttpClient')
->setMethods(['sendRequest'])
->getMock();
$httpClient
->method('sendRequest')
->willReturn(null === $this->httpResponse ? new Response() : $this->httpResponse);
$requestClient = $this->getMockBuilder('Mailgun\RequestBuilder') $requestClient = $this->getMockBuilder('Mailgun\RequestBuilder')
->setMethods(['create']) ->setMethods(['create'])
->getMock(); ->getMock();
@ -89,10 +120,10 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
$this->callback([$this, 'validateRequestBody']) $this->callback([$this, 'validateRequestBody'])
) )
->willReturn(new Request('GET', '/')); ->willReturn(new Request('GET', '/'));
}
$hydrator = new ModelHydrator(); $hydrator = new ModelHydrator();
if (null === $hydrator && null === $this->httpResponse) { if (null === $this->httpResponse) {
$hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator') $hydrator = $this->getMockBuilder('Mailgun\Hydrator\Hydrator')
->setMethods(['hydrate']) ->setMethods(['hydrate'])
->getMock(); ->getMock();
@ -227,7 +258,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
return true; return true;
} }
return is_callable($property) ? ($property)($value) : $value === $property; return is_callable($property) ? call_user_func($property, $value) : $value === $property;
} }
/** /**