Skip to content

Commit

Permalink
Add Pagination/AbstractStrategy latestResponse()
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Nov 14, 2023
1 parent bbf3bd4 commit 32382c9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class AbstractStrategy
protected $resourcesKey;
protected $params;
protected $pageSize;
protected $latestResponse;

public function __construct($resourcesKey, $params)
{
Expand All @@ -21,6 +22,11 @@ public function params()
return $this->params;
}

public function latestResponse()
{
return $this->latestResponse;
}

protected function pageSize()
{
if (isset($this->pageSize)) {
Expand Down
9 changes: 5 additions & 4 deletions src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class MockResource {
public $params;
public $foundDifferent = false;
public $response;
private $resources;
private $resourceName;
private $callCount = 0;
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -187,5 +189,6 @@ public function testHandlesError()
}

$this->assertEquals($expectedErrorMessage, $actualErrorMessage);
$this->assertEquals($mockResults->response, $iterator->latestResponse());
}
}

0 comments on commit 32382c9

Please sign in to comment.