View source
<?php
namespace Smartling\Tests\Unit;
use Smartling\File\FileApi;
use Smartling\File\Params\DownloadFileParameters;
use Smartling\File\Params\UploadFileParameters;
class FileApiTest extends ApiTestAbstract {
private function prepareFileApiMock() {
$this->object = $this
->getMockBuilder('Smartling\\File\\FileApi')
->setMethods([
'readFile',
])
->setConstructorArgs([
$this->projectId,
$this->client,
null,
FileApi::ENDPOINT_URL,
])
->getMock();
$this->object
->expects($this
->any())
->method('readFile')
->willReturn($this->streamPlaceholder);
$this
->invokeMethod($this->object, 'setAuth', [
$this->authProvider,
]);
}
protected function setUp() {
parent::setUp();
$this
->prepareFileApiMock();
}
public function testConstructor($projectId, $client, $expected_base_url) {
$fileApi = new FileApi($projectId, $client, null, $expected_base_url);
$this
->assertEquals(rtrim($expected_base_url, '/') . '/' . $projectId, $this
->invokeMethod($fileApi, 'getBaseUrl'));
$this
->assertEquals($projectId, $this
->invokeMethod($fileApi, 'getProjectId'));
$this
->assertEquals($client, $this
->invokeMethod($fileApi, 'getHttpClient'));
}
public function constructorDataProvider() {
$this
->prepareHttpClientMock();
$mockedClient = $this->client;
return [
[
'product-id',
$mockedClient,
FileApi::ENDPOINT_URL,
],
[
'product-id',
$mockedClient,
FileApi::ENDPOINT_URL,
],
[
'product-id',
$mockedClient,
FileApi::ENDPOINT_URL . '/',
],
[
'product-id',
$mockedClient,
'https://www.google.com.ua/webhp',
],
];
}
public function testUploadFile() {
$this->client
->expects($this
->once())
->method('request')
->with('post', FileApi::ENDPOINT_URL . '/' . $this->projectId . '/file', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'multipart' => [
[
'name' => 'authorize',
'contents' => 0,
],
[
'name' => 'smartling.client_lib_id',
'contents' => '{"client":"smartling-api-sdk-php","version":"3.0.0"}',
],
[
'name' => 'localeIdsToAuthorize[]',
'contents' => 'es',
],
[
'name' => 'file',
'contents' => $this->streamPlaceholder,
],
[
'name' => 'fileUri',
'contents' => 'test.xml',
],
[
'name' => 'fileType',
'contents' => 'xml',
],
],
])
->willReturn($this->responseMock);
$params = new UploadFileParameters();
$params
->setAuthorized(true);
$params
->setLocalesToApprove('es');
$this->object
->uploadFile('tests/resources/test.xml', 'test.xml', 'xml', $params);
}
public function testFileUploadParams() {
$fileUploadParams = new UploadFileParameters();
$fileUploadParams
->setAuthorized(false);
$exportedSettings = $fileUploadParams
->exportToArray();
$this
->assertEquals($exportedSettings['authorize'], false);
$fileUploadParams
->setAuthorized(true);
$exportedSettings = $fileUploadParams
->exportToArray();
$this
->assertEquals($exportedSettings['authorize'], true);
$fileUploadParams
->setLocalesToApprove('locale');
$fileUploadParams
->setAuthorized(false);
$exportedSettings = $fileUploadParams
->exportToArray();
$this
->assertEquals($exportedSettings['authorize'], false);
$fileUploadParams
->setAuthorized(true);
$exportedSettings = $fileUploadParams
->exportToArray();
$this
->assertEquals($exportedSettings['authorize'], false);
}
public function testDownloadFile($options, $locale, $expected_translated_file) {
$this
->prepareClientResponseMock(false);
$this->responseMock
->expects($this
->any())
->method('getBody')
->willReturn($expected_translated_file);
$endpointUrl = vsprintf('%s/%s/locales/%s/file', [
FileApi::ENDPOINT_URL,
$this->projectId,
$locale,
]);
$params = $options instanceof DownloadFileParameters ? $options
->exportToArray() : [];
$params['fileUri'] = 'test.xml';
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => $params,
])
->willReturn($this->responseMock);
$actual_xml = $this->object
->downloadFile('test.xml', $locale, $options);
$this
->assertEquals($expected_translated_file, $actual_xml);
}
public function downloadFileParams() {
return [
[
(new DownloadFileParameters())
->setRetrievalType(DownloadFileParameters::RETRIEVAL_TYPE_PSEUDO),
'en-EN',
'<?xml version="1.0"?><response><item key="6"></item></response>',
],
[
null,
'en-EN',
'<?xml version="1.0"?><response><item key="6"></item></response>',
],
[
null,
'en',
'{"string1":"translation1", "string2":"translation2"}',
],
];
}
public function testDownloadFileLocaleCheckFails($options, $locale) {
$this->object
->downloadFile('test.xml', $locale, $options);
}
public function downloadFileLocaleCheckSuccessParams() {
return [
[
(new DownloadFileParameters())
->setRetrievalType(DownloadFileParameters::RETRIEVAL_TYPE_PSEUDO),
'e',
],
[
(new DownloadFileParameters())
->setRetrievalType(DownloadFileParameters::RETRIEVAL_TYPE_PSEUDO),
'',
],
[
(new DownloadFileParameters())
->setRetrievalType(DownloadFileParameters::RETRIEVAL_TYPE_PSEUDO),
[],
],
[
(new DownloadFileParameters())
->setRetrievalType(DownloadFileParameters::RETRIEVAL_TYPE_PSEUDO),
null,
],
[
(new DownloadFileParameters())
->setRetrievalType(DownloadFileParameters::RETRIEVAL_TYPE_PSEUDO),
(object) [
'foo',
],
],
];
}
public function testLastModified() {
$endpointUrl = vsprintf('%s/%s/file/last-modified', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [
'fileUri' => 'test.xml',
],
])
->willReturn($this->responseMock);
$this->object
->lastModified('test.xml');
}
public function testGetStatusForAllLocales() {
$endpointUrl = vsprintf('%s/%s/file/status', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [
'fileUri' => 'test.xml',
],
])
->willReturn($this->responseMock);
$this->object
->getStatusForAllLocales('test.xml');
}
public function testGetStatus() {
$endpointUrl = vsprintf('%s/%s/locales/%s/file/status', [
FileApi::ENDPOINT_URL,
$this->projectId,
'en-EN',
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [
'fileUri' => 'test.xml',
],
])
->willReturn($this->responseMock);
$this->object
->getStatus('test.xml', 'en-EN');
}
public function testGetList() {
$endpointUrl = vsprintf('%s/%s/files/list', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [],
])
->willReturn($this->responseMock);
$this->object
->getList();
}
public function testGetExtendedList() {
$locale = 'nl-NL';
$endpointUrl = vsprintf('%s/%s/locales/%s/files/list', [
FileApi::ENDPOINT_URL,
$this->projectId,
$locale,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [],
])
->willReturn($this->responseMock);
$this->object
->getExtendedList($locale);
}
public function testValidationErrorSendRequest() {
$this
->prepareClientResponseMock(false);
$this->responseMock
->expects($this
->any())
->method('getStatusCode')
->willReturn(400);
$this->responseMock
->expects($this
->any())
->method('getBody')
->willReturn($this->responseWithException);
$endpointUrl = vsprintf('%s/%s/context/html', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [],
])
->willReturn($this->responseMock);
$requestData = $this
->invokeMethod($this->object, 'getDefaultRequestData', [
'query',
[],
]);
$this
->invokeMethod($this->object, 'setBaseUrl', [
FileApi::ENDPOINT_URL . '/' . $this->projectId,
]);
$this
->invokeMethod($this->object, 'sendRequest', [
'context/html',
$requestData,
'get',
]);
}
public function testBadJsonFormatSendRequest() {
$this
->prepareClientResponseMock(false);
$this->responseMock
->expects($this
->any())
->method('getStatusCode')
->willReturn(400);
$this->responseMock
->expects($this
->any())
->method('getBody')
->willReturn(rtrim($this->responseWithException, '}'));
$endpointUrl = vsprintf('%s/%s/context/html', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [],
])
->willReturn($this->responseMock);
$requestData = $this
->invokeMethod($this->object, 'getDefaultRequestData', [
'query',
[],
]);
$this
->invokeMethod($this->object, 'setBaseUrl', [
FileApi::ENDPOINT_URL . '/' . $this->projectId,
]);
$this
->invokeMethod($this->object, 'sendRequest', [
'context/html',
$requestData,
'get',
]);
}
public function testBadJsonFormatInErrorMessageSendRequest() {
$this
->prepareClientResponseMock(false);
$this->responseMock
->expects($this
->any())
->method('getStatusCode')
->willReturn(401);
$this->responseMock
->expects($this
->any())
->method('getBody')
->willReturn(rtrim($this->responseWithException, '}'));
$endpointUrl = vsprintf('%s/%s/context/html', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [],
])
->willReturn($this->responseMock);
$requestData = $this
->invokeMethod($this->object, 'getDefaultRequestData', [
'query',
[],
]);
$this
->invokeMethod($this->object, 'setBaseUrl', [
FileApi::ENDPOINT_URL . '/' . $this->projectId,
]);
$this
->invokeMethod($this->object, 'sendRequest', [
'context/html',
$requestData,
'get',
]);
}
public function testSendRequest($uri, $requestData, $method, $params, $paramsType) {
$defaultRequestData = $this
->invokeMethod($this->object, 'getDefaultRequestData', [
$paramsType,
$requestData,
]);
$params['headers']['Authorization'] = vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]);
$this->client
->expects($this
->once())
->method('request')
->with($method, FileApi::ENDPOINT_URL . '/' . $this->projectId . '/' . $uri, $params)
->willReturn($this->responseMock);
$this
->invokeMethod($this->object, 'setBaseUrl', [
FileApi::ENDPOINT_URL . '/' . $this->projectId,
]);
$result = $this
->invokeMethod($this->object, 'sendRequest', [
$uri,
$defaultRequestData,
$method,
]);
self::assertEquals([
'wordCount' => 1629,
'stringCount' => 503,
'overWritten' => false,
], $result);
}
public function sendRequestValidProvider() {
return [
[
'uri',
[],
'get',
[
'headers' => [
'Accept' => 'application/json',
],
'exceptions' => false,
'query' => [],
],
'query',
],
[
'uri',
[
'key' => 'value',
'boolean_false' => false,
'boolean_true' => true,
'file' => './tests/resources/test.xml',
],
'post',
[
'headers' => [
'Accept' => 'application/json',
],
'exceptions' => false,
'multipart' => [
[
'name' => 'key',
'contents' => 'value',
],
[
'name' => 'boolean_false',
'contents' => '0',
],
[
'name' => 'boolean_true',
'contents' => '1',
],
[
'name' => 'file',
'contents' => $this->streamPlaceholder,
],
],
],
'multipart',
],
];
}
public function testRenameFile() {
$endpointUrl = vsprintf('%s/%s/file/rename', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('post', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'form_params' => [
'fileUri' => 'test.xml',
'newFileUri' => 'new_test.xml',
],
])
->willReturn($this->responseMock);
$this->object
->renameFile('test.xml', 'new_test.xml');
}
public function testGetAuthorizedLocales() {
$endpointUrl = vsprintf('%s/%s/file/authorized-locales', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('get', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'query' => [
'fileUri' => 'test.xml',
],
])
->willReturn($this->responseMock);
$this->object
->getAuthorizedLocales('test.xml');
}
public function testDeleteFile() {
$endpointUrl = vsprintf('%s/%s/file/delete', [
FileApi::ENDPOINT_URL,
$this->projectId,
]);
$this->client
->expects($this
->once())
->method('request')
->with('post', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'form_params' => [
'fileUri' => 'test.xml',
],
])
->willReturn($this->responseMock);
$this->object
->deleteFile('test.xml');
}
public function testImport() {
$locale = 'en-EN';
$endpointUrl = vsprintf('%s/%s/locales/%s/file/import', [
FileApi::ENDPOINT_URL,
$this->projectId,
$locale,
]);
$this->client
->expects($this
->once())
->method('request')
->with('post', $endpointUrl, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => vsprintf('%s %s', [
$this->authProvider
->getTokenType(),
$this->authProvider
->getAccessToken(),
]),
],
'exceptions' => false,
'multipart' => [
[
'name' => 'fileUri',
'contents' => 'test.xml',
],
[
'name' => 'fileType',
'contents' => 'xml',
],
[
'name' => 'file',
'contents' => $this->streamPlaceholder,
],
[
'name' => 'translationState',
'contents' => 'PUBLISHED',
],
[
'name' => 'overwrite',
'contents' => '0',
],
],
])
->willReturn($this->responseMock);
$this->object
->import($locale, 'test.xml', 'xml', 'tests/resources/test.xml', 'PUBLISHED', false);
}
public function testReadFile() {
$validFilePath = './tests/resources/test.xml';
$fileApi = $this
->getMockBuilder('Smartling\\File\\FileApi')
->setConstructorArgs([
$this->projectId,
$this->client,
])
->getMock();
$stream = $this
->invokeMethod($fileApi, 'readFile', [
$validFilePath,
]);
$this
->assertEquals('stream', get_resource_type($stream));
}
public function testFailedReadFile() {
$invalidFilePath = 'unexisted';
$fileApi = $this
->getMockBuilder('Smartling\\File\\FileApi')
->setConstructorArgs([
$this->projectId,
$this->client,
])
->getMock();
$stream = $this
->invokeMethod($fileApi, 'readFile', [
$invalidFilePath,
]);
$this
->assertEquals('stream', get_resource_type($stream));
}
public function testAcceptResponse() {
$responseMock = $this
->getMockBuilder('Guzzle\\Message\\ResponseInterface')
->setMethods(array_merge(self::$responseInterfaceMethods, self::$messageInterfaceMethods))
->disableOriginalConstructor()
->getMock();
$responseMock
->expects(self::any())
->method('getStatusCode')
->willReturn(202);
$responseMock
->expects(self::any())
->method('getBody')
->willReturn($this->responseAsync);
$responseMock
->expects(self::any())
->method('json')
->willReturn(json_decode($this->responseAsync, true));
$this->client
->expects(self::once())
->method('request')
->willReturn($responseMock);
$this->object
->renameFile('test.xml', 'new_test.xml');
}
}