From 77659a1d94dd9e00956060f92e822e013bb63c42 Mon Sep 17 00:00:00 2001 From: Sudheer Kumar Paturi Date: Sat, 29 Feb 2020 17:54:49 +0100 Subject: [PATCH 1/5] End point to retrieve the progress of a given job End point to retrieve progress details of a given job by id Unit, integration tests and example usages of the end point --- examples/jobs-example.php | 54 ++++++++++++++++++---- src/Jobs/JobsApi.php | 15 ++++++ src/Jobs/Params/JobProgressParameters.php | 14 ++++++ tests/functional/JobsApiFunctionalTest.php | 26 +++++++++++ tests/unit/JobsApiTest.php | 40 ++++++++++++++++ 5 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 src/Jobs/Params/JobProgressParameters.php diff --git a/examples/jobs-example.php b/examples/jobs-example.php index aee11a4..b6be6f3 100755 --- a/examples/jobs-example.php +++ b/examples/jobs-example.php @@ -9,6 +9,7 @@ * Be sure you that dependencies are solved bu composer BEFORE running. */ +use Smartling\AuthApi\AuthApiInterface; use Smartling\Jobs\Params\AddFileToJobParameters; $longOpts = [ @@ -44,7 +45,7 @@ $authProvider = \Smartling\AuthApi\AuthTokenProvider::create($userIdentifier, $userSecretKey); /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @return bool */ @@ -80,7 +81,7 @@ function listJobsDemo($authProvider, $projectId) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @return string */ @@ -118,7 +119,7 @@ function createJobDemo($authProvider, $projectId) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return string @@ -156,7 +157,7 @@ function updateJobDemo($authProvider, $projectId, $jobId) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return string @@ -183,7 +184,7 @@ function cancelJobDemo($authProvider, $projectId, $jobId) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return bool @@ -215,7 +216,7 @@ function getJobDemo($authProvider, $projectId, $jobId) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @param string $fileUri * @return bool @@ -251,7 +252,7 @@ function searchJobDemo($authProvider, $projectId, $fileUri) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @param string $fileUri @@ -279,7 +280,7 @@ function addFileToJobDemo($authProvider, $projectId, $jobId, $fileUri) } /** - * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return bool @@ -303,7 +304,43 @@ function authorizeJobDemo($authProvider, $projectId, $jobId) echo \vsprintf('Request took %s seconds.%s', [\round($time, 3), "\n\r"]); } +/** + * @param AuthApiInterface $authProvider + * @param string $projectId + * @param string $jobId + * @param string $targetLocaleId + * @return array + */ +function getJobProgress($authProvider, $projectId, $jobId, $targetLocaleId) +{ + echo "--- Retrieving job progress ---\n"; + + $jobs = \Smartling\Jobs\JobsApi::create($authProvider, $projectId); + $info = FALSE; + $progressParameters = new \Smartling\Jobs\Params\JobProgressParameters(); + $progressParameters->setTargetLocaleId($targetLocaleId); + $st = \microtime(true); + + try { + $info = $jobs->getJobProgress($jobId, $progressParameters); + } catch (\Smartling\Exceptions\SmartlingApiException $e) { + \var_dump($e->getErrors()); + } + + $et = \microtime(true); + $time = $et - $st; + + echo \vsprintf('Request took %s seconds.%s', [\round($time, 3), "\n\r"]); + + if (!empty($info)) { + \var_dump($info); + } + + return $info; +} + $fileUri = 'JobID1_en_fr.xml'; +$targetLocaleId = 'fr-CA'; $jobs = listJobsDemo($authProvider, $projectId); $jobId = createJobDemo($authProvider, $projectId); $jobId = updateJobDemo($authProvider, $projectId, $jobId); @@ -312,3 +349,4 @@ function authorizeJobDemo($authProvider, $projectId, $jobId) $job = searchJobDemo($authProvider, $projectId, $fileUri); authorizeJobDemo($authProvider, $projectId, $jobId); cancelJobDemo($authProvider, $projectId, $jobId); +$jobProgress = getJobProgress($authProvider, $projectId, $jobId, $targetLocaleId); diff --git a/src/Jobs/JobsApi.php b/src/Jobs/JobsApi.php index 48b94cd..36921c5 100755 --- a/src/Jobs/JobsApi.php +++ b/src/Jobs/JobsApi.php @@ -10,6 +10,7 @@ use Smartling\Jobs\Params\AddLocaleToJobParameters; use Smartling\Jobs\Params\CancelJobParameters; use Smartling\Jobs\Params\CreateJobParameters; +use Smartling\Jobs\Params\JobProgressParameters; use Smartling\Jobs\Params\ListJobsParameters; use Smartling\Jobs\Params\SearchJobsParameters; use Smartling\Jobs\Params\UpdateJobParameters; @@ -233,4 +234,18 @@ public function checkAsynchronousProcessingStatus($jobId, $processId) return $this->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET); } + /** + * Returns the status of a job + * + * @param string $jobId Job Id + * @param JobProgressParameters $parameters Job Progress parameters + * @return array + * @throws SmartlingApiException + */ + public function getJobProgress($jobId, JobProgressParameters $parameters) { + $endpoint = \vsprintf('jobs/%s/progress', [$jobId]); + $requestData = $this->getDefaultRequestData('query', $parameters->exportToArray()); + + return $this->sendRequest($endpoint, $requestData, self::HTTP_METHOD_GET); + } } diff --git a/src/Jobs/Params/JobProgressParameters.php b/src/Jobs/Params/JobProgressParameters.php new file mode 100644 index 0000000..9061193 --- /dev/null +++ b/src/Jobs/Params/JobProgressParameters.php @@ -0,0 +1,14 @@ +set('targetLocaleId', $localeId); + } + +} diff --git a/tests/functional/JobsApiFunctionalTest.php b/tests/functional/JobsApiFunctionalTest.php index 970bf60..f5d003c 100755 --- a/tests/functional/JobsApiFunctionalTest.php +++ b/tests/functional/JobsApiFunctionalTest.php @@ -14,6 +14,7 @@ use Smartling\Jobs\Params\AddLocaleToJobParameters; use Smartling\Jobs\Params\CancelJobParameters; use Smartling\Jobs\Params\CreateJobParameters; +use Smartling\Jobs\Params\JobProgressParameters; use Smartling\Jobs\Params\ListJobsParameters; use Smartling\Jobs\Params\SearchJobsParameters; use Smartling\Jobs\Params\UpdateJobParameters; @@ -303,4 +304,29 @@ public function testJobsApiCheckAsynchronousProcessingStatus() } } + /** + * Test for retrieving job progress + */ + public function testJobsApiGetJobProgress() + { + try { + $params = new JobProgressParameters(); + $params->setTargetLocaleId('some_locale'); + + $result = $this->jobsApi->getJobProgress($this->jobId, $params); + + $this->assertArrayHasKey('translationJobUid', $result); + $this->assertArrayHasKey('contentProgressReport', $result); + $this->assertArrayHasKey('targetLocaleDescription', $result); + $this->assertArrayHasKey('targetLocaleId', $result); + $this->assertArrayHasKey('unauthorizedProgressReport', $result); + $this->assertArrayHasKey('percentComplete', $result); + $this->assertArrayHasKey('totalWordCount', $result); + $this->assertArrayHasKey('percentComplete', $result); + $this->assertArrayHasKey('workflowProgressReportList', $result); + } catch (SmartlingApiException $e) { + $this->fail($e->getMessage()); + } + } + } diff --git a/tests/unit/JobsApiTest.php b/tests/unit/JobsApiTest.php index 0ee781f..ee3c516 100755 --- a/tests/unit/JobsApiTest.php +++ b/tests/unit/JobsApiTest.php @@ -4,12 +4,14 @@ use DateTime; use DateTimeZone; +use Smartling\Exceptions\InvalidAccessTokenException; use Smartling\Jobs\JobsApi; use Smartling\Jobs\JobStatus; use Smartling\Jobs\Params\AddFileToJobParameters; use Smartling\Jobs\Params\AddLocaleToJobParameters; use Smartling\Jobs\Params\CancelJobParameters; use Smartling\Jobs\Params\CreateJobParameters; +use Smartling\Jobs\Params\JobProgressParameters; use Smartling\Jobs\Params\ListJobsParameters; use Smartling\Jobs\Params\SearchJobsParameters; use Smartling\Jobs\Params\UpdateJobParameters; @@ -503,4 +505,42 @@ public function testCreateJobParametersSetCallbackMethodValidation() { (new CreateJobParameters())->setCallbackMethod("TEST"); } + + /** + * @covers \Smartling\Jobs\JobsApi::getJobProgress + * + */ + public function testGetJobProgress() + { + $jobId = 'Some job id'; + $localeId = 'some locale id'; + $endpointUrl = \vsprintf('%s/%s/jobs/%s/progress', [ + JobsApi::ENDPOINT_URL, + $this->projectId, + $jobId, + ]); + + $params = new JobProgressParameters(); + $params->setTargetLocaleId($localeId); + + $this->client + ->expects(self::once()) + ->method('request') + ->with('get', $endpointUrl, [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => \vsprintf('%s %s', [ + $this->authProvider->getTokenType(), + $this->authProvider->getAccessToken(), + ]), + ], + 'exceptions' => FALSE, + 'query' => [ + 'targetLocaleId' => $localeId + ], + ]) + ->willReturn($this->responseMock); + + $this->object->getJobProgress($jobId, $params); + } } From 5a05ff3d00623b4c03368eb10548e173106d22b8 Mon Sep 17 00:00:00 2001 From: Sudheer Kumar Paturi Date: Sun, 1 Mar 2020 11:33:52 +0100 Subject: [PATCH 2/5] Removing unnecessary assertions --- tests/functional/JobsApiFunctionalTest.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tests/functional/JobsApiFunctionalTest.php b/tests/functional/JobsApiFunctionalTest.php index f5d003c..5348054 100755 --- a/tests/functional/JobsApiFunctionalTest.php +++ b/tests/functional/JobsApiFunctionalTest.php @@ -311,19 +311,12 @@ public function testJobsApiGetJobProgress() { try { $params = new JobProgressParameters(); - $params->setTargetLocaleId('some_locale'); + $params->setTargetLocaleId('fr'); $result = $this->jobsApi->getJobProgress($this->jobId, $params); - $this->assertArrayHasKey('translationJobUid', $result); $this->assertArrayHasKey('contentProgressReport', $result); - $this->assertArrayHasKey('targetLocaleDescription', $result); - $this->assertArrayHasKey('targetLocaleId', $result); - $this->assertArrayHasKey('unauthorizedProgressReport', $result); - $this->assertArrayHasKey('percentComplete', $result); - $this->assertArrayHasKey('totalWordCount', $result); - $this->assertArrayHasKey('percentComplete', $result); - $this->assertArrayHasKey('workflowProgressReportList', $result); + $this->assertArrayHasKey('progress', $result); } catch (SmartlingApiException $e) { $this->fail($e->getMessage()); } From 8f2f2823c3e9670c077561a50e97351b959345a6 Mon Sep 17 00:00:00 2001 From: Sudheer Kumar Paturi Date: Sun, 1 Mar 2020 11:52:39 +0100 Subject: [PATCH 3/5] removing unused imports and fixing styling issues --- examples/jobs-example.php | 15 +++++++-------- tests/unit/JobsApiTest.php | 1 - 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/jobs-example.php b/examples/jobs-example.php index b6be6f3..b548c25 100755 --- a/examples/jobs-example.php +++ b/examples/jobs-example.php @@ -9,7 +9,6 @@ * Be sure you that dependencies are solved bu composer BEFORE running. */ -use Smartling\AuthApi\AuthApiInterface; use Smartling\Jobs\Params\AddFileToJobParameters; $longOpts = [ @@ -252,7 +251,7 @@ function searchJobDemo($authProvider, $projectId, $fileUri) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @param string $fileUri @@ -280,7 +279,7 @@ function addFileToJobDemo($authProvider, $projectId, $jobId, $fileUri) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return bool @@ -305,10 +304,10 @@ function authorizeJobDemo($authProvider, $projectId, $jobId) } /** - * @param AuthApiInterface $authProvider - * @param string $projectId - * @param string $jobId - * @param string $targetLocaleId + * @param \Smartling\AuthApi\AuthApiInterface $authProvider + * @param string $projectId + * @param string $jobId + * @param string $targetLocaleId * @return array */ function getJobProgress($authProvider, $projectId, $jobId, $targetLocaleId) @@ -340,7 +339,7 @@ function getJobProgress($authProvider, $projectId, $jobId, $targetLocaleId) } $fileUri = 'JobID1_en_fr.xml'; -$targetLocaleId = 'fr-CA'; +$targetLocaleId = 'fr'; $jobs = listJobsDemo($authProvider, $projectId); $jobId = createJobDemo($authProvider, $projectId); $jobId = updateJobDemo($authProvider, $projectId, $jobId); diff --git a/tests/unit/JobsApiTest.php b/tests/unit/JobsApiTest.php index ee3c516..59e2bc3 100755 --- a/tests/unit/JobsApiTest.php +++ b/tests/unit/JobsApiTest.php @@ -4,7 +4,6 @@ use DateTime; use DateTimeZone; -use Smartling\Exceptions\InvalidAccessTokenException; use Smartling\Jobs\JobsApi; use Smartling\Jobs\JobStatus; use Smartling\Jobs\Params\AddFileToJobParameters; From b1aabc3cb77d0c97ed39c417d6d84a769e176ed8 Mon Sep 17 00:00:00 2001 From: Sudheer Kumar Paturi Date: Sun, 1 Mar 2020 11:56:44 +0100 Subject: [PATCH 4/5] using fully qualified imports instead of simplified imports --- examples/jobs-example.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/jobs-example.php b/examples/jobs-example.php index b548c25..f24f5d1 100755 --- a/examples/jobs-example.php +++ b/examples/jobs-example.php @@ -44,7 +44,7 @@ $authProvider = \Smartling\AuthApi\AuthTokenProvider::create($userIdentifier, $userSecretKey); /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @return bool */ @@ -80,7 +80,7 @@ function listJobsDemo($authProvider, $projectId) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @return string */ @@ -118,7 +118,7 @@ function createJobDemo($authProvider, $projectId) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return string @@ -156,7 +156,7 @@ function updateJobDemo($authProvider, $projectId, $jobId) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return string @@ -183,7 +183,7 @@ function cancelJobDemo($authProvider, $projectId, $jobId) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @param string $jobId * @return bool @@ -215,7 +215,7 @@ function getJobDemo($authProvider, $projectId, $jobId) } /** - * @param AuthApiInterface $authProvider + * @param \Smartling\AuthApi\AuthApiInterface $authProvider * @param string $projectId * @param string $fileUri * @return bool From 7b5574cea030311f6a80acc87b0a9aad28b65340 Mon Sep 17 00:00:00 2001 From: Sudheer Kumar Paturi Date: Sun, 1 Mar 2020 12:05:25 +0100 Subject: [PATCH 5/5] Fixing unit tests of getjobProgress Argument 1 passed to Smartling\Jobs\Params\JobProgressParameters::setTargetLocaleId() must be an instance of Smartling\Jobs\Params\string, string given, called in /home/travis/build/Smartling/api-sdk-php/tests/unit/JobsApiTest.php on line 523 and defined --- src/Jobs/Params/JobProgressParameters.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Jobs/Params/JobProgressParameters.php b/src/Jobs/Params/JobProgressParameters.php index 9061193..9a91dc7 100644 --- a/src/Jobs/Params/JobProgressParameters.php +++ b/src/Jobs/Params/JobProgressParameters.php @@ -7,7 +7,10 @@ class JobProgressParameters extends BaseParameters { - public function setTargetLocaleId(string $localeId) { + /** + * @param string $localeId + */ + public function setTargetLocaleId($localeId) { $this->set('targetLocaleId', $localeId); }