Compare commits

...

23 Commits

Author SHA1 Message Date
8d189ae541
fix(TM): List is now working again
It was referencing the new memsource API url (v2), but list still uses
v1.
2022-12-30 10:50:14 -03:00
a22f23cc10
test: adding phpUnit 2022-12-23 00:27:51 -03:00
adfea614ad
feat: added new Async and TM modules on main file 2022-12-23 00:26:18 -03:00
1788befec6
refactor: moving saveIntoFile to Helper function 2022-12-23 00:25:50 -03:00
228b3a06e1
feat: adding TM module 2022-12-23 00:25:12 -03:00
aab66427a8
refactor: creating Helper class and moving saveIntoFile function 2022-12-23 00:24:41 -03:00
1a3c690138
feat: added async module 2022-12-23 00:24:24 -03:00
f14492d744
Include edit Project Status 2021-02-12 19:03:12 -03:00
b739763448
Include job download source and target files 2021-02-12 19:02:25 -03:00
abda9e4ee8
Fix the pre-push hook error with different phpunit versions
Now the pre-push hook check first for a composer phpunit, it not found,
try to use a system wide one. If none of them are available, it will
rise an error
2021-02-11 18:29:32 -03:00
c88478f7d8
Includes file extension on bilingual download
It is important to choose the file extension, so including it on the
filename would be the easier way for us, as we already have the
extension when using it.
2021-02-11 18:25:32 -03:00
37ebdabc20 Improving Makefile and PHPUNIT dependency on composer.json 2019-06-28 02:21:55 -03:00
92409b9571 Avoiding composer.lock to be commited 2019-06-28 00:58:33 -03:00
8e14eb86b3 Added Makefile to install the hook for automatic test 2019-06-28 00:54:06 -03:00
dc437d7044
Fixing a project Test
The test was to make sure it would return a error on No token, but it
was providing a token. More than that, it were a simple and direct check
if it were throwing an error.

Now it is checking for the specific Missing Access Token error message.
2019-06-27 20:21:43 -03:00
51980ebf14
Including git hook to run tests 2019-06-27 20:05:02 -03:00
283e85b834
Refactoring Oauth and Memsource classes 2019-06-27 20:03:41 -03:00
aaabb91039
Including Tests and verifications for errors 2019-06-27 20:03:10 -03:00
e4f2bd04be
Fixing README command with wrong function name 2019-06-25 20:35:31 -03:00
97e0a56b65
Updating oauth.php file to match the new base api 2019-06-25 18:08:21 -03:00
13fadfe9ae
Refactoring function names as no one is using it yet 2019-06-25 10:49:57 -03:00
6f19522c52
Including more information on README.md 2019-06-25 01:29:13 -03:00
3c6d2f0442
Improving README.md file 2019-06-24 22:06:43 -03:00
20 changed files with 638 additions and 88 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ tags
vendor/
.php_cs.cache
*.mxliff
composer.lock

13
Makefile Normal file
View File

@ -0,0 +1,13 @@
install:
command -v composer && composer install || echo "Please, install Composer to use tests"
command -v phpunit || command -v composer && composer require --dev phpunit/phpunit ^8
chmod +x ./pre-push.sh
mkdir -p .git/hooks
ln -fs ../../pre-push.sh .git/hooks/pre-push
uninstall:
rm -f ./.git/hooks/pre-push
if [ -d "./vendor/phpunit" ] ; then composer remove --dev phpunit/phpunit; fi
check:
gaze '{src,tests}/**/*.php' -c "./vendor/phpunit/phpunit/phpunit --bootstrap vendor/autoload.php tests --testdox --color"

115
README.md
View File

