View source
<?php
namespace Smartling\Tests\Unit;
use GuzzleHttp\ClientInterface;
use PHPUnit_Framework_MockObject_MockObject;
use Psr\Http\Message\ResponseInterface;
use Smartling\AuthApi\AuthApiInterface;
use Smartling\BaseApiAbstract;
abstract class ApiTestAbstract extends \PHPUnit_Framework_TestCase {
const JSON_OBJECT_AS_ARRAY = 1;
protected $object;
protected $userIdentifier = 'SomeUserIdentifier';
protected $secretKey = 'SomeSecretKey';
protected $projectId = 'SomeProjectId';
protected $validResponse = '{"response":{"data":{"wordCount":1629,"stringCount":503,"overWritten":false},"code":"SUCCESS","messages":[]}}';
protected $responseWithException = '{"response":{"data":null,"code":"VALIDATION_ERROR","errors":[{"message":"Validation error text"}]}}';
protected $responseAsync = '{"response":{"data":{"message":"message", "url":"url"},"code":"ACCEPTED"}}';
protected $client;
protected $authProvider;
protected $streamPlaceholder = 'stream';
protected $responseMock;
protected $requestMock;
protected static $requestInterfaceMethods = [
'setUrl',
'getUrl',
'getResource',
'getQuery',
'setQuery',
'getMethod',
'setMethod',
'getScheme',
'setScheme',
'getPort',
'setPort',
'getHost',
'setHost',
'getPath',
'setPath',
'getConfig',
];
protected static $hasEmitterInterfaceMethods = [
'getEmitter',
];
protected static $messageInterfaceMethods = [
'__toString',
'getProtocolVersion',
'setBody',
'getBody',
'getHeaders',
'getHeader',
'getHeaderAsArray',
'hasHeader',
'removeHeader',
'addHeader',
'addHeaders',
'setHeader',
'setHeaders',
];
protected static $clientInterfaceMethods = [
'createRequest',
'get',
'head',
'delete',
'put',
'patch',
'post',
'options',
'send',
'request',
'getDefaultOption',
'setDefaultOption',
'getBaseUrl',
];
protected static $responseInterfaceMethods = [
'getStatusCode',
'setStatusCode',
'getReasonPhrase',
'setReasonPhrase',
'getEffectiveUrl',
'setEffectiveUrl',
'json',
'xml',
];
protected function invokeMethod($object, $methodName, array $parameters = []) {
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection
->getMethod($methodName);
$method
->setAccessible(true);
return $method
->invokeArgs($object, $parameters);
}
protected function readProperty($object, $propertyName) {
$reflection = new \ReflectionClass(get_class($object));
$property = $reflection
->getProperty($propertyName);
$property
->setAccessible(true);
return $property
->getValue($object);
}
protected function prepareHttpClientMock() {
$this->client = $this
->getMockBuilder('GuzzleHttp\\Client')
->setMethods(array_merge(self::$clientInterfaceMethods, self::$hasEmitterInterfaceMethods))
->disableOriginalConstructor()
->getMock();
}
protected function prepareAuthProviderMock() {
$this->authProvider = $this
->getMockBuilder('\\Smartling\\AuthApi\\AuthApiInterface')
->setMethods([
'getAccessToken',
'getTokenType',
'resetToken',
])
->setConstructorArgs([
$this->userIdentifier,
$this->secretKey,
$this->client,
])
->getMock();
$this->authProvider
->expects($this
->any())
->method('getAccessToken')
->willReturn('fakeToken');
$this->authProvider
->expects($this
->any())
->method('getTokenType')
->willReturn('Bearer');
$this->authProvider
->expects($this
->any())
->method('resetToken');
}
protected function prepareClientResponseMock($setDefaultResponse = true) {
$this->responseMock = $this
->getMockBuilder('GuzzleHttp\\Psr7\\Response')
->setMethods(array_merge(self::$responseInterfaceMethods, self::$messageInterfaceMethods))
->disableOriginalConstructor()
->getMock();
if (true === $setDefaultResponse) {
$this->responseMock
->expects(self::any())
->method('json')
->willReturn(json_decode($this->validResponse, self::JSON_OBJECT_AS_ARRAY));
$this->responseMock
->expects(self::any())
->method('getBody')
->willReturn($this->validResponse);
}
$this->responseMock
->expects($this
->any())
->method('getStatusCode')
->willReturn(200);
}
protected function setUp() {
$this
->prepareHttpClientMock();
$this
->prepareAuthProviderMock();
$this
->prepareClientResponseMock();
}
}