From 1f32c17c206bc56604ee4b15658c85817375646c Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Wed, 18 Oct 2023 14:13:33 +1000 Subject: [PATCH 01/18] Extract TicketsIterator isEndOfPage() --- src/Zendesk/API/Traits/Utility/TicketsIterator.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/TicketsIterator.php b/src/Zendesk/API/Traits/Utility/TicketsIterator.php index d72524c7..64857a97 100644 --- a/src/Zendesk/API/Traits/Utility/TicketsIterator.php +++ b/src/Zendesk/API/Traits/Utility/TicketsIterator.php @@ -61,7 +61,7 @@ public function __construct($resources, $pageSize = self::DEFAULT_PAGE_SIZE) */ public function current() { - if (!isset($this->tickets[$this->position]) && (!$this->started || $this->afterCursor)) { + if ($this->isEndOfPage()) { $this->getPage(); } return $this->tickets[$this->position]; @@ -96,7 +96,7 @@ public function rewind() */ public function valid() { - if (!isset($this->tickets[$this->position]) && (!$this->started || $this->afterCursor)) { + if ($this->isEndOfPage()) { $this->getPage(); } return isset($this->tickets[$this->position]); @@ -116,4 +116,12 @@ private function getPage() $this->tickets = array_merge($this->tickets, $response->tickets); $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; } + + /** + * @return bool True if the end of the page has been reached, false otherwise. + */ + private function isEndOfPage() + { + return !isset($this->tickets[$this->position]) && (!$this->started || $this->afterCursor); + } } From f4c4ee590d380a7877f4280f38504739e2f48c06 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Wed, 18 Oct 2023 14:22:28 +1000 Subject: [PATCH 02/18] Rename TicketsIterator $resources $resourcesRoot --- src/Zendesk/API/Traits/Utility/TicketsIterator.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/TicketsIterator.php b/src/Zendesk/API/Traits/Utility/TicketsIterator.php index 64857a97..6cbab403 100644 --- a/src/Zendesk/API/Traits/Utility/TicketsIterator.php +++ b/src/Zendesk/API/Traits/Utility/TicketsIterator.php @@ -17,7 +17,7 @@ class TicketsIterator implements Iterator /** * @var Zendesk\API\HttpClient The Zendesk API client. */ - private $resources; + private $resourcesRoot; /** * @var int The current position in the tickets array. @@ -47,12 +47,12 @@ class TicketsIterator implements Iterator /** * TicketsIterator constructor. * - * @param \stdClass $resources implementing the iterator ($this), with findAll() + * @param \stdClass $resourcesRoot implementing the iterator ($this), with findAll() defined * @param int $pageSize The number of tickets to fetch per page. */ - public function __construct($resources, $pageSize = self::DEFAULT_PAGE_SIZE) + public function __construct($resourcesRoot, $pageSize = self::DEFAULT_PAGE_SIZE) { - $this->resources = $resources; + $this->resourcesRoot = $resourcesRoot; $this->pageSize = $pageSize; } @@ -112,7 +112,7 @@ private function getPage() if ($this->afterCursor) { $params['page[after]'] = $this->afterCursor; } - $response = $this->resources->findAll($params); + $response = $this->resourcesRoot->findAll($params); $this->tickets = array_merge($this->tickets, $response->tickets); $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; } From 408ca26f72d6f3d1a635316c18da36bc9670449e Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Wed, 18 Oct 2023 14:46:22 +1000 Subject: [PATCH 03/18] Rename TicketsIterator tickets to resources --- .../API/Traits/Utility/TicketsIterator.php | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/TicketsIterator.php b/src/Zendesk/API/Traits/Utility/TicketsIterator.php index 6cbab403..91b0d272 100644 --- a/src/Zendesk/API/Traits/Utility/TicketsIterator.php +++ b/src/Zendesk/API/Traits/Utility/TicketsIterator.php @@ -5,7 +5,7 @@ use Iterator; /** - * An iterator for fetching tickets from the Zendesk API using cursor-based pagination. + * An iterator for fetching resources from the Zendesk API using cursor-based pagination. */ class TicketsIterator implements Iterator { @@ -15,40 +15,39 @@ class TicketsIterator implements Iterator public const DEFAULT_PAGE_SIZE = 100; /** - * @var Zendesk\API\HttpClient The Zendesk API client. + * @var \stdClass implementing the iterator ($this), with findAll() defined */ private $resourcesRoot; /** - * @var int The current position in the tickets array. + * @var int The current position in the page. */ private $position = 0; + // TODO: page is actually growing every call, should be only one page (reset position) /** - * @var array The fetched tickets. + * @var array The fetched page resources. */ - private $tickets = []; + private $page = []; /** - * @var string|null The cursor for the next page of tickets. + * @var string|null The cursor for the next page of resources. */ private $afterCursor = null; /** - * @var int The number of tickets to fetch per page. + * @var int The number of resources to fetch per page. */ private $pageSize; /** - * @var bool A flag indicating whether the iterator has started fetching tickets. + * @var bool A flag indicating whether the iterator has started fetching resources. */ private $started = false; /** - * TicketsIterator constructor. - * * @param \stdClass $resourcesRoot implementing the iterator ($this), with findAll() defined - * @param int $pageSize The number of tickets to fetch per page. + * @param int $pageSize The number of resources to fetch per page. */ public function __construct($resourcesRoot, $pageSize = self::DEFAULT_PAGE_SIZE) { @@ -57,14 +56,14 @@ public function __construct($resourcesRoot, $pageSize = self::DEFAULT_PAGE_SIZE) } /** - * @return Ticket The current ticket, possibly fetching a new page. + * @return mixed (using FindAll) The current resource, maybe fetching a new page. */ public function current() { if ($this->isEndOfPage()) { $this->getPage(); } - return $this->tickets[$this->position]; + return $this->page[$this->position]; } /** @@ -76,7 +75,7 @@ public function key() } /** - * Moves to the next ticket. + * Moves to the next resource. */ public function next() { @@ -84,7 +83,7 @@ public function next() } /** - * Rewinds to the first ticket. + * Rewinds to the first resource. */ public function rewind() { @@ -99,11 +98,11 @@ public function valid() if ($this->isEndOfPage()) { $this->getPage(); } - return isset($this->tickets[$this->position]); + return isset($this->page[$this->position]); } /** - * Fetches the next page of tickets from the API. + * Fetches the next page of resources from the API. */ private function getPage() { @@ -113,7 +112,7 @@ private function getPage() $params['page[after]'] = $this->afterCursor; } $response = $this->resourcesRoot->findAll($params); - $this->tickets = array_merge($this->tickets, $response->tickets); + $this->page = array_merge($this->page, $response->tickets); $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; } @@ -122,6 +121,6 @@ private function getPage() */ private function isEndOfPage() { - return !isset($this->tickets[$this->position]) && (!$this->started || $this->afterCursor); + return !isset($this->page[$this->position]) && (!$this->started || $this->afterCursor); } } From 04cfebf996413f283c35361323d4d7bd3d6a613f Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Wed, 18 Oct 2023 14:49:47 +1000 Subject: [PATCH 04/18] Rename TicketsIterator.php to CbpIterator.php --- src/Zendesk/API/Resources/Core/Tickets.php | 6 +++--- .../Traits/Utility/{TicketsIterator.php => CbpIterator.php} | 2 +- .../{TicketsIteratorTest.php => CbpIteratorTest.php} | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/Zendesk/API/Traits/Utility/{TicketsIterator.php => CbpIterator.php} (98%) rename tests/Zendesk/API/UnitTests/Traits/Utility/{TicketsIteratorTest.php => CbpIteratorTest.php} (86%) diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 35e1c8c2..c2688d6d 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -12,7 +12,7 @@ use Zendesk\API\Traits\Resource\FindMany; use Zendesk\API\Traits\Resource\UpdateMany; use Zendesk\API\Traits\Utility\InstantiatorTrait; -use Zendesk\API\Traits\Utility\TicketsIterator; +use Zendesk\API\Traits\Utility\CbpIterator; /** * The Tickets class exposes key methods for reading and updating ticket data @@ -51,11 +51,11 @@ class Tickets extends ResourceAbstract * process($ticket) * } * - * @return TicketsIterator Returns a new TicketsIterator object. + * @return CbpIterator Returns a new CbpIterator object. */ public function iterator() { - return new TicketsIterator($this); + return new CbpIterator($this); } /** diff --git a/src/Zendesk/API/Traits/Utility/TicketsIterator.php b/src/Zendesk/API/Traits/Utility/CbpIterator.php similarity index 98% rename from src/Zendesk/API/Traits/Utility/TicketsIterator.php rename to src/Zendesk/API/Traits/Utility/CbpIterator.php index 91b0d272..7aded76c 100644 --- a/src/Zendesk/API/Traits/Utility/TicketsIterator.php +++ b/src/Zendesk/API/Traits/Utility/CbpIterator.php @@ -7,7 +7,7 @@ /** * An iterator for fetching resources from the Zendesk API using cursor-based pagination. */ -class TicketsIterator implements Iterator +class CbpIterator implements Iterator { /** * The default number of items per page for pagination. diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/TicketsIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php similarity index 86% rename from tests/Zendesk/API/UnitTests/Traits/Utility/TicketsIteratorTest.php rename to tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php index c19e4747..8422ee75 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/TicketsIteratorTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php @@ -3,7 +3,7 @@ namespace Zendesk\API\UnitTests\Core; use Zendesk\API\UnitTests\BasicTest; -use Zendesk\API\Traits\Utility\TicketsIterator; +use Zendesk\API\Traits\Utility\CbpIterator; class MockTickets { public function findAll($params) @@ -30,12 +30,12 @@ public function findAll($params) } } -class TicketsIteratorTest extends BasicTest +class CbpIteratorTest extends BasicTest { public function testFetchesTickets() { $mockTickets = new MockTickets; - $iterator = new TicketsIterator($mockTickets, 2); + $iterator = new CbpIterator($mockTickets, 2); $tickets = iterator_to_array($iterator); From c5016b95220b018cfadcb37c5bce8a66c62e95f7 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Wed, 18 Oct 2023 15:31:14 +1000 Subject: [PATCH 05/18] Add Users iterator() --- src/Zendesk/API/Resources/Core/Tickets.php | 4 +- src/Zendesk/API/Resources/Core/Users.php | 14 +++++ .../API/Traits/Utility/CbpIterator.php | 15 ++++-- .../Traits/Utility/CbpIteratorTest.php | 54 ++++++++++++++----- 4 files changed, 69 insertions(+), 18 deletions(-) diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index c2688d6d..54d9efbf 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -51,11 +51,11 @@ class Tickets extends ResourceAbstract * process($ticket) * } * - * @return CbpIterator Returns a new CbpIterator object. + * @return CbpIterator to fetch all pages. */ public function iterator() { - return new CbpIterator($this); + return new CbpIterator($this, 'tickets'); } /** diff --git a/src/Zendesk/API/Resources/Core/Users.php b/src/Zendesk/API/Resources/Core/Users.php index 00273254..25ca2420 100755 --- a/src/Zendesk/API/Resources/Core/Users.php +++ b/src/Zendesk/API/Resources/Core/Users.php @@ -14,6 +14,7 @@ use Zendesk\API\Traits\Resource\MultipartUpload; use Zendesk\API\Traits\Resource\UpdateMany; use Zendesk\API\Traits\Utility\InstantiatorTrait; +use Zendesk\API\Traits\Utility\CbpIterator; /** * The Users class exposes user management methods @@ -48,6 +49,19 @@ class Users extends ResourceAbstract */ protected $identities; + /** + * Usage: + * foreach ($usersIterator as $user) { + * process($user) + * } + * + * @return CbpIterator to fetch all pages. + */ + public function iterator() + { + return new CbpIterator($this, 'users'); + } + /** * {@inheritdoc} */ diff --git a/src/Zendesk/API/Traits/Utility/CbpIterator.php b/src/Zendesk/API/Traits/Utility/CbpIterator.php index 7aded76c..cc9a73da 100644 --- a/src/Zendesk/API/Traits/Utility/CbpIterator.php +++ b/src/Zendesk/API/Traits/Utility/CbpIterator.php @@ -14,8 +14,13 @@ class CbpIterator implements Iterator */ public const DEFAULT_PAGE_SIZE = 100; + + /** + * @var string The root key in the response with the page resources (eg: users, tickets). + */ + private $resourcesKey; /** - * @var \stdClass implementing the iterator ($this), with findAll() defined + * @var \stdClass implementing the iterator ($this), with findAll() defined. */ private $resourcesRoot; @@ -46,12 +51,14 @@ class CbpIterator implements Iterator private $started = false; /** - * @param \stdClass $resourcesRoot implementing the iterator ($this), with findAll() defined + * @param mixed $resourcesRoot (using trait FindAll) * @param int $pageSize The number of resources to fetch per page. + * @param string $resourcesKey The root key in the response with the page resources (eg: users, tickets) */ - public function __construct($resourcesRoot, $pageSize = self::DEFAULT_PAGE_SIZE) + public function __construct($resourcesRoot, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) { $this->resourcesRoot = $resourcesRoot; + $this->resourcesKey = $resourcesKey; $this->pageSize = $pageSize; } @@ -112,7 +119,7 @@ private function getPage() $params['page[after]'] = $this->afterCursor; } $response = $this->resourcesRoot->findAll($params); - $this->page = array_merge($this->page, $response->tickets); + $this->page = array_merge($this->page, $response->{$this->resourcesKey}); $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; } diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php index 8422ee75..8c5eb0cb 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php @@ -5,23 +5,32 @@ use Zendesk\API\UnitTests\BasicTest; use Zendesk\API\Traits\Utility\CbpIterator; -class MockTickets { - public function findAll($params) +class MockResource { + private $resources; + private $resourceName; + private $callCount = 0; + + public function __construct($resourceName, $resources) { - static $callCount = 0; + $this->resourceName = $resourceName; + $this->resources = $resources; + $this->callCount = 0; + } - // Simulate two pages of tickets - $tickets = $callCount === 0 - ? [['id' => 1], ['id' => 2]] - : [['id' => 3], ['id' => 4]]; + 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 = $callCount === 0 ? 'cursor_for_next_page' : null; + $afterCursor = $this->callCount === 0 ? 'cursor_for_next_page' : null; - $callCount++; + $this->callCount++; return (object) [ - 'tickets' => $tickets, + $this->resourceName => $resources, 'meta' => (object) [ 'has_more' => $afterCursor !== null, 'after_cursor' => $afterCursor, @@ -34,11 +43,32 @@ class CbpIteratorTest extends BasicTest { public function testFetchesTickets() { - $mockTickets = new MockTickets; - $iterator = new CbpIterator($mockTickets, 2); + $mockTickets = new MockResource('tickets', [ + [['id' => 1], ['id' => 2]], + [['id' => 3], ['id' => 4]] + ]); + $iterator = new CbpIterator($mockTickets, 'tickets', 2); $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']] + ]); + $iterator = new CbpIterator($mockUsers, 'users', 2); + + $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); + } } From 238b31473068b928072460b4cce302a1e86510e7 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Thu, 19 Oct 2023 07:57:38 +1000 Subject: [PATCH 06/18] Refactor extract CbpIterator getPageIfNecessary() --- .../API/Traits/Utility/CbpIterator.php | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/CbpIterator.php b/src/Zendesk/API/Traits/Utility/CbpIterator.php index cc9a73da..e2add051 100644 --- a/src/Zendesk/API/Traits/Utility/CbpIterator.php +++ b/src/Zendesk/API/Traits/Utility/CbpIterator.php @@ -14,7 +14,6 @@ class CbpIterator implements Iterator */ public const DEFAULT_PAGE_SIZE = 100; - /** * @var string The root key in the response with the page resources (eg: users, tickets). */ @@ -62,17 +61,6 @@ public function __construct($resourcesRoot, $resourcesKey, $pageSize = self::DEF $this->pageSize = $pageSize; } - /** - * @return mixed (using FindAll) The current resource, maybe fetching a new page. - */ - public function current() - { - if ($this->isEndOfPage()) { - $this->getPage(); - } - return $this->page[$this->position]; - } - /** * @return int The current position. */ @@ -102,17 +90,28 @@ public function rewind() */ public function valid() { - if ($this->isEndOfPage()) { - $this->getPage(); - } + $this->getPageIfNecessary(); return isset($this->page[$this->position]); } + /** + * @return mixed (using FindAll) The current resource, maybe fetching a new page. + */ + public function current() + { + $this->getPageIfNecessary(); + return $this->page[$this->position]; + } + /** * Fetches the next page of resources from the API. */ - private function getPage() + private function getPageIfNecessary() { + if (!$this->isEndOfPage()) { + return; + } + $this->started = true; $params = ['page[size]' => $this->pageSize]; if ($this->afterCursor) { From 73457ea2917d72eb7a46873889a7a195e0cdc796 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Thu, 19 Oct 2023 10:13:26 +1000 Subject: [PATCH 07/18] Add Zendesk\API\Traits\Utility\Pagination --- docker-compose.yml | 1 + src/Zendesk/API/Resources/Core/Tickets.php | 9 +- src/Zendesk/API/Resources/Core/Users.php | 8 +- .../API/Traits/Utility/CbpIterator.php | 132 ------------------ .../Traits/Utility/Pagination/CbpStrategy.php | 25 ++++ .../Utility/Pagination/PaginationIterator.php | 58 ++++++++ .../Utility/Pagination/PaginationStrategy.php | 22 +++ ...CbpIteratorTest.php => PaginationTest.php} | 12 +- 8 files changed, 125 insertions(+), 142 deletions(-) delete mode 100644 src/Zendesk/API/Traits/Utility/CbpIterator.php create mode 100644 src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php create mode 100644 src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php create mode 100644 src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php rename tests/Zendesk/API/UnitTests/Traits/Utility/{CbpIteratorTest.php => PaginationTest.php} (81%) diff --git a/docker-compose.yml b/docker-compose.yml index c28da4a3..d4282c55 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,7 @@ 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 volumes: vendor: diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 54d9efbf..b6aec308 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -12,7 +12,8 @@ use Zendesk\API\Traits\Resource\FindMany; use Zendesk\API\Traits\Resource\UpdateMany; use Zendesk\API\Traits\Utility\InstantiatorTrait; -use Zendesk\API\Traits\Utility\CbpIterator; +use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; +use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; /** * The Tickets class exposes key methods for reading and updating ticket data @@ -51,11 +52,13 @@ class Tickets extends ResourceAbstract * process($ticket) * } * - * @return CbpIterator to fetch all pages. + * @return PaginationIterator to fetch all pages. */ public function iterator() { - return new CbpIterator($this, 'tickets'); + // TODO: default page size everywhere + $strategy = new CbpStrategy($this, 'tickets', 2); + return new PaginationIterator($strategy); } /** diff --git a/src/Zendesk/API/Resources/Core/Users.php b/src/Zendesk/API/Resources/Core/Users.php index 25ca2420..58ad367c 100755 --- a/src/Zendesk/API/Resources/Core/Users.php +++ b/src/Zendesk/API/Resources/Core/Users.php @@ -14,7 +14,8 @@ use Zendesk\API\Traits\Resource\MultipartUpload; use Zendesk\API\Traits\Resource\UpdateMany; use Zendesk\API\Traits\Utility\InstantiatorTrait; -use Zendesk\API\Traits\Utility\CbpIterator; +use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; +use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; /** * The Users class exposes user management methods @@ -55,11 +56,12 @@ class Users extends ResourceAbstract * process($user) * } * - * @return CbpIterator to fetch all pages. + * @return PaginationIterator to fetch all pages. */ public function iterator() { - return new CbpIterator($this, 'users'); + $strategy = new CbpStrategy($this, 'users', 2); + return new PaginationIterator($strategy); } /** diff --git a/src/Zendesk/API/Traits/Utility/CbpIterator.php b/src/Zendesk/API/Traits/Utility/CbpIterator.php deleted file mode 100644 index e2add051..00000000 --- a/src/Zendesk/API/Traits/Utility/CbpIterator.php +++ /dev/null @@ -1,132 +0,0 @@ -resourcesRoot = $resourcesRoot; - $this->resourcesKey = $resourcesKey; - $this->pageSize = $pageSize; - } - - /** - * @return int The current position. - */ - public function key() - { - return $this->position; - } - - /** - * Moves to the next resource. - */ - public function next() - { - ++$this->position; - } - - /** - * Rewinds to the first resource. - */ - 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() - { - $this->getPageIfNecessary(); - return isset($this->page[$this->position]); - } - - /** - * @return mixed (using FindAll) The current resource, maybe fetching a new page. - */ - public function current() - { - $this->getPageIfNecessary(); - return $this->page[$this->position]; - } - - /** - * Fetches the next page of resources from the API. - */ - private function getPageIfNecessary() - { - if (!$this->isEndOfPage()) { - return; - } - - $this->started = true; - $params = ['page[size]' => $this->pageSize]; - if ($this->afterCursor) { - $params['page[after]'] = $this->afterCursor; - } - $response = $this->resourcesRoot->findAll($params); - $this->page = array_merge($this->page, $response->{$this->resourcesKey}); - $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; - } - - /** - * @return bool True if the end of the page has been reached, false otherwise. - */ - private function isEndOfPage() - { - return !isset($this->page[$this->position]) && (!$this->started || $this->afterCursor); - } -} 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 00000000..9057d872 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -0,0 +1,25 @@ +started = true; + $params = ['page[size]' => $this->pageSize]; + if ($this->afterCursor) { + $params['page[after]'] = $this->afterCursor; + } + $response = $this->resourcesRoot->findAll($params); + $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; + return $response->{$this->resourcesKey}; + } + + public function isEndOfPage() { + return !$this->started || $this->afterCursor; + } +} 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 00000000..6b8d0a3c --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -0,0 +1,58 @@ +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 isset($this->page[$this->position]); + } + + // TODO: refactor to isset($this->current()) + public function current() + { + $this->getPageIfNecessary(); + return $this->page[$this->position]; + } + + private function getPageIfNecessary() + { + if (!$this->isEndOfPage()) { + return; + } + + $this->page = array_merge($this->page, $this->strategy->getPage()); + } + + private function isEndOfPage() { + return !isset($this->page[$this->position]) && $this->strategy->isEndOfPage(); + } +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php new file mode 100644 index 00000000..5e3ebf7e --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php @@ -0,0 +1,22 @@ +resourcesRoot = $resourcesRoot; + $this->resourcesKey = $resourcesKey; + $this->pageSize = $pageSize; + } + + abstract public function getPage(); + abstract public function isEndOfPage(); +} diff --git a/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php similarity index 81% rename from tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php rename to tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php index 8c5eb0cb..c044d0cc 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/CbpIteratorTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php @@ -2,8 +2,9 @@ namespace Zendesk\API\UnitTests\Core; +use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; use Zendesk\API\UnitTests\BasicTest; -use Zendesk\API\Traits\Utility\CbpIterator; +use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; class MockResource { private $resources; @@ -39,7 +40,8 @@ public function findAll($params) } } -class CbpIteratorTest extends BasicTest +// TODO: test resources +class PaginationTest extends BasicTest { public function testFetchesTickets() { @@ -47,7 +49,8 @@ public function testFetchesTickets() [['id' => 1], ['id' => 2]], [['id' => 3], ['id' => 4]] ]); - $iterator = new CbpIterator($mockTickets, 'tickets', 2); + $strategy = new CbpStrategy($mockTickets, 'tickets', 2); + $iterator = new PaginationIterator($strategy); $tickets = iterator_to_array($iterator); @@ -60,7 +63,8 @@ public function testFetchesUsers() [['id' => 1, 'name' => 'User 1'], ['id' => 2, 'name' => 'User 2']], [['id' => 3, 'name' => 'User 3'], ['id' => 4, 'name' => 'User 4']] ]); - $iterator = new CbpIterator($mockUsers, 'users', 2); + $strategy = new CbpStrategy($mockUsers, 'users', 2); + $iterator = new PaginationIterator($strategy); $users = iterator_to_array($iterator); From c65f6265cd873e1c8735da0f0bc1835b36e729c6 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Fri, 20 Oct 2023 15:31:13 +1000 Subject: [PATCH 08/18] ObpStrategy --- docker-compose.yml | 1 + .../Traits/Utility/Pagination/ObpStrategy.php | 20 +++++++ .../Utility/Pagination/PaginationIterator.php | 1 - .../API/UnitTests/Core/TicketsTest.php | 26 ++++++++- .../Traits/Utility/PaginationTest.php | 55 ++++++++++++++++++- 5 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php diff --git a/docker-compose.yml b/docker-compose.yml index d4282c55..90e70f03 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - 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 volumes: vendor: 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 00000000..fd703c89 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -0,0 +1,20 @@ +pageNumber; + $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; + $response = $this->resourcesRoot->findAll($params); + return $response->{$this->resourcesKey}; + } + + public function isEndOfPage() { + return true; // TODO: explain + } +} diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php index 6b8d0a3c..bc553aeb 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -36,7 +36,6 @@ public function valid() return isset($this->page[$this->position]); } - // TODO: refactor to isset($this->current()) public function current() { $this->getPageIfNecessary(); diff --git a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index c84bf5aa..066dcee7 100755 --- a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php @@ -38,6 +38,30 @@ public function setUp() parent::setUp(); } + /** + * Test the iterator + */ + 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 +202,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 index c044d0cc..315e878a 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php @@ -40,7 +40,6 @@ public function findAll($params) } } -// TODO: test resources class PaginationTest extends BasicTest { public function testFetchesTickets() @@ -76,3 +75,57 @@ public function testFetchesUsers() ], $users); } } + +// agl "use (FindAll|Default)" src +// src/Zendesk/API/Resources/Core/Activities.php +// src/Zendesk/API/Resources/Core/AppInstallationLocations.php +// src/Zendesk/API/Resources/Core/AppInstallations.php +// src/Zendesk/API/Resources/Core/AppLocations.php +// src/Zendesk/API/Resources/Core/AuditLogs.php +// src/Zendesk/API/Resources/Core/Automations.php +// src/Zendesk/API/Resources/Core/Bookmarks.php +// src/Zendesk/API/Resources/Core/Brands.php +// src/Zendesk/API/Resources/Core/CustomRoles.php +// src/Zendesk/API/Resources/Core/DynamicContentItems.php +// src/Zendesk/API/Resources/Core/DynamicContentItemVariants.php +// src/Zendesk/API/Resources/Core/GroupMemberships.php +// src/Zendesk/API/Resources/Core/Groups.php +// src/Zendesk/API/Resources/Core/Locales.php +// src/Zendesk/API/Resources/Core/Macros.php +// src/Zendesk/API/Resources/Core/OAuthClients.php +// src/Zendesk/API/Resources/Core/OAuthTokens.php +// src/Zendesk/API/Resources/Core/OrganizationFields.php +// src/Zendesk/API/Resources/Core/OrganizationMemberships.php +// src/Zendesk/API/Resources/Core/Organizations.php +// src/Zendesk/API/Resources/Core/OrganizationSubscriptions.php +// src/Zendesk/API/Resources/Core/OrganizationTickets.php +// src/Zendesk/API/Resources/Core/RequestComments.php +// src/Zendesk/API/Resources/Core/Requests.php +// src/Zendesk/API/Resources/Core/SatisfactionRatings.php +// src/Zendesk/API/Resources/Core/Sessions.php +// src/Zendesk/API/Resources/Core/SharingAgreements.php +// src/Zendesk/API/Resources/Core/SlaPolicies.php +// src/Zendesk/API/Resources/Core/SupportAddresses.php +// src/Zendesk/API/Resources/Core/SuspendedTickets.php +// src/Zendesk/API/Resources/Core/Tags.php +// src/Zendesk/API/Resources/Core/Targets.php +// src/Zendesk/API/Resources/Core/TicketAudits.php +// src/Zendesk/API/Resources/Core/TicketComments.php +// src/Zendesk/API/Resources/Core/TicketFields.php +// src/Zendesk/API/Resources/Core/TicketFieldsOptions.php +// src/Zendesk/API/Resources/Core/TicketForms.php +// src/Zendesk/API/Resources/Core/TicketMetrics.php +// src/Zendesk/API/Resources/Core/Tickets.php +// src/Zendesk/API/Resources/Core/Translations.php +// src/Zendesk/API/Resources/Core/Triggers.php +// src/Zendesk/API/Resources/Core/TwitterHandles.php +// src/Zendesk/API/Resources/Core/UserFields.php +// src/Zendesk/API/Resources/Core/UserIdentities.php +// src/Zendesk/API/Resources/Core/Users.php +// src/Zendesk/API/Resources/Core/Views.php +// src/Zendesk/API/Resources/Core/Webhooks.php +// src/Zendesk/API/Resources/HelpCenter/Articles.php +// src/Zendesk/API/Resources/HelpCenter/Categories.php +// src/Zendesk/API/Resources/HelpCenter/Sections.php +// src/Zendesk/API/Resources/Voice/PhoneNumbers.php +// src/Zendesk/API/Traits/Resource/Defaults.php From 5922fb6f32c6607ad1d1303f233227f430dbd2b0 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Thu, 26 Oct 2023 08:08:42 +1000 Subject: [PATCH 09/18] Add FindAll iterator() --- src/Zendesk/API/Resources/Core/Tickets.php | 21 +++------------- src/Zendesk/API/Traits/Resource/FindAll.php | 28 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index b6aec308..5388a167 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -12,8 +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\Pagination\CbpStrategy; -use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; /** * The Tickets class exposes key methods for reading and updating ticket data @@ -46,21 +44,6 @@ class Tickets extends ResourceAbstract */ protected $lastAttachments = []; - /** - * Usage: - * foreach ($ticketsIterator as $ticket) { - * process($ticket) - * } - * - * @return PaginationIterator to fetch all pages. - */ - public function iterator() - { - // TODO: default page size everywhere - $strategy = new CbpStrategy($this, 'tickets', 2); - return new PaginationIterator($strategy); - } - /** * {@inheritdoc} */ @@ -474,4 +457,8 @@ public function merge(array $params = []) return $response; } + + protected function paginatedPath() { + return 'tickets'; + } } diff --git a/src/Zendesk/API/Traits/Resource/FindAll.php b/src/Zendesk/API/Traits/Resource/FindAll.php index 30dd22ae..e1b0b18f 100644 --- a/src/Zendesk/API/Traits/Resource/FindAll.php +++ b/src/Zendesk/API/Traits/Resource/FindAll.php @@ -3,6 +3,8 @@ namespace Zendesk\API\Traits\Resource; use Zendesk\API\Exceptions\RouteException; +use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; +use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; trait FindAll { @@ -35,4 +37,30 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__) $params ); } + + // TODO: own trait? + /** + * Usage: + * foreach ($ticketsIterator as $ticket) { + * process($ticket) + * } + * + * @return PaginationIterator to fetch all pages. + */ + public function iterator() + { + $strategyClass = $this->paginationStrategyClass(); + $strategy = new $strategyClass($this, $this->paginatedPath(), 2); + return new PaginationIterator($strategy); + } + + + private function paginationStrategyClass() { + return CbpStrategy::class; + } + + // TODO: abstract + protected function paginatedPath() { + return "/"; + } } From aec89057042fbfcbe04ce86a53d4abbce35e5671 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Thu, 26 Oct 2023 08:19:19 +1000 Subject: [PATCH 10/18] FindAll iterator() using $objectNamePlural --- .../API/Resources/Core/AppInstallations.php | 5 +++++ src/Zendesk/API/Resources/Core/Tickets.php | 4 ---- src/Zendesk/API/Resources/Core/Users.php | 14 -------------- src/Zendesk/API/Traits/Resource/FindAll.php | 10 +++++----- .../API/Traits/Utility/Pagination/ObpStrategy.php | 6 ++++++ .../Utility/Pagination/PaginationIterator.php | 1 + 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/Zendesk/API/Resources/Core/AppInstallations.php b/src/Zendesk/API/Resources/Core/AppInstallations.php index b774b87a..004deff6 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\ObpStrategy; /** * The AppInstallations class exposes methods seen at @@ -123,4 +124,8 @@ public function update($id = null, array $updateResourceFields = [], $routeKey = $updateResourceFields ); } + + private function paginationStrategyClass() { + return ObpStrategy::class; + } } diff --git a/src/Zendesk/API/Resources/Core/Tickets.php b/src/Zendesk/API/Resources/Core/Tickets.php index 5388a167..e109ebed 100755 --- a/src/Zendesk/API/Resources/Core/Tickets.php +++ b/src/Zendesk/API/Resources/Core/Tickets.php @@ -457,8 +457,4 @@ public function merge(array $params = []) return $response; } - - protected function paginatedPath() { - return 'tickets'; - } } diff --git a/src/Zendesk/API/Resources/Core/Users.php b/src/Zendesk/API/Resources/Core/Users.php index 58ad367c..1840f300 100755 --- a/src/Zendesk/API/Resources/Core/Users.php +++ b/src/Zendesk/API/Resources/Core/Users.php @@ -50,20 +50,6 @@ class Users extends ResourceAbstract */ protected $identities; - /** - * Usage: - * foreach ($usersIterator as $user) { - * process($user) - * } - * - * @return PaginationIterator to fetch all pages. - */ - public function iterator() - { - $strategy = new CbpStrategy($this, 'users', 2); - return new PaginationIterator($strategy); - } - /** * {@inheritdoc} */ diff --git a/src/Zendesk/API/Traits/Resource/FindAll.php b/src/Zendesk/API/Traits/Resource/FindAll.php index e1b0b18f..15564c66 100644 --- a/src/Zendesk/API/Traits/Resource/FindAll.php +++ b/src/Zendesk/API/Traits/Resource/FindAll.php @@ -38,7 +38,8 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__) ); } - // TODO: own trait? + // TODO: own trait + // TODO: page size 100 /** * Usage: * foreach ($ticketsIterator as $ticket) { @@ -50,7 +51,7 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__) public function iterator() { $strategyClass = $this->paginationStrategyClass(); - $strategy = new $strategyClass($this, $this->paginatedPath(), 2); + $strategy = new $strategyClass($this, $this->resourcesRoot(), 2); return new PaginationIterator($strategy); } @@ -59,8 +60,7 @@ private function paginationStrategyClass() { return CbpStrategy::class; } - // TODO: abstract - protected function paginatedPath() { - return "/"; + protected function resourcesRoot() { + return $this->objectNamePlural; } } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php index fd703c89..cd15990a 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -2,6 +2,11 @@ namespace Zendesk\API\Traits\Utility\Pagination; + +/** + * Offset Based Pagination + * Can also be used for no pagination + */ class ObpStrategy extends PaginationStrategy { private $pageNumber = 0; @@ -11,6 +16,7 @@ public function getPage() ++$this->pageNumber; $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; $response = $this->resourcesRoot->findAll($params); + return $response->{$this->resourcesKey}; } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php index bc553aeb..c44681ca 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -48,6 +48,7 @@ private function getPageIfNecessary() return; } + // TODO: don't keep all pages $this->page = array_merge($this->page, $this->strategy->getPage()); } From 3191160f71f0cf086ee1011dccf8eaaa217ec844 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Thu, 26 Oct 2023 15:31:29 +1000 Subject: [PATCH 11/18] SinglePageStrategy --- README.md | 2 +- docker-compose.yml | 1 + .../API/Resources/Core/AppInstallations.php | 4 +-- src/Zendesk/API/Resources/Core/Users.php | 2 -- src/Zendesk/API/Traits/Resource/FindAll.php | 2 +- .../Traits/Utility/Pagination/CbpStrategy.php | 10 ++++++- .../Traits/Utility/Pagination/ObpStrategy.php | 11 ++++++-- .../Utility/Pagination/PaginationIterator.php | 21 ++++++++------ .../Utility/Pagination/PaginationStrategy.php | 2 +- .../Utility/Pagination/SinglePageStrategy.php | 24 ++++++++++++++++ .../UnitTests/Core/AppInstallationsTest.php | 28 +++++++++++++++++++ .../API/UnitTests/Core/AutomationsTest.php | 1 + .../API/UnitTests/Core/TicketsTest.php | 3 -- 13 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php diff --git a/README.md b/README.md index 62aa961f..343299ec 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 90e70f03..0d912cb5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/src/Zendesk/API/Resources/Core/AppInstallations.php b/src/Zendesk/API/Resources/Core/AppInstallations.php index 004deff6..e6dfaf4e 100644 --- a/src/Zendesk/API/Resources/Core/AppInstallations.php +++ b/src/Zendesk/API/Resources/Core/AppInstallations.php @@ -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 @@ -126,6 +126,6 @@ public function update($id = null, array $updateResourceFields = [], $routeKey = } private function paginationStrategyClass() { - return ObpStrategy::class; + return SinglePageStrategy::class; } } diff --git a/src/Zendesk/API/Resources/Core/Users.php b/src/Zendesk/API/Resources/Core/Users.php index 1840f300..00273254 100755 --- a/src/Zendesk/API/Resources/Core/Users.php +++ b/src/Zendesk/API/Resources/Core/Users.php @@ -14,8 +14,6 @@ use Zendesk\API\Traits\Resource\MultipartUpload; use Zendesk\API\Traits\Resource\UpdateMany; use Zendesk\API\Traits\Utility\InstantiatorTrait; -use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; -use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; /** * The Users class exposes user management methods diff --git a/src/Zendesk/API/Traits/Resource/FindAll.php b/src/Zendesk/API/Traits/Resource/FindAll.php index 15564c66..ed2c56f9 100644 --- a/src/Zendesk/API/Traits/Resource/FindAll.php +++ b/src/Zendesk/API/Traits/Resource/FindAll.php @@ -24,7 +24,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/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index 9057d872..e12093de 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -15,11 +15,19 @@ public function getPage() $params['page[after]'] = $this->afterCursor; } $response = $this->resourcesRoot->findAll($params); + + // TODO: remove + // echo "\npage ids: "; + // foreach ($response->tickets as $ticket) { + // echo $ticket->id . " "; + // } + $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; return $response->{$this->resourcesKey}; } - public function isEndOfPage() { + public function shouldGetPage($position) { + // TODO: calc 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 index cd15990a..9f5fa748 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -5,7 +5,6 @@ /** * Offset Based Pagination - * Can also be used for no pagination */ class ObpStrategy extends PaginationStrategy { @@ -17,10 +16,16 @@ public function getPage() $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; $response = $this->resourcesRoot->findAll($params); + // TODO: remove + // echo "\npage ids: "; + // foreach ($response->{$this->resourcesRoot} as $item) { + // echo $item->id . " "; + // } + return $response->{$this->resourcesKey}; } - public function isEndOfPage() { - return true; // TODO: explain + 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 index c44681ca..12c3be53 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationIterator.php @@ -4,6 +4,10 @@ use Iterator; +// TODO: doc +// TODO: errors +// TODO: params +// TODO: sorting class PaginationIterator implements Iterator { private $position = 0; @@ -33,26 +37,27 @@ 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)) { + // TODO: remove + echo("\ngetPageIfNecessary NO. " . $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(); - } } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php index 5e3ebf7e..571fedd1 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php @@ -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); } 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 00000000..6a955457 --- /dev/null +++ b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php @@ -0,0 +1,24 @@ +started = true; + $response = $this->resourcesRoot->findAll(); + + return $response->{$this->resourcesKey}; + } + + public function shouldGetPage($position) { + return !$this->started; + } +} diff --git a/tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php b/tests/Zendesk/API/UnitTests/Core/AppInstallationsTest.php index 0b5fa987..dba89e96 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 5ed9d516..ddb16be5 100755 --- a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php @@ -16,6 +16,7 @@ class AutomationsTest extends BasicTest public function testActive() { $this->assertEndpointCalled(function () { + // TODO: let's test this $this->client->automations()->findActive(); }, 'automations/active.json'); } diff --git a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php index 066dcee7..726aaafa 100755 --- a/tests/Zendesk/API/UnitTests/Core/TicketsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/TicketsTest.php @@ -38,9 +38,6 @@ public function setUp() parent::setUp(); } - /** - * Test the iterator - */ public function testIterator() { $this->mockApiResponses([ From 5994a595c2cf5c433bd402b29dab85f977cb6929 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 13:56:33 +1000 Subject: [PATCH 12/18] Add AutomationsTest testIterator --- .../Traits/Utility/Pagination/CbpStrategy.php | 1 + .../API/UnitTests/Core/AutomationsTest.php | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index e12093de..b1217ecd 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -17,6 +17,7 @@ public function getPage() $response = $this->resourcesRoot->findAll($params); // TODO: remove + // print_r( $response); // echo "\npage ids: "; // foreach ($response->tickets as $ticket) { // echo $ticket->id . " "; diff --git a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php index ddb16be5..4a75313a 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,36 @@ */ 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']; + parent::setUp(); + } + + public function testIterator() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'automations' => [$this->testResource0, $this->testResource1], + 'meta' => ['has_more' => false], + + ])) + ]); + + $iterator = $this->client->automations()->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); + } + /** * Test we can use endpoint to get active automations From 3ef701d6027c82f4508a99c13a1b21a56551035b Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 14:07:21 +1000 Subject: [PATCH 13/18] add Traits/Resource/Pagination.php --- src/Zendesk/API/Traits/Resource/FindAll.php | 29 +-------------- .../API/Traits/Resource/Pagination.php | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 src/Zendesk/API/Traits/Resource/Pagination.php diff --git a/src/Zendesk/API/Traits/Resource/FindAll.php b/src/Zendesk/API/Traits/Resource/FindAll.php index ed2c56f9..1c826cc6 100644 --- a/src/Zendesk/API/Traits/Resource/FindAll.php +++ b/src/Zendesk/API/Traits/Resource/FindAll.php @@ -4,10 +4,11 @@ use Zendesk\API\Exceptions\RouteException; use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; -use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; trait FindAll { + use Pagination; + /** * List all of this resource * @@ -37,30 +38,4 @@ public function findAll(array $params = [], $routeKey = __FUNCTION__) $params ); } - - // TODO: own trait - // TODO: page size 100 - /** - * Usage: - * foreach ($ticketsIterator as $ticket) { - * process($ticket) - * } - * - * @return PaginationIterator to fetch all pages. - */ - public function iterator() - { - $strategyClass = $this->paginationStrategyClass(); - $strategy = new $strategyClass($this, $this->resourcesRoot(), 2); - return new PaginationIterator($strategy); - } - - - private function paginationStrategyClass() { - return CbpStrategy::class; - } - - protected function resourcesRoot() { - return $this->objectNamePlural; - } } diff --git a/src/Zendesk/API/Traits/Resource/Pagination.php b/src/Zendesk/API/Traits/Resource/Pagination.php new file mode 100644 index 00000000..2fd18ed4 --- /dev/null +++ b/src/Zendesk/API/Traits/Resource/Pagination.php @@ -0,0 +1,37 @@ +paginationStrategyClass(); + $strategy = new $strategyClass($this, $this->resourcesRoot(), $this->defaultPageSize()); + return new PaginationIterator($strategy); + } + + private function defaultPageSize() + { + return 100; + } + + private function paginationStrategyClass() { + return CbpStrategy::class; + } + + protected function resourcesRoot() { + return $this->objectNamePlural; + } +} From 8aad52836f247c2a950e5e73d43b34816c79ebe8 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 14:15:23 +1000 Subject: [PATCH 14/18] Rename Traits/Utility/Pagination/PaginationStrategy.php to AbstractStrategy --- .../{PaginationStrategy.php => AbstractStrategy.php} | 3 +-- .../API/Traits/Utility/Pagination/CbpStrategy.php | 2 +- .../API/Traits/Utility/Pagination/ObpStrategy.php | 2 +- .../API/Traits/Utility/Pagination/PaginationIterator.php | 9 +-------- .../API/Traits/Utility/Pagination/SinglePageStrategy.php | 2 +- tests/Zendesk/API/UnitTests/Core/AutomationsTest.php | 1 - 6 files changed, 5 insertions(+), 14 deletions(-) rename src/Zendesk/API/Traits/Utility/Pagination/{PaginationStrategy.php => AbstractStrategy.php} (89%) diff --git a/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php similarity index 89% rename from src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php rename to src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php index 571fedd1..bf448bcc 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/PaginationStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -1,8 +1,7 @@ strategy = $strategy; } @@ -52,12 +48,9 @@ public function current() private function getPageIfNecessary() { if (!$this->strategy->shouldGetPage($this->position)) { - // TODO: remove - echo("\ngetPageIfNecessary NO. " . $this->position); return; } - // TODO: don't keep all pages $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 index 6a955457..07bb96f1 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php @@ -6,7 +6,7 @@ /** * Single Page (no pagination) */ -class SinglePageStrategy extends PaginationStrategy +class SinglePageStrategy extends AbstractStrategy { protected $started = false; diff --git a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php index 4a75313a..92b8a938 100755 --- a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php @@ -47,7 +47,6 @@ public function testIterator() public function testActive() { $this->assertEndpointCalled(function () { - // TODO: let's test this $this->client->automations()->findActive(); }, 'automations/active.json'); } From fdd8bd1bb5fbd96b855834c3075571099a388ffb Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 14:25:49 +1000 Subject: [PATCH 15/18] Reuse AbstractStrategy::DEFAULT_PAGE_SIZE --- src/Zendesk/API/Traits/Resource/Pagination.php | 10 +++------- .../API/Traits/Utility/Pagination/AbstractStrategy.php | 3 ++- .../API/Traits/Utility/Pagination/CbpStrategy.php | 8 -------- .../API/Traits/Utility/Pagination/ObpStrategy.php | 6 ------ 4 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/Zendesk/API/Traits/Resource/Pagination.php b/src/Zendesk/API/Traits/Resource/Pagination.php index 2fd18ed4..b9a19965 100644 --- a/src/Zendesk/API/Traits/Resource/Pagination.php +++ b/src/Zendesk/API/Traits/Resource/Pagination.php @@ -2,6 +2,7 @@ namespace Zendesk\API\Traits\Resource; +use Zendesk\API\Traits\Utility\Pagination\AbstractStrategy; use Zendesk\API\Traits\Utility\Pagination\CbpStrategy; use Zendesk\API\Traits\Utility\Pagination\PaginationIterator; @@ -18,16 +19,11 @@ trait Pagination { public function iterator() { $strategyClass = $this->paginationStrategyClass(); - $strategy = new $strategyClass($this, $this->resourcesRoot(), $this->defaultPageSize()); + $strategy = new $strategyClass($this, $this->resourcesRoot(), AbstractStrategy::DEFAULT_PAGE_SIZE); return new PaginationIterator($strategy); } - private function defaultPageSize() - { - return 100; - } - - private function paginationStrategyClass() { + protected function paginationStrategyClass() { return CbpStrategy::class; } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php index bf448bcc..7b2d8288 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -3,7 +3,8 @@ abstract class AbstractStrategy { - public const DEFAULT_PAGE_SIZE = 100; + // TODO: 100 + public const DEFAULT_PAGE_SIZE = 2; protected $resourcesRoot; protected $resourcesKey; diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index 2ed159a3..73d50e9a 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -16,19 +16,11 @@ public function getPage() } $response = $this->resourcesRoot->findAll($params); - // TODO: remove - // print_r( $response); - // echo "\npage ids: "; - // foreach ($response->tickets as $ticket) { - // echo $ticket->id . " "; - // } - $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; return $response->{$this->resourcesKey}; } public function shouldGetPage($position) { - // TODO: calc 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 index d741ed5f..16b68db8 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -16,12 +16,6 @@ public function getPage() $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; $response = $this->resourcesRoot->findAll($params); - // TODO: remove - // echo "\npage ids: "; - // foreach ($response->{$this->resourcesRoot} as $item) { - // echo $item->id . " "; - // } - return $response->{$this->resourcesKey}; } From 3bd81234b1b045a585273ad69933c90882ef1905 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 15:03:49 +1000 Subject: [PATCH 16/18] Rename trait Pagination resourcesRoot clientResources --- docker-compose.yml | 2 +- .../API/Resources/Core/CustomRoles.php | 5 +++ .../API/Traits/Resource/Pagination.php | 7 +++- .../Utility/Pagination/AbstractStrategy.php | 13 +++++-- .../Traits/Utility/Pagination/CbpStrategy.php | 2 +- .../Traits/Utility/Pagination/ObpStrategy.php | 8 +++- .../Utility/Pagination/SinglePageStrategy.php | 2 +- .../API/UnitTests/Core/AutomationsTest.php | 12 ++++-- .../API/UnitTests/Core/CustomRolesTest.php | 37 +++++++++++++++++++ 9 files changed, 75 insertions(+), 13 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0d912cb5..bf81bf3f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +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 + # command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php volumes: vendor: diff --git a/src/Zendesk/API/Resources/Core/CustomRoles.php b/src/Zendesk/API/Resources/Core/CustomRoles.php index 3fa3ccd0..32174b3b 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\ObpStrategy; /** * Class CustomRoles @@ -12,4 +13,8 @@ class CustomRoles extends ResourceAbstract { use FindAll; + + protected function paginationStrategyClass() { + return ObpStrategy::class; + } } diff --git a/src/Zendesk/API/Traits/Resource/Pagination.php b/src/Zendesk/API/Traits/Resource/Pagination.php index b9a19965..ea974aa5 100644 --- a/src/Zendesk/API/Traits/Resource/Pagination.php +++ b/src/Zendesk/API/Traits/Resource/Pagination.php @@ -19,7 +19,7 @@ trait Pagination { public function iterator() { $strategyClass = $this->paginationStrategyClass(); - $strategy = new $strategyClass($this, $this->resourcesRoot(), AbstractStrategy::DEFAULT_PAGE_SIZE); + $strategy = new $strategyClass($this, $this->resourcesKey(), AbstractStrategy::DEFAULT_PAGE_SIZE); return new PaginationIterator($strategy); } @@ -27,7 +27,10 @@ protected function paginationStrategyClass() { return CbpStrategy::class; } - protected function resourcesRoot() { + /* + * @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 index 7b2d8288..a8805014 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -6,13 +6,20 @@ abstract class AbstractStrategy // TODO: 100 public const DEFAULT_PAGE_SIZE = 2; - protected $resourcesRoot; + /* + * The object handling the list, Ie: `$client->{clientResources}()` + */ + protected $clientResources; + + /* + * The response key where the data is returned + */ protected $resourcesKey; protected $pageSize; - public function __construct($resourcesRoot, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) + public function __construct($clientResources, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) { - $this->resourcesRoot = $resourcesRoot; + $this->clientResources = $clientResources; $this->resourcesKey = $resourcesKey; $this->pageSize = $pageSize; } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index 73d50e9a..24562ec9 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -14,7 +14,7 @@ public function getPage() if ($this->afterCursor) { $params['page[after]'] = $this->afterCursor; } - $response = $this->resourcesRoot->findAll($params); + $response = $this->clientResources->findAll($params); $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; return $response->{$this->resourcesKey}; diff --git a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php index 16b68db8..97bb1da2 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -14,8 +14,12 @@ public function getPage() { ++$this->pageNumber; $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; - $response = $this->resourcesRoot->findAll($params); - + $response = $this->clientResources->findAll($params); + // TODO: remove + // echo "\npage ids: "; + // foreach ($response->{$this->resourcesKey} as $item) { + // echo $item->id . " "; + // } return $response->{$this->resourcesKey}; } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php index 07bb96f1..3e6c9ae3 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php @@ -13,7 +13,7 @@ class SinglePageStrategy extends AbstractStrategy public function getPage() { $this->started = true; - $response = $this->resourcesRoot->findAll(); + $response = $this->clientResources->findAll(); return $response->{$this->resourcesKey}; } diff --git a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php index 92b8a938..9e61ce9d 100755 --- a/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php +++ b/tests/Zendesk/API/UnitTests/Core/AutomationsTest.php @@ -18,6 +18,7 @@ public function setUp() { $this->testResource0 = ['anyField' => 'Any field 0']; $this->testResource1 = ['anyField' => 'Any field 1']; + $this->testResource2 = ['anyField' => 'Any field 2']; parent::setUp(); } @@ -27,20 +28,25 @@ public function testIterator() $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(2, $actual); + $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 f6c14ef8..10e0a632 100644 --- a/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php +++ b/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php @@ -2,10 +2,47 @@ 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() + { + // CBP + $this->mockApiResponses([ + new Response(200, [], json_encode([ + 'custom_roles' => [$this->testResource0, $this->testResource1], + 'meta' => ['after_cursor' => '', 'has_more' => true], + + ])), + new Response(200, [], json_encode([ + 'custom_roles' => [$this->testResource2], + 'meta' => ['has_more' => false], + + ])), + ]); + + $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')); From 72737d8123190f13b85160abc48b28f3d942720d Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 15:05:19 +1000 Subject: [PATCH 17/18] Rename trait Pagination resourcesRoot to clientList --- .../API/Traits/Utility/Pagination/AbstractStrategy.php | 8 ++++---- src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php | 2 +- src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php | 2 +- .../API/Traits/Utility/Pagination/SinglePageStrategy.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php index a8805014..8253a189 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -7,9 +7,9 @@ abstract class AbstractStrategy public const DEFAULT_PAGE_SIZE = 2; /* - * The object handling the list, Ie: `$client->{clientResources}()` + * @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()` */ - protected $clientResources; + protected $clientList; /* * The response key where the data is returned @@ -17,9 +17,9 @@ abstract class AbstractStrategy protected $resourcesKey; protected $pageSize; - public function __construct($clientResources, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) + public function __construct($clientList, $resourcesKey, $pageSize = self::DEFAULT_PAGE_SIZE) { - $this->clientResources = $clientResources; + $this->clientList = $clientList; $this->resourcesKey = $resourcesKey; $this->pageSize = $pageSize; } diff --git a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php index 24562ec9..bdfeff57 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php @@ -14,7 +14,7 @@ public function getPage() if ($this->afterCursor) { $params['page[after]'] = $this->afterCursor; } - $response = $this->clientResources->findAll($params); + $response = $this->clientList->findAll($params); $this->afterCursor = $response->meta->has_more ? $response->meta->after_cursor : null; return $response->{$this->resourcesKey}; diff --git a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php index 97bb1da2..32371f55 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -14,7 +14,7 @@ public function getPage() { ++$this->pageNumber; $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; - $response = $this->clientResources->findAll($params); + $response = $this->clientList->findAll($params); // TODO: remove // echo "\npage ids: "; // foreach ($response->{$this->resourcesKey} as $item) { diff --git a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php index 3e6c9ae3..d82d5269 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/SinglePageStrategy.php @@ -13,7 +13,7 @@ class SinglePageStrategy extends AbstractStrategy public function getPage() { $this->started = true; - $response = $this->clientResources->findAll(); + $response = $this->clientList->findAll(); return $response->{$this->resourcesKey}; } From 48ac3f6e63bfe9fb7267ee728b4e49e043280a63 Mon Sep 17 00:00:00 2001 From: "Erik Trapin (ecoologic)" Date: Mon, 30 Oct 2023 15:14:18 +1000 Subject: [PATCH 18/18] Add testIterator to resources --- docker-compose.yml | 2 +- .../API/Resources/Core/CustomRoles.php | 4 +- .../API/Resources/Core/SharingAgreements.php | 6 +++ src/Zendesk/API/Resources/Core/Targets.php | 5 ++ .../Utility/Pagination/AbstractStrategy.php | 5 +- .../Traits/Utility/Pagination/ObpStrategy.php | 6 +-- .../API/UnitTests/Core/CustomRolesTest.php | 12 ++--- .../Zendesk/API/UnitTests/Core/MacrosTest.php | 38 +++++++++++++ .../Core/OrganizationMembershipsTest.php | 38 +++++++++++++ .../API/UnitTests/Core/OrganizationsTest.php | 38 +++++++++++++ .../API/UnitTests/Core/RequestsTest.php | 38 +++++++++++++ .../Core/SatisfactionRatingsTest.php | 38 +++++++++++++ tests/Zendesk/API/UnitTests/Core/TagsTest.php | 38 +++++++++++++ .../Traits/Utility/PaginationTest.php | 54 ------------------- 14 files changed, 248 insertions(+), 74 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index bf81bf3f..e1ac1d78 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +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/CustomRolesTest.php + # command: vendor/bin/phpunit tests/Zendesk/API/UnitTests/Core/OrganizationMembershipsTest.php volumes: vendor: diff --git a/src/Zendesk/API/Resources/Core/CustomRoles.php b/src/Zendesk/API/Resources/Core/CustomRoles.php index 32174b3b..5090ba47 100644 --- a/src/Zendesk/API/Resources/Core/CustomRoles.php +++ b/src/Zendesk/API/Resources/Core/CustomRoles.php @@ -4,7 +4,7 @@ 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; /** * Class CustomRoles @@ -15,6 +15,6 @@ class CustomRoles extends ResourceAbstract use FindAll; protected function paginationStrategyClass() { - return ObpStrategy::class; + return SinglePageStrategy::class; } } diff --git a/src/Zendesk/API/Resources/Core/SharingAgreements.php b/src/Zendesk/API/Resources/Core/SharingAgreements.php index 188ca61c..ddd7495a 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 5fffb1dc..99ad029f 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/Traits/Utility/Pagination/AbstractStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php index 8253a189..5f5a0a34 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/AbstractStrategy.php @@ -3,8 +3,7 @@ abstract class AbstractStrategy { - // TODO: 100 - public const DEFAULT_PAGE_SIZE = 2; + public const DEFAULT_PAGE_SIZE = 100; /* * @var mixed use trait FindAll. The object handling the list, Ie: `$client->{clientList}()` @@ -12,7 +11,7 @@ abstract class AbstractStrategy protected $clientList; /* - * The response key where the data is returned + * @var string The response key where the data is returned */ protected $resourcesKey; protected $pageSize; diff --git a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php index 32371f55..83dd3cc6 100644 --- a/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php +++ b/src/Zendesk/API/Traits/Utility/Pagination/ObpStrategy.php @@ -15,11 +15,7 @@ public function getPage() ++$this->pageNumber; $params = ['page' => $this->pageNumber, 'page_size' => $this->pageSize]; $response = $this->clientList->findAll($params); - // TODO: remove - // echo "\npage ids: "; - // foreach ($response->{$this->resourcesKey} as $item) { - // echo $item->id . " "; - // } + return $response->{$this->resourcesKey}; } diff --git a/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php b/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php index 10e0a632..0d78211a 100644 --- a/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php +++ b/tests/Zendesk/API/UnitTests/Core/CustomRolesTest.php @@ -21,18 +21,12 @@ public function setUp() public function testIterator() { - // CBP + // Single Page $this->mockApiResponses([ new Response(200, [], json_encode([ - 'custom_roles' => [$this->testResource0, $this->testResource1], - 'meta' => ['after_cursor' => '', 'has_more' => true], + 'custom_roles' => [$this->testResource0, $this->testResource1, $this->testResource2] - ])), - new Response(200, [], json_encode([ - 'custom_roles' => [$this->testResource2], - 'meta' => ['has_more' => false], - - ])), + ])) ]); $iterator = $this->client->customRoles()->iterator(); diff --git a/tests/Zendesk/API/UnitTests/Core/MacrosTest.php b/tests/Zendesk/API/UnitTests/Core/MacrosTest.php index 3f17c0f4..da581824 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 308f46ad..8b116302 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 ef753946..c0c1051b 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 8ed12b21..dd321f25 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 b218f0d3..339c24e5 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 fc9e73ed..3796b318 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/Traits/Utility/PaginationTest.php b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php index 315e878a..0ac8fda8 100644 --- a/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php +++ b/tests/Zendesk/API/UnitTests/Traits/Utility/PaginationTest.php @@ -75,57 +75,3 @@ public function testFetchesUsers() ], $users); } } - -// agl "use (FindAll|Default)" src -// src/Zendesk/API/Resources/Core/Activities.php -// src/Zendesk/API/Resources/Core/AppInstallationLocations.php -// src/Zendesk/API/Resources/Core/AppInstallations.php -// src/Zendesk/API/Resources/Core/AppLocations.php -// src/Zendesk/API/Resources/Core/AuditLogs.php -// src/Zendesk/API/Resources/Core/Automations.php -// src/Zendesk/API/Resources/Core/Bookmarks.php -// src/Zendesk/API/Resources/Core/Brands.php -// src/Zendesk/API/Resources/Core/CustomRoles.php -// src/Zendesk/API/Resources/Core/DynamicContentItems.php -// src/Zendesk/API/Resources/Core/DynamicContentItemVariants.php -// src/Zendesk/API/Resources/Core/GroupMemberships.php -// src/Zendesk/API/Resources/Core/Groups.php -// src/Zendesk/API/Resources/Core/Locales.php -// src/Zendesk/API/Resources/Core/Macros.php -// src/Zendesk/API/Resources/Core/OAuthClients.php -// src/Zendesk/API/Resources/Core/OAuthTokens.php -// src/Zendesk/API/Resources/Core/OrganizationFields.php -// src/Zendesk/API/Resources/Core/OrganizationMemberships.php -// src/Zendesk/API/Resources/Core/Organizations.php -// src/Zendesk/API/Resources/Core/OrganizationSubscriptions.php -// src/Zendesk/API/Resources/Core/OrganizationTickets.php -// src/Zendesk/API/Resources/Core/RequestComments.php -// src/Zendesk/API/Resources/Core/Requests.php -// src/Zendesk/API/Resources/Core/SatisfactionRatings.php -// src/Zendesk/API/Resources/Core/Sessions.php -// src/Zendesk/API/Resources/Core/SharingAgreements.php -// src/Zendesk/API/Resources/Core/SlaPolicies.php -// src/Zendesk/API/Resources/Core/SupportAddresses.php -// src/Zendesk/API/Resources/Core/SuspendedTickets.php -// src/Zendesk/API/Resources/Core/Tags.php -// src/Zendesk/API/Resources/Core/Targets.php -// src/Zendesk/API/Resources/Core/TicketAudits.php -// src/Zendesk/API/Resources/Core/TicketComments.php -// src/Zendesk/API/Resources/Core/TicketFields.php -// src/Zendesk/API/Resources/Core/TicketFieldsOptions.php -// src/Zendesk/API/Resources/Core/TicketForms.php -// src/Zendesk/API/Resources/Core/TicketMetrics.php -// src/Zendesk/API/Resources/Core/Tickets.php -// src/Zendesk/API/Resources/Core/Translations.php -// src/Zendesk/API/Resources/Core/Triggers.php -// src/Zendesk/API/Resources/Core/TwitterHandles.php -// src/Zendesk/API/Resources/Core/UserFields.php -// src/Zendesk/API/Resources/Core/UserIdentities.php -// src/Zendesk/API/Resources/Core/Users.php -// src/Zendesk/API/Resources/Core/Views.php -// src/Zendesk/API/Resources/Core/Webhooks.php -// src/Zendesk/API/Resources/HelpCenter/Articles.php -// src/Zendesk/API/Resources/HelpCenter/Categories.php -// src/Zendesk/API/Resources/HelpCenter/Sections.php -// src/Zendesk/API/Resources/Voice/PhoneNumbers.php -// src/Zendesk/API/Traits/Resource/Defaults.php