@ -4,53 +4,128 @@ I am creating this Memsource API as a way to learn how to deal with one and to u
There are other Memsource API repositories on GibHub that appears to be fully functional if you need it.
## Getting an Access Token
## Installing
To be able to use the Memsource API, you need an **access token**, but to get it, you need to:
Install it with [Composer](https://getcomposer.org/):
### Register as a developer on Memsource website
1. Create a `composer.json` file with the following content:
So you will receive your:
- *client id*
- *client secret*
```json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/brunofontes/Memsource-API"
}
],
"require": {
"brunofontes/memsource-api": "*"
}
}
```
### Get an Authorization Code
2. Run `php composer.phar install`
3. Add the following line on your .php file:
```php
require_once __DIR__ . '/vendor/autoload.php';
```
## Using
This repository returns a JSON string for almost any command.
If you are not sure how to use it, just convert it to an object or an array as follows:
```php
$myObject = json_decode($response);
$myArray = json_decode($response, true);
```
### Create an instance
```php
$memsource = new \BrunoFontes\Memsource();
$url = $memsource->oauth()->getAuthorizationCodeUrl($cliend_id, $callback_uri);
```
- If you have already an access token, just include it:
```php
$memsource = new \BrunoFontes\Memsource($token);
```
### Getting an Access Token
To be able to use the Memsource API, you need an **access token**. In order to get it, just follow the instructions below.
#### Register as a developer on Memsource website
So you will receive your:
- *client id*
- *client secret*
#### Get an Authorization Code
```php
$memsourceUrl = $memsource->oauth()->getAuthorizationCodeUrl($cliend_id, $callback_uri);
```
Redirect your browser to this returned `$url` so the user can login via *oauth*.
The `$callback_uri` will be called by **Memsource** with a `$_GET['code']` that contains your Authorization Code, which you can use to...
The `$callback_uri` will be called by *Memsource* with a `$_GET['code']` that contains your Authorization Code, which you can use to...
### Get an Access Token
#### Get an Access Token
```php
$authCode = $_GET['code'];
$memsource = new \BrunoFontes\Memsource();
$token = $memsource->oauth()->getAccessToken($authCode, $client_id, $client_secret, $callback_uri);
```
Safely store this `$token` with the related user data and use it on any
Safely store this `$token` with the related user data and use it to instantiate the class whenever it were necessary.
## Project
### Project
### Project list
#### Project list
To list all projects...
To list all projects:
```php
$memsource = new \BrunoFontes\Memsource();
$projectList = $memsource->project()->listProjects;
$projectList = $memsource->project()->list;
```
To use filters, add the API filter as parâmeter:
To use filters, add the API filter as parameter:
```php
$projectList = $memsource->project()->listProjects(['name' => 'Project X']);
$projectList = $memsource->project()->list(['name' => 'Project X']);
```
## Bilingual Files
#### Get Project
```php
$projectList = $memsource->project()->get($projectUid);
```
### Jobs
#### List Jobs
Only projectUid is essencial:
```php
$jobs = $memsource->jobs()->list($projectUid, ['count' => true, 'filename' => 'my_file.html']);
```
### Bilingual Files
#### Download Bilingual File
```php
$memsource->BilingualFile()->download($projectUid, ['JobUid1', 'jobUid2'], 'download.mxliff');
```
#### Upload Bilingual File
```php
$parameters = ['format' => 'MXLF', 'saveToTransMemory' => 'None', 'setCompleted' => 'false'];
$result = $api->bilingualFile()->upload('upload.mxliff', $parameters);
```

View File

@ -2,6 +2,7 @@
"name": "brunofontes/memsource-api",
"description": "A personal memsource api to better understand how it works",
"license": "GPL-3.0-or-later",
"version": "1.2",
"authors": [
{
"name": "Bruno F. Fontes",
@ -20,5 +21,8 @@
"type": "vcs",
"url": "https://github.com/brunofontes/Memsource-API"
}
]
],
"require-dev": {
"phpunit/phpunit": "^8"
}
}

11
pre-push.sh Executable file
View File

@ -0,0 +1,11 @@
params="--bootstrap vendor/autoload.php tests --testdox --color"
if [ -d ./vendor/phpunit ]; then
./vendor/phpunit/phpunit/phpunit $params
else
if [ $(command -v phpunit) ]; then
phpunit $params
else
echo "Please, run 'make install' or install phpunit globally to run the tests"
fi
fi

32
src/Async.php Normal file
View File

@ -0,0 +1,32 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class Async extends \BrunoFontes\Memsource\BaseApi
{
private $_url = '/api2/v1/async';
/**
* Get asynchronous request
*
* @param string $queryParams An array with the Query parameters to filter projects
*
* @return string The JSON answer from Memsource
*/
public function getAsyncRequest(string $asyncRequestId, array $queryParams = []): string
{
$response = $this->fetchApi->fetch('get', "{$this->_url}/{$asyncRequestId}", $queryParams);
if ($this->hasError($response)) {
throw new \Exception("Error listing AsyncRequest: " . $this->getError($response), 1);
}
return $response;
}
}

