Skip to content

Commit

Permalink
Merge pull request #524 from zendesk/RED-1955-iterator-page
Browse files Browse the repository at this point in the history
PaginationIterator storing only one page at a time
  • Loading branch information
ecoologic committed Nov 17, 2023
2 parents 2ce6e7f + ef32bb4 commit d1f7c81
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 25 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +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'];`

**Note**: Refer to the documentation for the correct params for sorting with the pagination type you're using.
**Note**:

* Refer to the documentation for the correct params for sorting with the pagination type you're using
* The helper method `iterator_to_array` doesn't work with this implementation

##### Iterator API call response

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ protected function pageSize()
}

abstract public function page($getPageFn);
abstract public function shouldGetPage($position);
abstract public function shouldGetPage($current_page);
}
2 changes: 1 addition & 1 deletion src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function page($getPageFn)
return $this->latestResponse->{$this->resourcesKey};
}

public function shouldGetPage($position) {
public function shouldGetPage($current_page) {
return !$this->started || $this->hasMore;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function page($getPageFn)
return $response->{$this->resourcesKey};
}

public function shouldGetPage($position) {
return $this->pageNumber == 0 || $position >= $this->pageNumber * $this->pageSize();
public function shouldGetPage($current_page) {
return $this->pageNumber == 0 || count($current_page) == 0;
}
}
12 changes: 6 additions & 6 deletions src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PaginationIterator implements Iterator
private $strategy;
private $method;
private $position = 0;
private $items = [];
private $page = [];

/**
* @param mixed using trait FindAll. The resources collection, Eg: `$client->tickets()` which uses FindAll
Expand Down Expand Up @@ -56,8 +56,8 @@ public function valid()
#[\ReturnTypeWillChange]
public function current()
{
if (isset($this->items[$this->position])) {
return $this->items[$this->position];
if (isset($this->page[$this->position])) {
return $this->page[$this->position];
} else {
return null;
}
Expand All @@ -74,14 +74,14 @@ public function latestResponse()
}
private function getPageIfNeeded()
{
if (isset($this->items[$this->position]) || !$this->strategy->shouldGetPage($this->position)) {
if (isset($this->page[$this->position]) || !$this->strategy->shouldGetPage($this->page)) {
return;
}

$getPageFn = function () {
return $this->clientList->{$this->method}($this->strategy->params());
};

$this->items = array_merge($this->items, $this->strategy->page($getPageFn));
$this->page = $this->strategy->page($getPageFn);
$this->position = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function page($getPageFn)
return $response->{$this->resourcesKey};
}

public function shouldGetPage($position) {
public function shouldGetPage($current_page) {
return !$this->started;
}
}
16 changes: 16 additions & 0 deletions tests/Zendesk/API/UnitTests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,20 @@ protected function assertEndpointCalled($userFunction, $endpoint, $method = 'GET
])
);
}

/**
* replacement for iterator_to_array
* which doesn't work when storing with single page
*
* @param \Iterator $iterator
* @return array all the items
*/
protected function iterator_to_array($iterator)
{
$results = [];
foreach ($iterator as $item) {
$results[] = $item;
}
return $results;
}
}
4 changes: 2 additions & 2 deletions tests/Zendesk/API/UnitTests/Core/AutomationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testIterator()

$iterator = $this->client->automations()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
Expand All @@ -65,7 +65,7 @@ public function testIteratorFindActive()

$iterator = $this->client->automations()->iterator([], 'findActive');

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);

$this->assertLastRequestIs(
[
Expand Down
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/Core/MacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testIterator()

$iterator = $this->client->macros()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testIterator()

$iterator = $this->client->organizationMemberships()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
Expand Down
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/Core/OrganizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testIterator()

$iterator = $this->client->organizations()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
Expand Down
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/Core/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testIterator()

$iterator = $this->client->requests()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testIterator()

$iterator = $this->client->satisfactionRatings()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
Expand Down
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/Core/TagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testIterator()
]);
$iterator = $this->client->tags()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);

$this->assertCount(3, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
Expand Down
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/Core/TicketsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testIteratorToArray()

$iterator = $this->client->tickets()->iterator();

$actual = iterator_to_array($iterator);
$actual = $this->iterator_to_array($iterator);
$this->assertCount(2, $actual);
$this->assertEquals($this->testTicket['subject'], $actual[0]->subject);
$this->assertEquals($this->testTicket2['subject'], $actual[1]->subject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ public function testFetchesTickets()
$strategy = new CbpStrategy('tickets', ['page[size]' => 2]);
$iterator = new PaginationIterator($mockTickets, $strategy, 'findAll');

$tickets = $this->iterator_to_array($iterator);

$this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], $tickets);
$this->assertEquals($mockTickets->response, $iterator->latestResponse());
}

public function testFetchesTicketsIteratorToArray()
{
$this->markTestSkipped("Doesn't work unless you store all pages in the iterator");
$mockTickets = new MockResource('tickets', [
[['id' => 1], ['id' => 2]],
[['id' => 3], ['id' => 4]]
]);
$strategy = new CbpStrategy('tickets', ['page[size]' => 2]);
$iterator = new PaginationIterator($mockTickets, $strategy, 'findAll');

$tickets = iterator_to_array($iterator);

$this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], $tickets);
Expand All @@ -97,7 +113,7 @@ public function testFetchesUsers()
$strategy = new CbpStrategy('users', ['page[size]' => 2]);
$iterator = new PaginationIterator($mockUsers, $strategy, 'findAll');

$users = iterator_to_array($iterator);
$users = $this->iterator_to_array($iterator);

$this->assertEquals([
['id' => 1, 'name' => 'User 1'],
Expand All @@ -116,7 +132,7 @@ public function testFetchesCbpWithParams()
$strategy = new CbpStrategy('tickets', ['page[size]' => 2, 'any' => 'param']);
$iterator = new PaginationIterator($mockTickets, $strategy, 'findAll');

$tickets = iterator_to_array($iterator);
$tickets = $this->iterator_to_array($iterator);

$this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], $tickets);
$this->assertEquals([
Expand All @@ -135,7 +151,7 @@ public function testFetchesSinglePageWithParams()
$strategy = new SinglePageStrategy($resultsKey, $userParams);
$iterator = new PaginationIterator($mockResults, $strategy, 'findAll');

$resources = iterator_to_array($iterator);
$resources = $this->iterator_to_array($iterator);

$this->assertEquals([
['id' => 1, 'name' => 'Resource 1'],
Expand All @@ -153,7 +169,7 @@ public function testCustomMethod()
$strategy = new SinglePageStrategy($resultsKey, $userParams);
$iterator = new PaginationIterator($mockResults, $strategy, 'findDifferent');

$resources = iterator_to_array($iterator);
$resources = $this->iterator_to_array($iterator);

$this->assertEquals([
['id' => 1, 'name' => 'Resource 1'],
Expand Down

0 comments on commit d1f7c81

Please sign in to comment.