From 32382c9d18514a9c65eeb6c1da312b3cda23325c Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Tue, 14 Nov 2023 13:54:12 +1000 Subject: [PATCH] Add Pagination/AbstractStrategy latestResponse() --- README.md | 4 ++++ docker-compose.yml | 1 + .../Traits/Utility/Pagination/AbstractStrategy.php | 6 ++++++ .../API/Traits/Utility/Pagination/CbpStrategy.php | 9 +++++---- .../Utility/Pagination/PaginationIterator.php | 4 ++++ .../Traits/Utility/PaginationIteratorTest.php | 13 ++++++++----- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e75cea4c..d5c710e9 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,10 @@ foreach ($iterator as $ticket) { * Refer to the docs for details, including allowed sort fields * Combine everything: `$params = ['page[size]' => 2, 'sort' => 'updated_at', 'extra' => 'param'];` +##### Iterator API call response + +The latest response is exposed in the iterator at `$iterator->latestResponse()`. This could come handy for debugging. + ##### Custom iterators If you want to use the iterator for custom methods, as opposed to the default `findAll()`, you can create an iterator for your collection: diff --git a/docker-compose.yml b/docker-compose.yml index 1d1ef183..0f2ee2bc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,7 @@ services: - .:/app - vendor:/app/vendor command: vendor/bin/phpunit --testsuite "Zendesk API Unit Test Suites" + # command: vendor/bin/phpunit --testdox --testsuite "Zendesk API Unit Test Suites" # command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php # command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/TicketsTest.php # command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php diff --git a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php index b766d777..227a9d24 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -9,6 +9,7 @@ abstract class AbstractStrategy protected $resourcesKey; protected $params; protected $pageSize; + protected $latestResponse; public function __construct($resourcesKey, $params) { @@ -21,6 +22,11 @@ public function params() return $this->params; } + public function latestResponse() + { + return $this->latestResponse; + } + protected function pageSize() { if (isset($this->pageSize)) { diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index 92bfa538..77b95b06 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -8,16 +8,16 @@ */ class CbpStrategy extends AbstractStrategy { - private $afterCursor = null; + private $afterCursor; private $started = false; public function page($getPageFn) { $this->started = true; - $response = $getPageFn(); - $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; + $this->latestResponse = $getPageFn(); + $this->afterCursor = $this->latestResponse->meta->has_more ? $this->latestResponse->meta->after_cursor : null; - return $response->{$this->resourcesKey}; + return $this->latestResponse->{$this->resourcesKey}; } public function shouldGetPage($position) { @@ -31,6 +31,7 @@ public function params() return $result; } + /** * The params that are needed to ordering in CBP (eg: ["sort" => "-age"]) * If OBP params are passed, they are converted to CBP diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php index b9e47608..4f6ccd7e 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -55,6 +55,10 @@ public function current() } } + public function latestResponse() + { + return $this->strategy->latestResponse(); + } private function getPageIfNeeded() { if (isset($this->page[$this->position]) || !$this->strategy->shouldGetPage($this->position)) { diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php index 5fed9b21..be3471af 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php @@ -14,6 +14,7 @@ class MockResource { public $params; public $foundDifferent = false; + public $response; private $resources; private $resourceName; private $callCount = 0; @@ -31,8 +32,8 @@ public function findAll($params) { if ($this->errorMessage) { $request = new Request('GET', 'http://example.zendesk.com'); - $response = new Response(400, [], '{ "a": "json"}'); - $requestException = new RequestException($this->errorMessage, $request, $response); + $this->response = new Response(400, [], '{ "a": "json"}'); + $requestException = new RequestException($this->errorMessage, $request, $this->response); throw new ApiResponseException($requestException); } // Simulate two pages of resources @@ -44,16 +45,16 @@ public function findAll($params) $afterCursor = $this->callCount === 0 ? 'cursor_for_next_page' : null; $this->callCount++; - $this->params = $params; - - return (object) [ + $this->response = (object) [ $this->resourceName => $resources, 'meta' => (object) [ 'has_more' => $afterCursor !== null, 'after_cursor' => $afterCursor, ], ]; + + return $this->response; } public function findDifferent($params) @@ -77,6 +78,7 @@ public function testFetchesTickets() $tickets = iterator_to_array($iterator); $this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], $tickets); + $this->assertEquals($mockTickets->response, $iterator->latestResponse()); } public function testFetchesUsers() @@ -187,5 +189,6 @@ public function testHandlesError() } $this->assertEquals($expectedErrorMessage, $actualErrorMessage); + $this->assertEquals($mockResults->response, $iterator->latestResponse()); } }