View File

@ -15,4 +15,15 @@ class BaseApi
{
$this->fetchApi = $fetchApi;
}
protected function hasError(string $jsonResponse): bool
{
return isset(json_decode($jsonResponse, true)['errorCode']);
}
protected function getError(string $jsonResponse): string
{
return json_decode($jsonResponse, true)['errorDescription'];
}
}

View File

@ -5,6 +5,7 @@
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class BilingualFile extends \BrunoFontes\Memsource\BaseApi
@ -27,16 +28,24 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
*
* @return array A list of the downloaded files
*/
public function downloadBilingualFile(string $projectUid, array $jobUids, string $filename): array
public function download(string $projectUid, array $jobUids, string $filename): array
{
$url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile";
$fileExtension = pathinfo($filename)['extension'];
$fileNoExtension = basename($filename,'.'.$fileExtension);
;
$url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile?format=" . strtoupper($fileExtension);
$filenames = [];
$groupedJobUids = array_chunk($jobUids, 100);
for ($i = 0; $i < count($groupedJobUids); $i++) {
$apiReadyArray = $this->_convertUidArrayToApiRequest($groupedJobUids[$i]);
$filenames[$i] = count($groupedJobUids) > 1?"{$i}_{$filename}":$filename;
$filenames[$i] = count($groupedJobUids) > 1 ? "{$fileNoExtension}_{$i}.{$fileExtension}" : $filename;
$filecontent = $this->fetchApi->fetch('jsonPost', $url, $apiReadyArray);
$this->_saveIntoFile($filenames[$i], $filecontent);
if ($this->hasError($filecontent)) {
$errorMsg = $this->getError($filecontent);
throw new \Exception("Error downloading file: {$errorMsg}", 1);
}
Helper::saveIntoFile($filenames[$i], $filecontent);
}
return $filenames;
}
@ -57,17 +66,6 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
return ['jobs' => $convertedArray];
}
private function _saveIntoFile(string $filename, string $filecontent): void
{
try {
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
} catch (\Exception $e) {
throw new \Exception("File could not be saved: {$e->error}", 1);
}
}
/**
* Upload a bilingual file to Memsource
*
@ -76,13 +74,20 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi
*
* @return string The http request responde from API in json format
*/
public function uploadBilingualFile(string $filename, array $params): string
public function upload(string $filename, array $params): string
{
$urlParams = http_build_query($params);
$fileContent = file_get_contents($filename);
if ($fileContent === false) {
throw new \Exception('File for upload inexistent or invalid', 1);
try {
$fileContent = file_get_contents($filename);
} catch (\Exception $e) {
throw new \Exception('File for upload inexistent or invalid: ' . $filename, 1);
}
return $this->fetchApi->fetch('put', $this->_url . "?{$urlParams}", [$fileContent]);
$response = $this->fetchApi->fetch('put', $this->_url . "?{$urlParams}", [$fileContent]);
if ($this->hasError($response)) {
throw new \Exception("Error uploading file {$filename}: " . $this->getError($response), 1);
}
return $response;
}
}

View File

