Skip to content

Commit

Permalink
CbpStrategy throw PaginationError
Browse files Browse the repository at this point in the history
  • Loading branch information
ecoologic committed Nov 15, 2023
1 parent 7bc3f8f commit 7b61553
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/Zendesk/API/Traits/Utility/Pagination/CbpStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function page($getPageFn)
{
$this->started = true;
$this->latestResponse = $getPageFn();
if (!isset($this->latestResponse->meta->has_more)) {
throw new PaginationError(
"Response is not CBP, if you think your request is correct, please open an issue at https://github.com/zendesk/zendesk_api_client_php/issues"
);
}
$this->hasMore = $this->latestResponse->meta->has_more;
if (isset($this->latestResponse->meta->after_cursor)) {
$this->afterCursor = $this->latestResponse->meta->after_cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Zendesk\API\Traits\Utility\Pagination;

class PaginationError extends \Exception {}

const DEFAULT_PAGE_SIZE = 100;

use Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
use GuzzleHttp\Psr7\Response;
use Zendesk\API\Exceptions\ApiResponseException;
use Zendesk\API\Traits\Utility\Pagination\CbpStrategy;
use Zendesk\API\Traits\Utility\Pagination\PaginationError;
use Zendesk\API\Traits\Utility\Pagination\SinglePageStrategy;
use Zendesk\API\UnitTests\BasicTest;
use Zendesk\API\Traits\Utility\Pagination\PaginationIterator;

class MockResource {
public $params;
public $foundDifferent = false;
public $isObp = false;
public $errorMessage;
public $response;
private $resources;
private $resourceName;
private $callCount = 0;
private $errorMessage;

public function __construct($resourceName, $resources, $errorMessage = null)
public function __construct($resourceName, $resources)
{
$this->resourceName = $resourceName;
$this->resources = $resources;
$this->callCount = 0;
$this->errorMessage = $errorMessage;
}

public function findAll($params)
Expand All @@ -35,24 +36,30 @@ public function findAll($params)
$this->response = new Response(400, [], '{ "a": "json"}');
$requestException = new RequestException($this->errorMessage, $request, $this->response);
throw new ApiResponseException($requestException);
} else if ($this->isObp) {
$this->response = (object) [
$this->resourceName => $this->resources[0],
// No CBP meta and links
];
} else {
// Simulate two pages of resources
$resources = $this->callCount === 0
? $this->resources[0]
: $this->resources[1];

// Simulate a cursor for the next page on the first call
$afterCursor = $this->callCount === 0 ? 'cursor_for_next_page' : null;

$this->callCount++;
$this->params = $params;
$this->response = (object) [
$this->resourceName => $resources,
'meta' => (object) [
'has_more' => $afterCursor !== null,
'after_cursor' => $afterCursor,
],
];
}
// Simulate two pages of resources
$resources = $this->callCount === 0
? $this->resources[0]
: $this->resources[1];

// Simulate a cursor for the next page on the first call
$afterCursor = $this->callCount === 0 ? 'cursor_for_next_page' : null;

$this->callCount++;
$this->params = $params;
$this->response = (object) [
$this->resourceName => $resources,
'meta' => (object) [
'has_more' => $afterCursor !== null,
'after_cursor' => $afterCursor,
],
];

return $this->response;
}
Expand Down Expand Up @@ -178,7 +185,8 @@ public function testHandlesError()
$expectedErrorMessage = "BOOM!";
$resultsKey = 'results';
$userParams = [];
$mockResults = new MockResource($resultsKey, [], $expectedErrorMessage);
$mockResults = new MockResource($resultsKey, []);
$mockResults->errorMessage = $expectedErrorMessage;
$strategy = new CbpStrategy($resultsKey, $userParams);
$iterator = new PaginationIterator($mockResults, $strategy);

Expand All @@ -191,4 +199,23 @@ public function testHandlesError()
$this->assertEquals($expectedErrorMessage, $error->getMessage());
$this->assertEquals([], $error->getErrorDetails());
}

public function testErrorsForWrongPagination()
{
$mockTickets = new MockResource('tickets', [
[['id' => 1], ['id' => 2]],
[['id' => 3], ['id' => 4]]
]);
$mockTickets->isObp = true;
$strategy = new CbpStrategy('tickets', ['page[size]' => 2]);
$iterator = new PaginationIterator($mockTickets, $strategy);

try {
iterator_to_array($iterator);
} catch (PaginationError $e) {
$error = $e;
}

$this->assertEquals("Response is not CBP, if you think your request is correct, please open an issue at https://github.com/zendesk/zendesk_api_client_php/issues", $error->getMessage());
}
}

0 comments on commit 7b61553

Please sign in to comment.