View source
<?php
namespace Smartling\Tests\Functional;
use GuzzleHttp\Psr7\Stream;
use PHPUnit_Framework_TestCase;
use Smartling\AuthApi\AuthTokenProvider;
use Smartling\Exceptions\SmartlingApiException;
use Smartling\File\FileApi;
use Smartling\File\Params\DownloadFileParameters;
use Smartling\File\Params\ListFilesParameters;
class FileApiFunctionalTest extends PHPUnit_Framework_TestCase {
private $fileApi;
const FILE_NAME = 'test.xml';
const NEW_FILE_NAME = 'new_test.xml';
private $fileUri;
private $fileRealPath;
private $fileType;
private $retrievalType;
private $translationState;
private $targetLocale;
public static function tearDownAfterClass() {
$authProvider = AuthTokenProvider::create(getenv('user_id'), getenv('user_key'));
$fileApi = FileApi::create($authProvider, getenv('project_id'));
foreach ([
self::FILE_NAME,
self::NEW_FILE_NAME,
] as $file) {
try {
$fileApi
->deleteFile($file);
} catch (SmartlingApiException $e) {
}
}
}
public function setUp() {
$projectId = getenv('project_id');
$userIdentifier = getenv('user_id');
$userSecretKey = getenv('user_key');
if (empty($projectId) || empty($userIdentifier) || empty($userSecretKey)) {
$this
->fail('Missing required parameters');
}
$this->fileUri = 'tests/resources/test.xml';
$this->fileRealPath = realpath($this->fileUri);
$this->fileType = 'xml';
$this->retrievalType = 'pseudo';
$this->translationState = 'PUBLISHED';
$this->targetLocale = 'ru-RU';
$authProvider = AuthTokenProvider::create($userIdentifier, $userSecretKey);
$this->fileApi = FileApi::create($authProvider, $projectId);
}
public function testFileApiUploadFile() {
try {
$result = $this->fileApi
->uploadFile($this->fileRealPath, self::FILE_NAME, $this->fileType);
$this
->assertArrayHasKey('wordCount', $result);
$this
->assertArrayHasKey('stringCount', $result);
$this
->assertArrayHasKey('overWritten', $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiLastModified() {
try {
$result = $this->fileApi
->lastModified(self::FILE_NAME);
$this
->assertArrayHasKey('totalCount', $result);
$this
->assertArrayHasKey('items', $result);
date_default_timezone_set('Pacific/Auckland');
$result_new_timezone = $this->fileApi
->lastModified(self::FILE_NAME);
$this
->assertEquals($result['items'][0]['lastModified']
->getTimestamp(), $result_new_timezone['items'][0]['lastModified']
->getTimestamp());
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiDownloadFile() {
try {
$params = new DownloadFileParameters();
$params
->setRetrievalType($this->retrievalType);
$result = $this->fileApi
->downloadFile(self::FILE_NAME, $this->targetLocale, $params);
$this
->assertInstanceOf(Stream::class, $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiGetStatus() {
try {
$result = $this->fileApi
->getStatus(self::FILE_NAME, $this->targetLocale);
$this
->assertArrayHasKey('fileUri', $result);
$this
->assertArrayHasKey('lastUploaded', $result);
$this
->assertArrayHasKey('created', $result);
$this
->assertArrayHasKey('fileType', $result);
$this
->assertArrayHasKey('parserVersion', $result);
$this
->assertArrayHasKey('hasInstructions', $result);
$this
->assertArrayHasKey('totalStringCount', $result);
$this
->assertArrayHasKey('totalWordCount', $result);
$this
->assertArrayHasKey('authorizedStringCount', $result);
$this
->assertArrayHasKey('authorizedWordCount', $result);
$this
->assertArrayHasKey('completedStringCount', $result);
$this
->assertArrayHasKey('completedWordCount', $result);
$this
->assertArrayHasKey('excludedStringCount', $result);
$this
->assertArrayHasKey('excludedWordCount', $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiGetStatusForAllLocales() {
try {
$result = $this->fileApi
->getStatusForAllLocales(self::FILE_NAME);
$this
->assertArrayHasKey('fileUri', $result);
$this
->assertArrayHasKey('lastUploaded', $result);
$this
->assertArrayHasKey('created', $result);
$this
->assertArrayHasKey('fileType', $result);
$this
->assertArrayHasKey('parserVersion', $result);
$this
->assertArrayHasKey('hasInstructions', $result);
$this
->assertArrayHasKey('totalStringCount', $result);
$this
->assertArrayHasKey('totalWordCount', $result);
$this
->assertArrayHasKey('totalCount', $result);
$this
->assertArrayHasKey('items', $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiGetAuthorizedLocales() {
try {
$result = $this->fileApi
->getAuthorizedLocales(self::FILE_NAME);
$this
->assertArrayHasKey('items', $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiGetList() {
try {
$params = new ListFilesParameters();
$params
->setFileTypes($this->fileType)
->setUriMask('test')
->setLimit(5);
$result = $this->fileApi
->getList($params);
$this
->assertArrayHasKey('items', $result);
$this
->assertArrayHasKey('totalCount', $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiImport() {
try {
$result = $this->fileApi
->import($this->targetLocale, self::FILE_NAME, $this->fileType, $this->fileRealPath, $this->translationState, TRUE);
$this
->assertArrayHasKey('wordCount', $result);
$this
->assertArrayHasKey('stringCount', $result);
$this
->assertArrayHasKey('translationImportErrors', $result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiRenameFile() {
try {
$result = $this->fileApi
->renameFile(self::FILE_NAME, self::NEW_FILE_NAME);
$this
->assertTrue($result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
public function testFileApiDeleteFile() {
try {
$result = $this->fileApi
->deleteFile(self::NEW_FILE_NAME);
$this
->assertTrue($result);
} catch (SmartlingApiException $e) {
$this
->fail($e
->getMessage());
}
}
}