@ -12,13 +12,18 @@ class FetchApi
protected $base_url;
protected $token;
public function getBase_url()
{
return $this->base_url;
}
/**
* BaseAPI needs at least the Memsource Token to use it's API
*
* @param string $token
* @param string $memsourceBaseUrl [Optional] A non-standard Memsource URL base for the API to work
*/
public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web')
public function __construct(string $token = null, string $memsourceBaseUrl)
{
$this->base_url = $memsourceBaseUrl;
$this->token = $token;
@ -27,7 +32,7 @@ class FetchApi
/**
* Fetch API data using curl
*
* @param string $method Should be 'get', 'post', 'jsonPost' or 'download'
* @param string $method Should be 'get', 'put', 'post', 'jsonPost', 'download' or 'raw'
* @param string $url The api url
* @param array $parameters Array ['key' => 'value'] of get or post fields or structured array for json requests
* @param string $filename [optional] Specified file in which the download request will be saved
@ -39,30 +44,47 @@ class FetchApi
$setopt = [];
switch ($method) {
case 'get':
$this->checkAccessToken();
$parameters = http_build_query($parameters);
$url = $url . ($parameters ? '?'.$parameters : '');
break;
case 'put':
$this->checkAccessToken();
$setopt = $this->getPutParam()+$this->getPostParam(implode("", $parameters));
break;
case 'post':
$this->checkAccessToken();
$parameters = http_build_query($parameters);
$setopt = $setopt + $this->getPostParam($parameters);
break;
case 'jsonPost':
$this->checkAccessToken();
$setopt = $this->getJsonPostParam($parameters);
break;
case 'download':
$this->checkAccessToken();
if (empty($filename)) {
throw new Exception('You need to specify a filename to download a file.', 1);
}
$setopt = $this->getDownloadFileParam($filename)
+ $this->getJsonPostParam($parameters);
break;
case 'raw':
$setopt = $parameters;
break;
default:
throw new \Exception("Method {$method} is invalid on Fetch", 1);
}
return $this->curl($url, $setopt);
}
private function checkAccessToken()
{
if (empty($this->token)) {
throw new \Exception("Missing Access Token", 1);
}
}
private function getDownloadFileParam(string $filename)
{
return [
@ -98,10 +120,10 @@ class FetchApi
protected function curl(string $url, array $curl_extra_setopt = [])
{
if (empty($url)) {
throw new Exception('URL not defined', 1);
throw new \Exception('URL not defined', 1);
}
$header = $this->token ? ["Authorization: Bearer {$this->token}"] : [];
$header = ($this->token ? ["Authorization: Bearer {$this->token}"] : []);
$header = array_merge($header, $curl_extra_setopt[CURLOPT_HTTPHEADER]??[]);
$curl_setopt = [
CURLOPT_URL => $this->base_url . $url,

19
src/Helper.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace BrunoFontes\Memsource;
class Helper
{
public static function saveIntoFile(string $filename, string $filecontent): void
{
try {
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
} catch (\Exception $e) {
throw new \Exception("File could not be saved: {$e->error}", 1);
}
}
}

View File

@ -10,7 +10,6 @@ namespace BrunoFontes\Memsource;
class Jobs extends \BrunoFontes\Memsource\BaseApi
{
/**
* List jobs of a project
* The API request returns a MAX of 50 Jobs.
@ -21,9 +20,48 @@ class Jobs extends \BrunoFontes\Memsource\BaseApi
*
* @return string The JSON answer from Memsource
*/
public function listJobs(string $projectUid, array $parameters = []): string
public function list(string $projectUid, array $parameters = []): string
{
$url = "/api2/v2/projects/{$projectUid}/jobs";
return $this->fetchApi->fetch('get', $url, $parameters);
$response = $this->fetchApi->fetch('get', $url, $parameters);
if ($this->hasError($response)) {
throw new \Exception("Error listing projects: " . $this->getError($response), 1);
}
return $response;
}
/**
* Download a target file
*
* @param string $projectUid The project uid which contain the jobs
* @param string $jobUid Job uid to be download
* @param string $filename Filename of the saved file
* @param string $format File format: ORIGINAL or PDF
*/
public function downloadTargetFile(string $projectUid, string $jobUid, string $filename, string $format = "ORIGINAL")
{
$url = "/api2/v1/projects/{$projectUid}/jobs/{$jobUid}/targetFile";
$filecontent = $this->fetchApi->fetch('get', $url);
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
}
/**
* Download a source file
*
* @param string $projectUid The project uid which contain the jobs
* @param string $jobUid Job uid to be download
*/
public function downloadOriginalFile(string $projectUid, string $jobUid, string $filename)
{
$url = "/api2/v1/projects/{$projectUid}/jobs/{$jobUid}/original";
$filecontent = $this->fetchApi->fetch('get', $url);
$f = fopen($filename, 'w+');
fwrite($f, $filecontent);
fclose($f);
}
}

View File

