Skip to content

Commit

Permalink
SinglePageStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Oct 26, 2023
1 parent aec8905 commit 3856a8e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 18 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
command: vendor/bin/phpunit --testsuite "Zendesk API Unit Test Suites"
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/TicketsTest.php
# command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php

volumes:
vendor:
4 changes: 2 additions & 2 deletions src/Zendesk/API/Resources/Core/AppInstallations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Zendesk\API\Traits\Resource\Find;
use Zendesk\API\Traits\Resource\FindAll;
use Zendesk\API\Traits\Resource\Update;
use Zendesk\API\Traits\Utility\Pagination\ObpStrategy;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;

/**
* The AppInstallations class exposes methods seen at
Expand Down Expand Up @@ -126,6 +126,6 @@ public function update($id = null, array $updateResourceFields = [], $routeKey =
}

private function paginationStrategyClass() {
return ObpStrategy::class;
return SinglePageStrategy::class;
}
}
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 @@ -19,7 +19,7 @@ public function getPage()
return $response->{$this->resourcesKey};
}

public function isEndOfPage() {
public function shouldGetPage($position) {
return !$this->started || $this->afterCursor;
}
}
5 changes: 2 additions & 3 deletions src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

/**
* Offset Based Pagination
* Can also be used for no pagination
*/
class ObpStrategy extends PaginationStrategy
{
Expand All @@ -20,7 +19,7 @@ public function getPage()
return $response->{$this->resourcesKey};
}

public function isEndOfPage() {
return true; // TODO: explain
public function shouldGetPage($position) {
return $this->pageNumber == 0 || $position >= $this->pageNumber * $this->pageSize;
}
}
19 changes: 11 additions & 8 deletions src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Iterator;

// TODO: doc
// TODO: errors
// TODO: params
// TODO: sorting
class PaginationIterator implements Iterator
{
private $position = 0;
Expand Down Expand Up @@ -33,26 +37,25 @@ public function rewind()
public function valid()
{
$this->getPageIfNecessary();
return isset($this->page[$this->position]);
return !!$this->current();
}

public function current()
{
$this->getPageIfNecessary();
return $this->page[$this->position];
if (isset($this->page[$this->position])) {
return $this->page[$this->position];
} else {
return null;
}
}

private function getPageIfNecessary()
{
if (!$this->isEndOfPage()) {
if (!$this->strategy->shouldGetPage($this->position)) {
return;
}

// TODO: don't keep all pages
$this->page = array_merge($this->page, $this->strategy->getPage());
}

private function isEndOfPage() {
return !isset($this->page[$this->position]) && $this->strategy->isEndOfPage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public function __construct($resourcesRoot, $resourcesKey, $pageSize = self::DEF
}

abstract public function getPage();
abstract public function isEndOfPage();
abstract public function shouldGetPage($position);
}
24 changes: 24 additions & 0 deletions src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Zendesk\API\Traits\Utility\Pagination;


/**
* Single Page (no pagination)
*/
class SinglePageStrategy extends PaginationStrategy
{
protected $started = false;

public function getPage()
{
$this->started = true;
$response = $this->resourcesRoot->findAll();

return $response->{$this->resourcesKey};
}

public function shouldGetPage($position) {
return !$this->started;
}
}
28 changes: 28 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,41 @@
namespace Zendesk\API\UnitTests\Core;

use Faker\Factory;
use GuzzleHttp\Psr7\Response;
use Zendesk\API\UnitTests\BasicTest;

/**
* Class AppInstallationsTest
*/
class AppInstallationsTest extends BasicTest
{
protected $testResource0;
protected $testResource1;
protected $testResource2;

public function setUp()
{
$this->testResource0 = ['anyField' => 'Any field 0'];
$this->testResource1 = ['anyField' => 'Any field 1'];
parent::setUp();
}

public function testIterator()
{
$this->mockApiResponses([
new Response(200, [], json_encode([
'installations' => [$this->testResource0, $this->testResource1],
]))
]);

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

$actual = iterator_to_array($iterator);
$this->assertCount(2, $actual);
$this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField);
$this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField);
}

/**
* Tests if the endpoint is correct since the format is app/installations
*/
Expand Down
3 changes: 0 additions & 3 deletions tests/Zendesk/API/UnitTests/Core/TicketsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public function setUp()
parent::setUp();
}

/**
* Test the iterator
*/
public function testIterator()
{
$this->mockApiResponses([
Expand Down

0 comments on commit 3856a8e

Please sign in to comment.