Skip to content

Commit

Permalink
Add PaginationIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Oct 31, 2023
1 parent eaab3b2 commit d55dcf3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ class PaginationIterator implements Iterator
private $position = 0;
private $page = [];
private $strategy;
private $params;

/**
* @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()`
*/
private $clientList;

public function __construct($clientList, AbstractStrategy $strategy)
public function __construct($clientList, AbstractStrategy $strategy, $params = [])
{
$this->clientList = $clientList;
$this->strategy = $strategy;
$this->params = $params;
}

public function key()
Expand Down Expand Up @@ -56,8 +59,8 @@ private function getPageIfNeeded()
return;
}

$pageFn = function ($params = []) {
return $this->clientList->findAll($params);
$pageFn = function ($paginationParams = []) {
return $this->clientList->findAll(array_merge($this->params, $paginationParams));
};

$this->page = array_merge($this->page, $this->strategy->getPage($pageFn));
Expand Down
42 changes: 42 additions & 0 deletions tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Zendesk\API\UnitTests\Core;

use Zendesk\API\Traits\Utility\Pagination\CbpStrategy;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;
use Zendesk\API\UnitTests\BasicTest;
use Zendesk\API\Traits\Utility\Pagination\PaginationIterator;

class MockResource {
public $params;
private $resources;
private $resourceName;
private $callCount = 0;
Expand All @@ -30,6 +32,8 @@ public function findAll($params)

$this->callCount++;

$this->params = $params;

return (object) [
$this->resourceName => $resources,
'meta' => (object) [
Expand Down Expand Up @@ -74,4 +78,42 @@ public function testFetchesUsers()
['id' => 4, 'name' => 'User 4']
], $users);
}

public function testFetchesCbpWithParams()
{
$mockTickets = new MockResource('tickets', [
[['id' => 1], ['id' => 2]],
[['id' => 3], ['id' => 4]]
]);
$strategy = new CbpStrategy('tickets', 2);
$iterator = new PaginationIterator($mockTickets, $strategy, ['sort_name' => 'id', 'sort_order' => 'desc']);

$tickets = iterator_to_array($iterator);

$this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], $tickets);
$this->assertEquals(
$mockTickets->params,
['sort_name' => 'id', 'sort_order' => 'desc',
'page[size]' => 2, 'page[after]' => 'cursor_for_next_page']);
}

public function testFetchesSinglePageWithParams()
{
$resultsKey = 'results';
$userParams = ['param' => 1];
$mockResults = new MockResource($resultsKey, [
[['id' => 1, 'name' => 'Resource 1'], ['id' => 2, 'name' => 'Resource 2']]
]);
$strategy = new SinglePageStrategy($resultsKey);
$iterator = new PaginationIterator($mockResults, $strategy, $userParams);

$resources = iterator_to_array($iterator);

$this->assertEquals([
['id' => 1, 'name' => 'Resource 1'],
['id' => 2, 'name' => 'Resource 2'],
], $resources);
$this->assertEquals($mockResults->params, $userParams);
}

}

0 comments on commit d55dcf3

Please sign in to comment.