@ -8,6 +8,14 @@
namespace BrunoFontes;
use \BrunoFontes\Memsource\BilingualFile;
use \BrunoFontes\Memsource\FetchApi;
use \BrunoFontes\Memsource\Jobs;
use \BrunoFontes\Memsource\Oauth;
use \BrunoFontes\Memsource\Project;
use \BrunoFontes\Memsource\TM;
use \BrunoFontes\Memsource\Async;
/**
* Memsource API class
*
@ -20,51 +28,72 @@ class Memsource
private $_bilingualFile;
private $_jobs;
private $_project;
private $_tm;
private $_async;
private $_fetchApi;
public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web')
{
$this->_fetchApi = new \BrunoFontes\Memsource\FetchApi($token, $memsourceBaseUrl);
$this->_fetchApi = new FetchApi($token, $memsourceBaseUrl);
}
/**
* Memsource Oauth functions
*
* @return \BrunoFontes\Memsource\Oauth
* @return Oauth
*/
public function oauth(): \BrunoFontes\Memsource\Oauth
public function oauth(): Oauth
{
return $this->_oauth ?? $this->_oauth = new \BrunoFontes\Memsource\oauth();
return $this->_oauth ?? $this->_oauth = new oauth($this->_fetchApi);
}
/**
* Memsource API BilingualFile related functions
*
* @return \BrunoFontes\Memsource\BilingualFile
* @return BilingualFile
*/
public function bilingualFile(): \BrunoFontes\Memsource\BilingualFile
public function bilingualFile(): BilingualFile
{
return $this->_bilingualFile ?? $this->_bilingualFile = new \BrunoFontes\Memsource\BilingualFile($this->_fetchApi);
return $this->_bilingualFile ?? $this->_bilingualFile = new BilingualFile($this->_fetchApi);
}
/**
* Memsource API Jobs related functions
*
* @return \BrunoFontes\Memsource\Jobs
* @return Jobs
*/
public function jobs(): \BrunoFontes\Memsource\Jobs
public function jobs(): Jobs
{
return $this->_jobs ?? $this->_jobs = new \BrunoFontes\Memsource\Jobs($this->_fetchApi);
return $this->_jobs ?? $this->_jobs = new Jobs($this->_fetchApi);
}
/**
* Memsource API Project related functions
*
* @return \BrunoFontes\Memsource\Project
* @return Project
*/
public function project(): \BrunoFontes\Memsource\Project
public function project(): Project
{
return $this->_project ?? $this->_project = new \BrunoFontes\Memsource\Project($this->_fetchApi);
return $this->_project ?? $this->_project = new Project($this->_fetchApi);
}
/**
* Memsource API Async related functions
*
* @return Async
*/
public function async(): Async
{
return $this->_async ?? $this->_async = new Async($this->_fetchApi);
}
/**
* Memsource API TM related functions
*
* @return TM
*/
public function tm(): TM
{
return $this->_tm ?? $this->_tm = new TM($this->_fetchApi);
}
}

View File

@ -5,9 +5,10 @@
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class Oauth extends \BrunoFontes\Memsource
class Oauth extends \BrunoFontes\Memsource\BaseApi
{
private $_url = '/oauth';
@ -22,8 +23,16 @@ class Oauth extends \BrunoFontes\Memsource
*/
public function getAuthorizationCodeUrl(string $client_id, string $callback_uri)
{
$authorize_url = $this->base_url . $this->_url . '/authorize';
return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
$authorize_url = $this->fetchApi->getBase_url() . $this->_url . '/authorize';
$parambeters = http_build_query(
[
"response_type" => 'code',
"client_id" => $client_id,
"redirect_uri" => $callback_uri,
"scope" => 'openid'
]
);
return "{$authorize_url}?{$parambeters}";
}
public function getAccessToken(string $authorization_code, string $client_id, string $client_secret, string $callback_uri)
@ -37,10 +46,12 @@ class Oauth extends \BrunoFontes\Memsource
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $content
];
$response = $this->curl($token_url, $params);
if ($respose['error']) {
throw new Exception("Error getting access token", 1);
$response = json_decode($this->fetchApi->fetch('raw', $token_url, $params), true);
if (isset($response['error'])) {
throw new \Exception("Error getting TOKEN: " . $response['error_description'], 1);
}
return $response['access_token'];
}
}

View File

