diff --git a/src/BaseApi.php b/src/BaseApi.php index 2cbe7c4..e9525e5 100644 --- a/src/BaseApi.php +++ b/src/BaseApi.php @@ -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']; + } + } diff --git a/src/BilingualFile.php b/src/BilingualFile.php index e34e7f0..2e1bba1 100644 --- a/src/BilingualFile.php +++ b/src/BilingualFile.php @@ -31,12 +31,17 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi public function download(string $projectUid, array $jobUids, string $filename): array { $url = "/api2/v1/projects/{$projectUid}/jobs/bilingualFile"; + $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; $filecontent = $this->fetchApi->fetch('jsonPost', $url, $apiReadyArray); + if ($this->hasError($filecontent)) { + $errorMsg = $this->getError($filecontent); + throw new \Exception("Error downloading file: {$errorMsg}", 1); + } $this->_saveIntoFile($filenames[$i], $filecontent); } return $filenames; @@ -80,10 +85,17 @@ class BilingualFile extends \BrunoFontes\Memsource\BaseApi 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; } } diff --git a/src/FetchApi.php b/src/FetchApi.php index 99e4fb9..466281f 100644 --- a/src/FetchApi.php +++ b/src/FetchApi.php @@ -39,30 +39,44 @@ 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; + 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 +112,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, diff --git a/src/Jobs.php b/src/Jobs.php index 32aa8f0..3c3cda2 100644 --- a/src/Jobs.php +++ b/src/Jobs.php @@ -23,6 +23,10 @@ class Jobs extends \BrunoFontes\Memsource\BaseApi 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; } } diff --git a/src/Project.php b/src/Project.php index de2ed7a..cfd2def 100644 --- a/src/Project.php +++ b/src/Project.php @@ -21,7 +21,11 @@ class Project extends \BrunoFontes\Memsource\BaseApi */ 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; } /** @@ -33,6 +37,9 @@ class Project extends \BrunoFontes\Memsource\BaseApi */ 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); + } } } diff --git a/tests/bilingualFileTest.php b/tests/bilingualFileTest.php new file mode 100644 index 0000000..e9d50ea --- /dev/null +++ b/tests/bilingualFileTest.php @@ -0,0 +1,37 @@ +assertEquals( + [], + $api->bilingualFile()->download('uid', [], 'filename') + ); + } + public function testInvalidDownloadUidsShouldThrowError() + { + $api = new Memsource('fakeToken'); + $this->expectException(\Exception::class); + $api->bilingualFile()->download('uid', ['a'], 'filename'); + } + + 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', []); + } +} diff --git a/tests/fetchApiTest.php b/tests/fetchApiTest.php new file mode 100644 index 0000000..5d6eb91 --- /dev/null +++ b/tests/fetchApiTest.php @@ -0,0 +1,57 @@ +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'); + } +} diff --git a/tests/jobsTest.php b/tests/jobsTest.php new file mode 100644 index 0000000..ced42d1 --- /dev/null +++ b/tests/jobsTest.php @@ -0,0 +1,15 @@ +expectException(\Exception::class); + $api->jobs()->list('invalidProjectUid', []); + } +} diff --git a/tests/oauthTest.php b/tests/oauthTest.php new file mode 100644 index 0000000..4d49dac --- /dev/null +++ b/tests/oauthTest.php @@ -0,0 +1,34 @@ +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('', '', '', ''); + } +} diff --git a/tests/projectTest.php b/tests/projectTest.php new file mode 100644 index 0000000..cdb500e --- /dev/null +++ b/tests/projectTest.php @@ -0,0 +1,21 @@ +expectException(\Exception::class); + $api->project()->list(); + } + public function testInvalidProjectUidShouldThrowError() + { + $api = new Memsource('fakeToken'); + $this->expectException(\Exception::class); + $api->project()->get('invalidProjectUid'); + } +}