Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DON'T MERGE] Iterator keeping only one page in memory #520

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ services:
volumes:
- .:/app
- vendor:/app/vendor
command: vendor/bin/phpunit --testsuite "Zendesk API Unit Test Suites"
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php
# command: vendor/bin/phpunit --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
8 changes: 7 additions & 1 deletion src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public function page($getPageFn)
}

public function shouldGetPage($position) {
return !$this->started || $this->afterCursor;
print("!!!!!!!!!!!! src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php:24 shouldGetPage\n");
print("started :"); print_r($this->started); print(" \n");
print("afterCursor :"); print_r($this->afterCursor); print(" \n");
print("position :"); print_r($position); print(" \n");
print("page size :"); print_r($this->pageSize()); print(" \n");
print("result :"); print_r(!$this->started || !$this->afterCursor && $position == $this->pageSize() ? "true" : "false"); print(" \n");
return !$this->started || $this->afterCursor && $position == $this->pageSize();
}

public function params()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ private function getPageIfNeeded()
if (!$this->strategy->shouldGetPage($this->position)) {
return;
}
print("!!!!!!!!!!!! src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php:63 getPage\n");
print_r($this->position); print(" \n");

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

$this->page = array_merge($this->page, $this->strategy->page($getPageFn));
$this->page = $this->strategy->page($getPageFn);
$this->position = 0;
}
}
228 changes: 119 additions & 109 deletions tests/Zendesk/API/UnitTests/Traits/Utility/PaginationIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,118 +74,128 @@ public function testFetchesTickets()
$strategy = new CbpStrategy('tickets', ['page[size]' => 2]);
$iterator = new PaginationIterator($mockTickets, $strategy);

$tickets = iterator_to_array($iterator);

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

public function testFetchesUsers()
{
$mockUsers = new MockResource('users', [
[['id' => 1, 'name' => 'User 1'], ['id' => 2, 'name' => 'User 2']],
[['id' => 3, 'name' => 'User 3'], ['id' => 4, 'name' => 'User 4']]
]);
$strategy = new CbpStrategy('users', ['page[size]' => 2]);
$iterator = new PaginationIterator($mockUsers, $strategy);

$users = iterator_to_array($iterator);

$this->assertEquals([
['id' => 1, 'name' => 'User 1'],
['id' => 2, 'name' => 'User 2'],
['id' => 3, 'name' => 'User 3'],
['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', ['page[size]' => 2, 'any' => 'param']);
$iterator = new PaginationIterator($mockTickets, $strategy);
// WORKS
$tickets = [];
foreach ($iterator as $ticket) {
print("!!!!!!!!!!!! LOOP \n");
print_r($tickets); print(" \n");
$tickets[] = $ticket;
}

$tickets = iterator_to_array($iterator);
// DOESN'T WORK
// $tickets = iterator_to_array($iterator);

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

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

iterator_to_array($iterator);

$this->assertEquals([
'sort' => '-id',
'page[size]' => 2, 'page[after]' => 'cursor_for_next_page'
], $mockTickets->params);
}

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, $userParams);
$iterator = new PaginationIterator($mockResults, $strategy);

$resources = iterator_to_array($iterator);

$this->assertEquals([
['id' => 1, 'name' => 'Resource 1'],
['id' => 2, 'name' => 'Resource 2'],
], $resources);
$this->assertEquals($mockResults->params, $userParams);
}
public function testCustomMethod()
{
$resultsKey = 'results';
$userParams = ['param' => 1];
$mockResults = new MockResource($resultsKey, [
[['id' => 1, 'name' => 'Resource 1'], ['id' => 2, 'name' => 'Resource 2']]
]);
$strategy = new SinglePageStrategy($resultsKey, $userParams);
$iterator = new PaginationIterator($mockResults, $strategy, 'findDifferent');

$resources = iterator_to_array($iterator);

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

public function testHandlesError()
{
$expectedErrorMessage = "BOOM!";
$resultsKey = 'results';
$userParams = [];
$mockResults = new MockResource($resultsKey, [], $expectedErrorMessage);
$strategy = new CbpStrategy($resultsKey, $userParams);
$iterator = new PaginationIterator($mockResults, $strategy);

try {
iterator_to_array($iterator);
} catch (ApiResponseException $e) {
$actualErrorMessage = $e->getMessage();
}

$this->assertEquals($expectedErrorMessage, $actualErrorMessage);
}
// public function testFetchesUsers()
// {
// $mockUsers = new MockResource('users', [
// [['id' => 1, 'name' => 'User 1'], ['id' => 2, 'name' => 'User 2']],
// [['id' => 3, 'name' => 'User 3'], ['id' => 4, 'name' => 'User 4']]
// ]);
// $strategy = new CbpStrategy('users', ['page[size]' => 2]);
// $iterator = new PaginationIterator($mockUsers, $strategy);

// $users = iterator_to_array($iterator);

// $this->assertEquals([
// ['id' => 1, 'name' => 'User 1'],
// ['id' => 2, 'name' => 'User 2'],
// ['id' => 3, 'name' => 'User 3'],
// ['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', ['page[size]' => 2, 'any' => 'param']);
// $iterator = new PaginationIterator($mockTickets, $strategy);

// $tickets = iterator_to_array($iterator);

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

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

// iterator_to_array($iterator);

// $this->assertEquals([
// 'sort' => '-id',
// 'page[size]' => 2, 'page[after]' => 'cursor_for_next_page'
// ], $mockTickets->params);
// }

// 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, $userParams);
// $iterator = new PaginationIterator($mockResults, $strategy);

// $resources = iterator_to_array($iterator);

// $this->assertEquals([
// ['id' => 1, 'name' => 'Resource 1'],
// ['id' => 2, 'name' => 'Resource 2'],
// ], $resources);
// $this->assertEquals($mockResults->params, $userParams);
// }
// public function testCustomMethod()
// {
// $resultsKey = 'results';
// $userParams = ['param' => 1];
// $mockResults = new MockResource($resultsKey, [
// [['id' => 1, 'name' => 'Resource 1'], ['id' => 2, 'name' => 'Resource 2']]
// ]);
// $strategy = new SinglePageStrategy($resultsKey, $userParams);
// $iterator = new PaginationIterator($mockResults, $strategy, 'findDifferent');

// $resources = iterator_to_array($iterator);

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

// public function testHandlesError()
// {
// $expectedErrorMessage = "BOOM!";
// $resultsKey = 'results';
// $userParams = [];
// $mockResults = new MockResource($resultsKey, [], $expectedErrorMessage);
// $strategy = new CbpStrategy($resultsKey, $userParams);
// $iterator = new PaginationIterator($mockResults, $strategy);

// try {
// iterator_to_array($iterator);
// $actualErrorMessage = null;
// } catch (ApiResponseException $e) {
// $actualErrorMessage = $e->getMessage();
// }

// $this->assertEquals($expectedErrorMessage, $actualErrorMessage);
// }
}
Loading