@ -19,9 +19,13 @@ class Project extends \BrunoFontes\Memsource\BaseApi
*
* @return string The JSON answer from Memsource
*/
public function listProjects(array $queryParams = []): string
public function list(array $queryParams = []): string
{
return $this->fetchApi->fetch('get', $this->_url, $queryParams);
$response = $this->fetchApi->fetch('get', $this->_url, $queryParams);
if ($this->hasError($response)) {
throw new \Exception("Error listing projects: " . $this->getError($response), 1);
}
return $response;
}
/**
@ -31,8 +35,26 @@ class Project extends \BrunoFontes\Memsource\BaseApi
*
* @return string A json string with all project info
*/
public function getProject(string $projectUid): string
public function get(string $projectUid): string
{
return $this->fetchApi->fetch('get', "{$this->_url}/{$projectUid}");
$response = $this->fetchApi->fetch('get', "{$this->_url}/{$projectUid}");
if ($this->hasError($response)) {
throw new \Exception("Error getting project {$projectUid}: " . $this->getError($response), 1);
}
}
/**
* Edit the project status
*
* @param string $projectUid The project UID
* @param string $status The new status
*/
public function editStatus(string $projectUid, string $status): void
{
$queryParam = ['status' => $status];
$response = $this->fetchApi->fetch('jsonPost', "{$this->_url}/{$projectUid}/setStatus", $queryParam);
if ($this->hasError($response)) {
throw new \Exception("Error editing project status on project: {$projectUid}: " . $this->getError($response), 1);
}
}
}

74
src/TM.php Normal file
View File

