diff --git a/README.md b/README.md index 62aa961f0..343299ec5 100755 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ The use of the correct pagination is encapsulated using the iterator pattern, wh $ticketsIterator = $client->tickets()->iterator(); foreach ($ticketsIterator as $ticket) { - process($ticket) // Your implementation + process($ticket); // Your implementation } ``` diff --git a/docker-compose.yml b/docker-compose.yml index c28da4a39..e1ac1d780 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,9 @@ services: - .:/app - vendor:/app/vendor 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/OrganizationMembershipsTest.php volumes: vendor: diff --git a/src/Zendesk/API/Resources/Core/AppInstallations.php b/src/Zendesk/API/Resources/Core/AppInstallations.php index b774b87a1..e6dfaf4ec 100644 --- a/src/Zendesk/API/Resources/Core/AppInstallations.php +++ b/src/Zendesk/API/Resources/Core/AppInstallations.php @@ -8,6 +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\SinglePageStrategy; /** * The AppInstallations class exposes methods seen at @@ -123,4 +124,8 @@ public function update($id = null, array $updateResourceFields = [], $routeKey = $updateResourceFields ); } + + private function paginationStrategyClass() { + return SinglePageStrategy::class; + } } diff --git a/src/Zendesk/API/Resources/Core/CustomRoles.php b/src/Zendesk/API/Resources/Core/CustomRoles.php index 3fa3ccd09..5090ba47e 100644 --- a/src/Zendesk/API/Resources/Core/CustomRoles.php +++ b/src/Zendesk/API/Resources/Core/CustomRoles.php @@ -4,6 +4,7 @@ use Zendesk\API\Resources\ResourceAbstract; use Zendesk\API\Traits\Resource\FindAll; +use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy; /** * Class CustomRoles @@ -12,4 +13,8 @@ class CustomRoles extends ResourceAbstract { use FindAll; + + protected function paginationStrategyClass() { + return SinglePageStrategy::class; + } } diff --git a/src/Zendesk/API/Resources/Core/SharingAgreements.php b/src/Zendesk/API/Resources/Core/SharingAgreements.php index 188ca61c7..ddd7495a6 100755 --- a/src/Zendesk/API/Resources/Core/SharingAgreements.php +++ b/src/Zendesk/API/Resources/Core/SharingAgreements.php @@ -4,6 +4,8 @@ use Zendesk\API\Resources\ResourceAbstract; use Zendesk\API\Traits\Resource\FindAll; +use Zendesk\API\Traits\Utility\Pagination\ObpStrategy; +use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy; /** * The SharingAgreements class @@ -12,4 +14,8 @@ class SharingAgreements extends ResourceAbstract { use FindAll; + + protected function paginationStrategyClass() { + return SinglePageStrategy::class; + } } diff --git a/src/Zendesk/API/Resources/Core/Targets.php b/src/Zendesk/API/Resources/Core/Targets.php index 5fffb1dc4..99ad029f6 100644 --- a/src/Zendesk/API/Resources/Core/Targets.php +++ b/src/Zendesk/API/Resources/Core/Targets.php @@ -4,6 +4,7 @@ use Zendesk\API\Resources\ResourceAbstract; use Zendesk\API\Traits\Resource\Defaults; +use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy; /** * The Tags class exposes methods as detailed on https://developer.zendesk.com/rest_api/docs/core/targets @@ -11,4 +12,8 @@ class Targets extends ResourceAbstract { use Defaults; + + protected function paginationStrategyClass() { + return SinglePageStrategy::class; + } } diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 35e1c8c26..e109ebeda 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -12,7 +12,6 @@ use Zendesk\API\Traits\Resource\FindMany; use Zendesk\API\Traits\Resource\UpdateMany; use Zendesk\API\Traits\Utility\InstantiatorTrait; -use Zendesk\API\Traits\Utility\TicketsIterator; /** * The Tickets class exposes key methods for reading and updating ticket data @@ -45,19 +44,6 @@ class Tickets extends ResourceAbstract */ protected $lastAttachments = []; - /** - * Usage: - * foreach ($ticketsIterator as $ticket) { - * process($ticket) - * } - * - * @return TicketsIterator Returns a new TicketsIterator object. - */ - public function iterator() - { - return new TicketsIterator($this); - } - /** * {@inheritdoc} */ diff --git a/src/Zendesk/API/Traits/Resource/FindAll.php b/src/Zendesk/API/Traits/Resource/FindAll.php index 30dd22aef..1c826cc63 100644 --- a/src/Zendesk/API/Traits/Resource/FindAll.php +++ b/src/Zendesk/API/Traits/Resource/FindAll.php @@ -3,9 +3,12 @@ namespace Zendesk\API\Traits\Resource; use Zendesk\API\Exceptions\RouteException; +use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; trait FindAll { + use Pagination; + /** * List all of this resource * @@ -22,7 +25,7 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__) try { $route = $this->getRoute($routeKey, $params); } catch (RouteException $e) { - if (! isset($this->resourceName)) { + if (!isset($this->resourceName)) { $this->resourceName = $this->getResourceNameFromClass(); } diff --git a/src/Zendesk/API/Traits/Resource/Pagination.php b/src/Zendesk/API/Traits/Resource/Pagination.php new file mode 100644 index 000000000..ea974aa5f --- /dev/null +++ b/src/Zendesk/API/Traits/Resource/Pagination.php @@ -0,0 +1,36 @@ +paginationStrategyClass(); + $strategy = new $strategyClass($this, $this->resourcesKey(), AbstractStrategy::DEFAULT_PAGE_SIZE); + return new PaginationIterator($strategy); + } + + protected function paginationStrategyClass() { + return CbpStrategy::class; + } + + /* + * @return string eg: "job_statuses" + */ + protected function resourcesKey() { + return $this->objectNamePlural; + } +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php new file mode 100644 index 000000000..5f5a0a349 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -0,0 +1,28 @@ +{clientList}()` + */ + protected $clientList; + + /* + * @var string The response key where the data is returned + */ + protected $resourcesKey; + protected $pageSize; + + public function __construct($clientList, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) + { + $this->clientList = $clientList; + $this->resourcesKey = $resourcesKey; + $this->pageSize = $pageSize; + } + + abstract public function getPage(); + abstract public function shouldGetPage($position); +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php new file mode 100644 index 000000000..bdfeff571 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -0,0 +1,26 @@ +started = true; + $params = ['page[size]' => $this->pageSize]; + if ($this->afterCursor) { + $params['page[after]'] = $this->afterCursor; + } + $response = $this->clientList->findAll($params); + + $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; + return $response->{$this->resourcesKey}; + } + + public function shouldGetPage($position) { + return !$this->started || $this->afterCursor; + } +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php new file mode 100644 index 000000000..83dd3cc69 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -0,0 +1,25 @@ +pageNumber; + $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; + $response = $this->clientList->findAll($params); + + return $response->{$this->resourcesKey}; + } + + public function shouldGetPage($position) { + return $this->pageNumber == 0 || $position >= $this->pageNumber * $this->pageSize; + } +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php new file mode 100644 index 000000000..d0c6d5104 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -0,0 +1,56 @@ +strategy = $strategy; + } + + public function key() + { + return $this->position; + } + + public function next() + { + ++$this->position; + } + + public function rewind() + { + $this->position = 0; + } + + public function valid() + { + $this->getPageIfNecessary(); + return !!$this->current(); + } + + public function current() + { + if (isset($this->page[$this->position])) { + return $this->page[$this->position]; + } else { + return null; + } + } + + private function getPageIfNecessary() + { + if (!$this->strategy->shouldGetPage($this->position)) { + return; + } + + $this->page = array_merge($this->page, $this->strategy->getPage()); + } +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php new file mode 100644 index 000000000..d82d5269e --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php @@ -0,0 +1,24 @@ +started = true; + $response = $this->clientList->findAll(); + + return $response->{$this->resourcesKey}; + } + + public function shouldGetPage($position) { + return !$this->started; + } +} diff --git a/src/Zendesk/API/Traits/Utility/TicketsIterator.php b/src/Zendesk/API/Traits/Utility/TicketsIterator.php deleted file mode 100644 index d72524c7a..000000000 --- a/src/Zendesk/API/Traits/Utility/TicketsIterator.php +++ /dev/null @@ -1,119 +0,0 @@ -resources = $resources; - $this->pageSize = $pageSize; - } - - /** - * @return Ticket The current ticket, possibly fetching a new page. - */ - public function current() - { - if (!isset($this->tickets[$this->position]) && (!$this->started || $this->afterCursor)) { - $this->getPage(); - } - return $this->tickets[$this->position]; - } - - /** - * @return int The current position. - */ - public function key() - { - return $this->position; - } - - /** - * Moves to the next ticket. - */ - public function next() - { - ++$this->position; - } - - /** - * Rewinds to the first ticket. - */ - public function rewind() - { - $this->position = 0; - } - - /** - * @return bool True there is a current element after calls to `rewind` or `next`, possibly fetching a new page. - */ - public function valid() - { - if (!isset($this->tickets[$this->position]) && (!$this->started || $this->afterCursor)) { - $this->getPage(); - } - return isset($this->tickets[$this->position]); - } - - /** - * Fetches the next page of tickets from the API. - */ - private function getPage() - { - $this->started = true; - $params = ['page[size]' => $this->pageSize]; - if ($this->afterCursor) { - $params['page[after]'] = $this->afterCursor; - } - $response = $this->resources->findAll($params); - $this->tickets = array_merge($this->tickets, $response->tickets); - $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; - } -} diff --git a/tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php b/tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php index 0b5fa987a..dba89e965 100644 --- a/tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php @@ -3,6 +3,7 @@ namespace Zendesk\API\UnitTests\Core; use Faker\Factory; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -10,6 +11,33 @@ */ 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 */ diff --git a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php index 5ed9d516d..9e61ce9da 100755 --- a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -9,6 +10,42 @@ */ class AutomationsTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'automations' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'automations' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->automations()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } /** * Test we can use endpoint to get active automations diff --git a/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php b/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php index f6c14ef81..0d78211a8 100644 --- a/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php +++ b/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php @@ -2,10 +2,41 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; class CustomRolesTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // Single Page + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'custom_roles' => [$this->testResource0, $this->testResource1, $this->testResource2] + + ])) + ]); + + $iterator = $this->client->customRoles()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } public function testRoutes() { $this->assertTrue(method_exists($this->client->customRoles(), 'findAll')); diff --git a/tests/Zendesk/API/UnitTests/Core/MacrosTest.php b/tests/Zendesk/API/UnitTests/Core/MacrosTest.php index 3f17c0f4d..da581824b 100755 --- a/tests/Zendesk/API/UnitTests/Core/MacrosTest.php +++ b/tests/Zendesk/API/UnitTests/Core/MacrosTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -10,6 +11,43 @@ */ class MacrosTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'macros' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'macros' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->macros()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } + /** * Test the `GET /api/v2/macros/active.json` endpoint * Lists active macros for the current user diff --git a/tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php b/tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php index 308f46ad6..8b1163022 100644 --- a/tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -9,6 +10,43 @@ */ class OrganizationMembershipsTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'organization_memberships' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'organization_memberships' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->organizationMemberships()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } + /** * Test find method */ diff --git a/tests/Zendesk/API/UnitTests/Core/OrganizationsTest.php b/tests/Zendesk/API/UnitTests/Core/OrganizationsTest.php index ef753946b..c0c1051be 100644 --- a/tests/Zendesk/API/UnitTests/Core/OrganizationsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/OrganizationsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -9,6 +10,43 @@ */ class OrganizationsTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'organizations' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'organizations' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->organizations()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } + /** * test for FindAll with chained user resource. */ diff --git a/tests/Zendesk/API/UnitTests/Core/RequestsTest.php b/tests/Zendesk/API/UnitTests/Core/RequestsTest.php index 8ed12b216..dd321f25b 100755 --- a/tests/Zendesk/API/UnitTests/Core/RequestsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/RequestsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -9,6 +10,43 @@ */ class RequestsTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'requests' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'requests' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->requests()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } + /** * test findAll method */ diff --git a/tests/Zendesk/API/UnitTests/Core/SatisfactionRatingsTest.php b/tests/Zendesk/API/UnitTests/Core/SatisfactionRatingsTest.php index b218f0d30..339c24e58 100755 --- a/tests/Zendesk/API/UnitTests/Core/SatisfactionRatingsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/SatisfactionRatingsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -9,6 +10,43 @@ */ class SatisfactionRatingsTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'satisfaction_ratings' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'satisfaction_ratings' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->satisfactionRatings()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } + /** * Test that the correct traits were added by checking the available methods */ diff --git a/tests/Zendesk/API/UnitTests/Core/TagsTest.php b/tests/Zendesk/API/UnitTests/Core/TagsTest.php index fc9e73edd..3796b318a 100644 --- a/tests/Zendesk/API/UnitTests/Core/TagsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TagsTest.php @@ -2,6 +2,7 @@ namespace Zendesk\API\UnitTests\Core; +use GuzzleHttp\Psr7\Response; use Zendesk\API\UnitTests\BasicTest; /** @@ -9,6 +10,43 @@ */ class TagsTest extends BasicTest { + protected $testResource0; + protected $testResource1; + protected $testResource2; + + public function setUp() + { + $this->testResource0 = ['anyField' => 'Any field 0']; + $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'tags' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'tags' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $iterator = $this->client->tags()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(3, $actual); + $this->assertEquals($this->testResource0['anyField'], $actual[0]->anyField); + $this->assertEquals($this->testResource1['anyField'], $actual[1]->anyField); + $this->assertEquals($this->testResource2['anyField'], $actual[2]->anyField); + } + /** * Test that the Tags resource class actually creates the correct routes: * diff --git a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index c84bf5aad..726aaafa2 100755 --- a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php @@ -38,6 +38,27 @@ public function setUp() parent::setUp(); } + public function testIterator() + { + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'tickets' => [$this->testTicket], + 'meta' => ['has_more' => true, 'after_cursor' => 'some_cursor'] + ])), + new Response(200, [], json_encode([ + 'tickets' => [$this->testTicket2], + 'meta' => ['has_more' => false, 'after_cursor' => null] + ])) + ]); + + $iterator = $this->client->tickets()->iterator(); + + $actual = iterator_to_array($iterator); + $this->assertCount(2, $actual); + $this->assertEquals($this->testTicket['subject'], $actual[0]->subject); + $this->assertEquals($this->testTicket2['subject'], $actual[1]->subject); + } + /** * Tests if the client can call and build the tickets endpoint with the proper sideloads */ @@ -178,7 +199,7 @@ public function testCreateWithAttachment() 'postFields' => $postFields, ]); } - + /** * Tests that we can create the ticket with an async parameter which will add `async=true` to the query parameters */ diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php new file mode 100644 index 000000000..0ac8fda8b --- /dev/null +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php @@ -0,0 +1,77 @@ +resourceName = $resourceName; + $this->resources = $resources; + $this->callCount = 0; + } + + public function findAll($params) + { + // Simulate two pages of resources + $resources = $this->callCount === 0 + ? $this->resources[0] + : $this->resources[1]; + + // Simulate a cursor for the next page on the first call + $afterCursor = $this->callCount === 0 ? 'cursor_for_next_page' : null; + + $this->callCount++; + + return (object) [ + $this->resourceName => $resources, + 'meta' => (object) [ + 'has_more' => $afterCursor !== null, + 'after_cursor' => $afterCursor, + ], + ]; + } +} + +class PaginationTest extends BasicTest +{ + public function testFetchesTickets() + { + $mockTickets = new MockResource('tickets', [ + [['id' => 1], ['id' => 2]], + [['id' => 3], ['id' => 4]] + ]); + $strategy = new CbpStrategy($mockTickets, 'tickets', 2); + $iterator = new PaginationIterator($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($mockUsers, 'users', 2); + $iterator = new PaginationIterator($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); + } +} diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/TicketsIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/TicketsIteratorTest.php deleted file mode 100644 index c19e47479..000000000 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/TicketsIteratorTest.php +++ /dev/null @@ -1,44 +0,0 @@ - 1], ['id' => 2]] - : [['id' => 3], ['id' => 4]]; - - // Simulate a cursor for the next page on the first call - $afterCursor = $callCount === 0 ? 'cursor_for_next_page' : null; - - $callCount++; - - return (object) [ - 'tickets' => $tickets, - 'meta' => (object) [ - 'has_more' => $afterCursor !== null, - 'after_cursor' => $afterCursor, - ], - ]; - } -} - -class TicketsIteratorTest extends BasicTest -{ - public function testFetchesTickets() - { - $mockTickets = new MockTickets; - $iterator = new TicketsIterator($mockTickets, 2); - - $tickets = iterator_to_array($iterator); - - $this->assertEquals([['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4]], $tickets); - } -}