diff --git a/Api/Endpoints/Comments.php b/Api/Endpoints/Comments.php index 1c46e0c..6959f2a 100644 --- a/Api/Endpoints/Comments.php +++ b/Api/Endpoints/Comments.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Comments @@ -46,7 +46,7 @@ public function listProject(string $projectId, array $queryParams = []): Lokalis */ public function fetchAllProject(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/comments", [], @@ -89,7 +89,7 @@ public function listKey(string $projectId, int $keyId, array $queryParams = []): */ public function fetchAllKey(string $projectId, int $keyId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/keys/$keyId/comments", [], diff --git a/Api/Endpoints/Contributors.php b/Api/Endpoints/Contributors.php index 8d6502e..97d8467 100644 --- a/Api/Endpoints/Contributors.php +++ b/Api/Endpoints/Contributors.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Contributors @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/contributors", [], diff --git a/Api/Endpoints/CustomTranslationStatuses.php b/Api/Endpoints/CustomTranslationStatuses.php index 245bef0..7a0700d 100644 --- a/Api/Endpoints/CustomTranslationStatuses.php +++ b/Api/Endpoints/CustomTranslationStatuses.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class CustomTranslationStatuses @@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/custom-translation-statuses", $queryParams, diff --git a/Api/Endpoints/Endpoint.php b/Api/Endpoints/Endpoint.php index b7594e1..eb7269c 100644 --- a/Api/Endpoints/Endpoint.php +++ b/Api/Endpoints/Endpoint.php @@ -110,6 +110,7 @@ protected function request(string $requestType, string $uri, array $queryParams * * @throws LokaliseApiException * @throws LokaliseResponseException + * @deprecated Use requestAllUsingCursor or requestAllUsingPaging instead */ protected function requestAll( string $requestType, @@ -118,12 +119,35 @@ protected function requestAll( array $body = [], string $bodyResponseKey = '' ): LokaliseApiResponse { + return $this->requestAllUsingPaging($requestType, $uri, $queryParams, $body, $bodyResponseKey); + } + + /** + * @param string $requestType + * @param string $uri + * @param array $queryParams + * @param array $body + * @param string $bodyResponseKey + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + protected function requestAllUsingPaging( + string $requestType, + string $uri, + array $queryParams = [], + array $body = [], + string $bodyResponseKey = '' + ): LokaliseApiResponse + { $page = 1; $queryParams = array_merge($queryParams, ['limit' => static::FETCH_ALL_LIMIT, 'page' => $page]); $bodyData = []; $result = $this->request($requestType, $uri, $queryParams, $body); - if (is_array($result->body[$bodyResponseKey])) { + if (isset($result->body[$bodyResponseKey]) && is_array($result->body[$bodyResponseKey])) { $bodyData = $result->body[$bodyResponseKey]; } while ($result->getPageCount() > $page) { @@ -134,7 +158,7 @@ protected function requestAll( $result = $this->request($requestType, $uri, $queryParams, $body); if (is_array($result->body[$bodyResponseKey])) { - $bodyData = array_merge($result->body[$bodyResponseKey], $bodyData); + $bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]); $result->body[$bodyResponseKey] = $bodyData; } } @@ -142,6 +166,51 @@ protected function requestAll( return $result; } + + /** + * @param string $requestType + * @param string $uri + * @param array $queryParams + * @param array $body + * @param string $bodyResponseKey + * + * @return LokaliseApiResponse + * + * @throws LokaliseApiException + * @throws LokaliseResponseException + */ + protected function requestAllUsingCursor( + string $requestType, + string $uri, + array $queryParams = [], + array $body = [], + string $bodyResponseKey = '' + ): LokaliseApiResponse + { + $bodyData = []; + $cursor = ''; + $queryParams['limit'] = static::FETCH_ALL_LIMIT; + $queryParams['pagination'] = 'cursor'; + while (true) { + $queryParams['cursor'] = $cursor; + + $result = $this->request($requestType, $uri, $queryParams, $body); + + if (is_array($result->body[$bodyResponseKey]) && !empty($result->body[$bodyResponseKey])) { + $bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]); + $result->body[$bodyResponseKey] = $bodyData; + } + + if (!$result->hasNextCursor()) { + break; + } + + $cursor = $result->getNextCursor(); + } + + return $result; + } + /** * @param array $queryParams * diff --git a/Api/Endpoints/Files.php b/Api/Endpoints/Files.php index 649ac68..f50c4c8 100644 --- a/Api/Endpoints/Files.php +++ b/Api/Endpoints/Files.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Files @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/files", [], diff --git a/Api/Endpoints/Keys.php b/Api/Endpoints/Keys.php index 6a403cf..3ea0652 100644 --- a/Api/Endpoints/Keys.php +++ b/Api/Endpoints/Keys.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Keys @@ -48,7 +48,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingCursor( 'GET', "projects/$projectId/keys", $queryParams, diff --git a/Api/Endpoints/Languages.php b/Api/Endpoints/Languages.php index 4bb2176..6731b9e 100644 --- a/Api/Endpoints/Languages.php +++ b/Api/Endpoints/Languages.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Languages @@ -43,7 +43,7 @@ public function listSystem(array $queryParams = []): LokaliseApiResponse */ public function fetchAllSystem(): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "system/languages", [], @@ -84,7 +84,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/languages", [], diff --git a/Api/Endpoints/Orders.php b/Api/Endpoints/Orders.php index 881215f..1ed0b64 100644 --- a/Api/Endpoints/Orders.php +++ b/Api/Endpoints/Orders.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Orders @@ -46,7 +46,7 @@ public function list(int $teamId, array $queryParams = []): LokaliseApiResponse */ public function fetchAll(int $teamId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "teams/{$teamId}/orders", [], diff --git a/Api/Endpoints/PaymentCards.php b/Api/Endpoints/PaymentCards.php index f81c17e..02cba7f 100644 --- a/Api/Endpoints/PaymentCards.php +++ b/Api/Endpoints/PaymentCards.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Cards @@ -43,7 +43,7 @@ public function list(array $queryParams = []): LokaliseApiResponse */ public function fetchAll(): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "payment_cards", [], diff --git a/Api/Endpoints/Projects.php b/Api/Endpoints/Projects.php index 485be70..93b1655 100644 --- a/Api/Endpoints/Projects.php +++ b/Api/Endpoints/Projects.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Projects @@ -45,7 +45,7 @@ public function list(array $queryParams = []): LokaliseApiResponse */ public function fetchAll(array $queryParams = []): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects", $queryParams, diff --git a/Api/Endpoints/Screenshots.php b/Api/Endpoints/Screenshots.php index 806cae6..0afa8ca 100644 --- a/Api/Endpoints/Screenshots.php +++ b/Api/Endpoints/Screenshots.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Screenshots @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/screenshots", [], diff --git a/Api/Endpoints/Snapshots.php b/Api/Endpoints/Snapshots.php index c33e865..f82b8ba 100644 --- a/Api/Endpoints/Snapshots.php +++ b/Api/Endpoints/Snapshots.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Snapshots @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/snapshots", [], diff --git a/Api/Endpoints/Tasks.php b/Api/Endpoints/Tasks.php index abd9bcd..bd5888b 100644 --- a/Api/Endpoints/Tasks.php +++ b/Api/Endpoints/Tasks.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Tasks @@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/tasks", $queryParams, diff --git a/Api/Endpoints/TeamUserGroups.php b/Api/Endpoints/TeamUserGroups.php index 9a15a92..2fec44d 100644 --- a/Api/Endpoints/TeamUserGroups.php +++ b/Api/Endpoints/TeamUserGroups.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class TeamUserGroups @@ -46,7 +46,7 @@ public function list(int $teamId, array $queryParams = []): LokaliseApiResponse */ public function fetchAll(int $teamId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "teams/$teamId/groups", [], diff --git a/Api/Endpoints/TeamUsers.php b/Api/Endpoints/TeamUsers.php index aa36904..9060539 100644 --- a/Api/Endpoints/TeamUsers.php +++ b/Api/Endpoints/TeamUsers.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class TeamUsers @@ -46,7 +46,7 @@ public function list(int $teamId, array $queryParams = []): LokaliseApiResponse */ public function fetchAll(int $teamId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "teams/$teamId/users", [], diff --git a/Api/Endpoints/Teams.php b/Api/Endpoints/Teams.php index d6cdb95..2454634 100644 --- a/Api/Endpoints/Teams.php +++ b/Api/Endpoints/Teams.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Teams @@ -43,7 +43,7 @@ public function list(array $queryParams = []): LokaliseApiResponse */ public function fetchAll(): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "teams", [], diff --git a/Api/Endpoints/TranslationProviders.php b/Api/Endpoints/TranslationProviders.php index eea6998..da35368 100644 --- a/Api/Endpoints/TranslationProviders.php +++ b/Api/Endpoints/TranslationProviders.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Providers @@ -46,7 +46,7 @@ public function list(int $teamId, array $queryParams = []): LokaliseApiResponse */ public function fetchAll(int $teamId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "teams/{$teamId}/translation_providers", [], diff --git a/Api/Endpoints/Translations.php b/Api/Endpoints/Translations.php index 17c4259..6b84f6a 100644 --- a/Api/Endpoints/Translations.php +++ b/Api/Endpoints/Translations.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Translations @@ -46,7 +46,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingCursor( 'GET', "projects/$projectId/translations", [], diff --git a/Api/Endpoints/Webhooks.php b/Api/Endpoints/Webhooks.php index 5178c94..bb8ebe7 100644 --- a/Api/Endpoints/Webhooks.php +++ b/Api/Endpoints/Webhooks.php @@ -2,9 +2,9 @@ namespace Lokalise\Endpoints; -use \Lokalise\LokaliseApiResponse; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse; /** * Class Webhooks @@ -47,7 +47,7 @@ public function list(string $projectId, array $queryParams = []): LokaliseApiRes */ public function fetchAll(string $projectId, array $queryParams = []): LokaliseApiResponse { - return $this->requestAll( + return $this->requestAllUsingPaging( 'GET', "projects/$projectId/webhooks", $queryParams, diff --git a/Api/LokaliseApiResponse.php b/Api/LokaliseApiResponse.php index 356558f..0a5c0ca 100644 --- a/Api/LokaliseApiResponse.php +++ b/Api/LokaliseApiResponse.php @@ -4,7 +4,7 @@ use Exception; use JsonException; -use \Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ResponseInterface; class LokaliseApiResponse { @@ -12,6 +12,7 @@ class LokaliseApiResponse public const HEADER_PAGINATION_PAGES = "X-Pagination-Page-Count"; public const HEADER_PAGINATION_LIMIT = "X-Pagination-Limit"; public const HEADER_PAGINATION_PAGE = "X-Pagination-Page"; + public const HEADER_PAGINATION_NEXT_CURSOR = "X-Pagination-Next-Cursor"; public array $headers; public ?array $body; @@ -31,9 +32,12 @@ public function __construct(ResponseInterface $guzzleResponse) ]; foreach ($headers as $header) { - $this->headers[$header] = (int)$guzzleResponse->getHeaderLine($header); + $this->headers[$header] = $guzzleResponse->getHeaderLine($header); } + $this->headers[self::HEADER_PAGINATION_NEXT_CURSOR] = + $guzzleResponse->getHeaderLine(self::HEADER_PAGINATION_NEXT_CURSOR); + try { $this->body = json_decode($guzzleResponse->getBody(), true, 512, JSON_THROW_ON_ERROR); } catch (Exception $e) { @@ -100,6 +104,16 @@ public function getPaginationPage(): ?int return $this->getPaginationHeader(self::HEADER_PAGINATION_PAGE); } + public function getNextCursor(): ?string + { + return $this->headers[self::HEADER_PAGINATION_NEXT_CURSOR]; + } + + public function hasNextCursor(): bool + { + return !empty($this->headers[self::HEADER_PAGINATION_NEXT_CURSOR]); + } + /** * @param string $header * diff --git a/README.md b/README.md index f93dd99..1ba2474 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,8 @@ $response-> getPageCount() // Return count of pages in List methods (based on limit parameter) getPaginationLimit() // Return pagination limit used in the request getPaginationPage() // Return current page of the request + getNextCursor() // Return next cursor for cursor based pagination + hasNextCursor() // Return true if next cursor is present for cursor based pagination ``` ## Utils diff --git a/Tests/Endpoints/EndpointTest.php b/Tests/Endpoints/EndpointTest.php index 26e8f2d..9441e32 100644 --- a/Tests/Endpoints/EndpointTest.php +++ b/Tests/Endpoints/EndpointTest.php @@ -3,30 +3,19 @@ namespace Lokalise\Tests\Endpoints; -use \PHPUnit\Framework\TestCase; -use \Lokalise\LokaliseApiResponse as ApiResponse; -use \Lokalise\Exceptions\LokaliseResponseException; -use \Lokalise\Exceptions\LokaliseApiException; -use \Lokalise\Endpoints\Endpoint; -use \GuzzleHttp\Client; -use \GuzzleHttp\Handler\MockHandler; -use \GuzzleHttp\HandlerStack; -use \GuzzleHttp\Psr7\Response; +use GuzzleHttp\Client; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Response; +use Lokalise\Endpoints\Endpoint; +use Lokalise\Exceptions\LokaliseApiException; +use Lokalise\Exceptions\LokaliseResponseException; +use Lokalise\LokaliseApiResponse as ApiResponse; +use PHPUnit\Framework\TestCase; use ReflectionClass; final class EndpointTest extends TestCase { - protected ?Endpoint $endpoint = null; - - protected function setUp(): void - { - $this->endpoint = new Endpoint('https://api.lokalise.com/api2', '{Test_Api_Token}'); - } - - protected function tearDown(): void - { - $this->endpoint = null; - } private function callPrivateMethod($class, string $name, array $arguments = []) { @@ -54,6 +43,7 @@ private function getMockClient($status = 200, $headers = [], $body = null): Clie public function testQueryParamsToQueryString(): void { + $endpoint = new Endpoint('https://api.lokalise.com/api2', '{Test_Api_Token}'); $params = [ 'param1' => 2, 'param2' => 'exist', @@ -62,7 +52,7 @@ public function testQueryParamsToQueryString(): void 'second' => 'World', ], ]; - $result = $this->callPrivateMethod($this->endpoint, 'queryParamsToQueryString', [$params]); + $result = $this->callPrivateMethod($endpoint, 'queryParamsToQueryString', [$params]); self::assertEquals( 'param1=2¶m2=exist&array%5Bfirst%5D=Hello&array%5Bsecond%5D=World', @@ -75,6 +65,7 @@ public function testQueryParamsToQueryString(): void */ public function testFixArraysInQueryParams(): void { + $endpoint = new Endpoint('https://api.lokalise.com/api2', '{Test_Api_Token}'); $params = [ 'param1' => 'single', 'param2' => [ @@ -86,7 +77,7 @@ public function testFixArraysInQueryParams(): void 'second', ], ]; - $result = $this->callPrivateMethod($this->endpoint, 'fixArraysInQueryParams', [$params]); + $result = $this->callPrivateMethod($endpoint, 'fixArraysInQueryParams', [$params]); self::assertEquals( [ @@ -100,6 +91,7 @@ public function testFixArraysInQueryParams(): void public function testRequest(): void { + $endpoint = new Endpoint('https://api.lokalise.com/api2', '{Test_Api_Token}'); $client = $this->getMockClient( $status = 200, $headers = [ @@ -107,15 +99,16 @@ public function testRequest(): void 'X-Pagination-Page-Count' => 2, 'X-Pagination-Limit' => 10, 'X-Pagination-Page' => 1, + 'X-Pagination-Next-Cursor' => '', ], $body = [ 'response' => 'success', ] ); - $this->callPrivateMethod($this->endpoint, 'setClient', [$client]); + $this->callPrivateMethod($endpoint, 'setClient', [$client]); /** @var ApiResponse $apiResponse */ - $apiResponse = $this->callPrivateMethod($this->endpoint, 'request', ['GET', '']); + $apiResponse = $this->callPrivateMethod($endpoint, 'request', ['GET', '']); self::assertInstanceOf(ApiResponse::class, $apiResponse); self::assertEquals($headers, $apiResponse->headers); @@ -124,16 +117,18 @@ public function testRequest(): void public function testRequestApiException(): void { + $endpoint = new Endpoint('https://api.lokalise.com/api2', '{Test_Api_Token}'); $this->expectException(LokaliseApiException::class); $this->expectExceptionCode(404); $client = $this->getMockClient(404); - $this->callPrivateMethod($this->endpoint, 'setClient', [$client]); - $this->callPrivateMethod($this->endpoint, 'request', ['GET', '']); + $this->callPrivateMethod($endpoint, 'setClient', [$client]); + $this->callPrivateMethod($endpoint, 'request', ['GET', '']); } public function testRequestResponseException(): void { + $endpoint = new Endpoint('https://api.lokalise.com/api2', '{Test_Api_Token}'); $this->expectException(LokaliseResponseException::class); $this->expectExceptionMessage('test'); $this->expectExceptionCode(401); @@ -141,7 +136,153 @@ public function testRequestResponseException(): void $body = ['error' => ['code' => 401, 'message' => 'test']]; $client = $this->getMockClient(500, [], $body); - $this->callPrivateMethod($this->endpoint, 'setClient', [$client]); - $this->callPrivateMethod($this->endpoint, 'request', ['GET', '']); + $this->callPrivateMethod($endpoint, 'setClient', [$client]); + $this->callPrivateMethod($endpoint, 'request', ['GET', '']); + } + + public function testRequestAll(): void + { + $endpoint = new class('https://api.lokalise.com/api2', '{Test_Api_Token}') extends Endpoint { + public function fetchAll(): ApiResponse + { + return $this->requestAll('GET', '', [], [], 'mockedResults'); + } + }; + $client = new Client([ + 'handler' => HandlerStack::create( + new MockHandler([ + new Response( + 200, + [ + "X-Pagination-Total-Count" => 14, + "X-Pagination-Limit" => 10, + "X-Pagination-Page" => 1, + "X-Pagination-Page-Count" => 2, + ], + json_encode(['mockedResults' => [["id" => "element from page 1"]]], JSON_THROW_ON_ERROR) + ), + new Response( + 200, + [ + "X-Pagination-Total-Count" => 14, + "X-Pagination-Limit" => 10, + "X-Pagination-Page" => 2, + "X-Pagination-Page-Count" => 2, + ], + json_encode(['mockedResults' => [["id" => "element from page 2"]]], JSON_THROW_ON_ERROR) + ), + ]) + ), + ]); + + $endpoint->setClient($client); + + $apiResponse = $endpoint->fetchAll(); + + self::assertEquals([ + "mockedResults" => [ + ["id" => "element from page 1"], + ["id" => "element from page 2"], + ] + ], $apiResponse->getContent()); + } + + public function testRequestAllUsingPaging(): void + { + $endpoint = new class('https://api.lokalise.com/api2', '{Test_Api_Token}') extends Endpoint { + public function fetchAll(): ApiResponse + { + return $this->requestAllUsingPaging('GET', '', [], [], 'mockedResults'); + } + }; + $client = new Client([ + 'handler' => HandlerStack::create( + new MockHandler([ + new Response( + 200, + [ + "X-Pagination-Total-Count" => 14, + "X-Pagination-Limit" => 10, + "X-Pagination-Page" => 1, + "X-Pagination-Page-Count" => 2, + ], + json_encode(['mockedResults' => [["id" => "element from page 1 using paging"]]], JSON_THROW_ON_ERROR) + ), + new Response( + 200, + [ + "X-Pagination-Total-Count" => 14, + "X-Pagination-Limit" => 10, + "X-Pagination-Page" => 2, + "X-Pagination-Page-Count" => 2, + ], + json_encode(['mockedResults' => [["id" => "element from page 2 using paging"]]], JSON_THROW_ON_ERROR) + ), + ]) + ), + ]); + + $endpoint->setClient($client); + + $apiResponse = $endpoint->fetchAll(); + + self::assertEquals([ + "mockedResults" => [ + ["id" => "element from page 1 using paging"], + ["id" => "element from page 2 using paging"], + ] + ], $apiResponse->getContent()); + } + + public function testRequestAllUsingCursor(): void + { + $endpoint = new class('https://api.lokalise.com/api2', '{Test_Api_Token}') extends Endpoint { + public function fetchAll(): ApiResponse + { + return $this->requestAllUsingCursor('GET', '', [], [], 'mockedResults'); + } + }; + $client = new Client([ + 'handler' => HandlerStack::create( + new MockHandler([ + new Response( + 200, + [ + "X-Pagination-Next-Cursor" => "cursor_list_1", + "X-Pagination-Limit" => 10, + ], + json_encode(['mockedResults' => [["id" => "element from cursor 1 using cursor"]]], JSON_THROW_ON_ERROR), + ), + new Response( + 200, + [ + "X-Pagination-Next-Cursor" => "cursor_list_2", + "X-Pagination-Limit" => 10, + ], + json_encode(['mockedResults' => [["id" => "element from cursor 2 using cursor"]]], JSON_THROW_ON_ERROR) + ), + new Response( + 200, + [ + "X-Pagination-Next-Cursor" => "", + "X-Pagination-Limit" => 10, + ], + json_encode(['mockedResults' => [["id" => "element from cursor 3 using cursor"]]], JSON_THROW_ON_ERROR) + ), + ]) + ), + ]); + + $endpoint->setClient($client); + + $apiResponse = $endpoint->fetchAll(); + + self::assertEquals([ + "mockedResults" => [ + ["id" => "element from cursor 1 using cursor"], + ["id" => "element from cursor 2 using cursor"], + ["id" => "element from cursor 3 using cursor"], + ] + ], $apiResponse->getContent()); } } diff --git a/Tests/Endpoints/MockEndpointTrait.php b/Tests/Endpoints/MockEndpointTrait.php index dd3c877..b2cd1dd 100644 --- a/Tests/Endpoints/MockEndpointTrait.php +++ b/Tests/Endpoints/MockEndpointTrait.php @@ -15,7 +15,7 @@ private function createEndpointMock(string $endpointClassName): Endpoint ->getMockBuilder($endpointClassName) ->disableOriginalConstructor() ->setConstructorArgs([null, '{Test_Api_Token}']) - ->onlyMethods(['request', 'requestAll']) + ->onlyMethods(['request', 'requestAllUsingPaging', 'requestAllUsingCursor']) ->getMock(); $mock->method('request')->willReturnCallback( @@ -36,7 +36,27 @@ function (string $requestType, string $uri, array $queryParams = [], array $body } ); - $mock->method('requestAll')->willReturnCallback( + $mock->method('requestAllUsingPaging')->willReturnCallback( + function (string $requestType, string $uri, array $queryParams = [], array $body = [], string $bodyResponseKey = ''): LokaliseApiResponse { + $lokaliseApiResponse = new LokaliseApiResponse($this->createMock(ResponseInterface::class)); + + $reflection = new ReflectionClass($lokaliseApiResponse); + $property = $reflection->getProperty('body'); + $property->setAccessible(true); + + $property->setValue($lokaliseApiResponse, [ + 'requestType' => $requestType, + 'uri' => $uri, + 'queryParams' => $queryParams, + 'body' => $body, + 'bodyResponseKey' => $bodyResponseKey, + ]); + + return $lokaliseApiResponse; + } + ); + + $mock->method('requestAllUsingCursor')->willReturnCallback( function (string $requestType, string $uri, array $queryParams = [], array $body = [], string $bodyResponseKey = ''): LokaliseApiResponse { $lokaliseApiResponse = new LokaliseApiResponse($this->createMock(ResponseInterface::class)); diff --git a/Tests/LokaliseApiResponseTest.php b/Tests/LokaliseApiResponseTest.php index 6034236..a9267d5 100644 --- a/Tests/LokaliseApiResponseTest.php +++ b/Tests/LokaliseApiResponseTest.php @@ -2,15 +2,15 @@ namespace Lokalise\Tests; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Response; use JsonException; -use \PHPUnit\Framework\TestCase; -use \Lokalise\LokaliseApiResponse as ApiResponse; -use \GuzzleHttp\Client; -use \GuzzleHttp\Handler\MockHandler; -use \GuzzleHttp\HandlerStack; -use \GuzzleHttp\Psr7\Response; -use \GuzzleHttp\Exception\RequestException; -use \GuzzleHttp\Exception\GuzzleException; +use Lokalise\LokaliseApiResponse as ApiResponse; +use PHPUnit\Framework\TestCase; final class LokaliseApiResponseTest extends TestCase { @@ -24,6 +24,7 @@ protected function setUp(): void 'X-Pagination-Page-Count' => 2, 'X-Pagination-Limit' => 10, 'X-Pagination-Page' => 1, + 'X-Pagination-Next-Cursor' => '' ]; }