@ -0,0 +1,74 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
enum ExportFormat
{
case TMX;
case XLSX;
}
class TM extends \BrunoFontes\Memsource\BaseApi
{
private $_url = '/api2/v2/transMemories';
/**
* List projects
*
* @param string $queryParams An array with the Query parameters to filter projects
*
* @return string The JSON answer from Memsource
*/
public function list(array $queryParams = []): string
{
$response = $this->fetchApi->fetch('get', '/api2/v1/transMemories/', $queryParams);
if ($this->hasError($response)) {
throw new \Exception("Error listing TMs: " . $this->getError($response), 1);
}
return $response;
}
/**
* Export a TM in an async way
*
* @param string $tmUid The TM UID
* @param array[string] $targetLangs The language pairs to export
*
* @return string A json string with all translation memories info
*/
public function export(string $tmUid, array $targetLangs): string
{
$queryParam['exportTargetlangs'] = $targetLangs;
$response = $this->fetchApi->fetch('jsonPost', "{$this->_url}/{$tmUid}/export", $queryParam);
if ($this->hasError($response)) {
throw new \Exception("Error getting tm {$tmUid}: " . $this->getError($response), 1);
}
return $response;
}
/**
* Download a TM. You need to export the TM first in order to obtain the
* asyncExport id
*
* @param string $asyncId The asyncId obtainable by export function
* @param ExportFormat $format The file format that will be exported
* @param string $filename The filename that will be created to store the
* downloaded TM
*/
public function download(string $asyncId, string $filename, ExportFormat $fileFormat = ExportFormat::TMX)
{
$queryParam['format'] = $fileFormat->name;
$filecontent = $this->fetchApi->fetch('get', "/api2/v1/transMemories/downloadExport/{$asyncId}/", $queryParam);
if ($this->hasError($filecontent)) {
throw new \Exception("Error downloading TM asyncID {$asyncId}: " . $this->getError($filecontent), 1);
}
Helper::saveIntoFile($filename, $filecontent);
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use BrunoFontes\Memsource;
final class BilingualFileTest extends TestCase
{
public function testEmptyDownloadJobUidListShouldReturnEmptyFilenames()
{
$api = new Memsource('fakeToken');
$this->assertEquals(
[],
$api->bilingualFile()->download('uid', [], 'filename.xliff')
);
}
public function testInvalidDownloadUidsShouldThrowError()
{
$api = new Memsource('fakeToken');
$this->expectException(\Exception::class);
$api->bilingualFile()->download('uid', ['a'], 'filename.xliff');
}
public function testUploadInexistentFileShouldThrowError()
{
$api = new Memsource('fakeToken');
$this->expectException(\Exception::class);
$api->bilingualFile()->upload('myInvalidFile', []);
}
public function testUploadWithNoTokenShouldThrowError()
{
$api = new Memsource('fakeToken');
$this->expectException(\Exception::class);
$api->bilingualFile()->upload('tests/bilingualFileTest.php', []);
}
}

57
tests/fetchApiTest.php Normal file
View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use BrunoFontes\Memsource\FetchApi;
final class FetchApiTest extends TestCase
{
public function testEmptyFetchUrlShouldThrowError()
{
$fetch = new FetchApi('fakeToken', 'https://google.com');
$this->expectExceptionMessage('URL not defined');
$this->assertNotEmpty($fetch->fetch('get', ''));
}
public function testNotEmptyTokenOnFetchShouldNotThrowError()
{
$fetch = new FetchApi('fakeToken', 'https://google.com');
$this->assertNotEmpty($fetch->fetch('get', '/'));
}
public function testEmptyTokenOnFetchRawShouldNotThrowError()
{
$fetch = new FetchApi(null, 'http://google.com');
$this->assertNotEmpty($fetch->fetch('raw', '/'));
}
public function testEmptyTokenOnFetchGetShouldThrowError()
{
$fetch = new FetchApi(null, 'http://testUrl.com');
$this->expectExceptionMessage('Missing Access Token');
$fetch->fetch('get', 'url');
}
public function testEmptyTokenOnFetchPutShouldThrowError()
{
$fetch = new FetchApi(null, 'http://testUrl.com');
$this->expectExceptionMessage('Missing Access Token');
$fetch->fetch('put', 'url');
}
public function testEmptyTokenOnFetchPostShouldThrowError()
{
$fetch = new FetchApi(null, 'http://testUrl.com');
$this->expectExceptionMessage('Missing Access Token');
$fetch->fetch('post', 'url');
}
public function testEmptyTokenOnFetchJsonPostShouldThrowError()
{
$fetch = new FetchApi(null, 'http://testUrl.com');
$this->expectExceptionMessage('Missing Access Token');
$fetch->fetch('jsonPost', 'url');
}
public function testEmptyTokenOnFetchDownloadShouldThrowError()
{
$fetch = new FetchApi(null, 'http://testUrl.com');
$this->expectExceptionMessage('Missing Access Token');
$fetch->fetch('download', 'url');
}
}

33
tests/jobsTest.php Normal file
View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use BrunoFontes\Memsource;
final class JobsTest extends TestCase
{
public function testInvalidProjectUidShouldThrowError()
{
$api = new Memsource('fakeToken');
$this->expectException(\Exception::class);
$api->jobs()->list('invalidProjectUid', []);
}
public function testDownloadTargetFileReturnsNull()
{
$api = new Memsource('fakeToken');
$this->assertEquals(
null,
$api->jobs()->downloadTargetFile('projUid', 'jobUid', 'filename.xliff')
);
}
public function testDownloadOriginalFileReturnsNull()
{
$api = new Memsource('fakeToken');
$this->assertEquals(
null,
$api->jobs()->downloadOriginalFile('projUid', 'jobUid', 'filename.xliff')
);
}
}

34
tests/oauthTest.php Normal file
View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use BrunoFontes\Memsource;
final class oauthTest extends TestCase
{
public function testOauthGetAuthorizationCodeUrl()
{
$customServerURL = 'http://myPersonalMemsource.com';
$api = new Memsource(null, $customServerURL);
$expected = $customServerURL . '/oauth/authorize?response_type=code&client_id=id&redirect_uri=http%3A%2F%2Furi&scope=openid';
$memsourceUrl = $api->oauth()->getAuthorizationCodeUrl('id', 'http://uri');
$this->assertEquals(
$expected,
$memsourceUrl
);
}
public function testGetAccessTokenExceptionOnFakeCode()
{
$api = new Memsource();
$this->expectException(\Exception::class);
$token = $api->oauth()->getAccessToken('fakeCode', 'fakeId', 'fakePass', 'http://any');
}
public function testGetAccessTokenExceptionOnEmptyCode()
{
$api = new Memsource();
$this->expectException(\Exception::class);
$token = $api->oauth()->getAccessToken('', '', '', '');
}
}

22
tests/projectTest.php Normal file
View File

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use BrunoFontes\Memsource;
final class ProjectTest extends TestCase
{
public function testNoTokenShouldThrowError()
{
$api = new Memsource();
$this->expectExceptionMessage('Missing Access Token');
$api->project()->list();
}
public function testInvalidProjectUidShouldThrowError()
{
$api = new Memsource('fakeToken');
$this->expectException(\Exception::class);
$api->project()->get('invalidProjectUid');
}
}