Skip to content

Commit

Permalink
PaginationIterator storing only one page at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Nov 17, 2023
1 parent 2ce6e7f commit b978d74
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 16 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 @@ -81,7 +81,7 @@ private function getPageIfNeeded()
$getPageFn = function () {
return $this->clientList->{$this->method}($this->strategy->params());
};

$this->items = array_merge($this->items, $this->strategy->page($getPageFn));
$this->items = $this->strategy->page($getPageFn);
$this->position = 0;
}
}
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 b978d74

Please sign in to comment.