Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fixed cursor pagination when different page size is used. #348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/IntercomClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,14 @@ public function nextSearchPage(string $path, array $query, $pages)
* @param string $startingAfter
* @return stdClass
*/
public function nextCursorPage(string $path, string $startingAfter)
public function nextCursorPage(string $path, $pages)
{
return $this->get($path . "?starting_after=" . $startingAfter);
$queryParams = [
"starting_after" => $pages->next->starting_after,
"per_page" => $pages->per_page,
];

return $this->get($path, $queryParams);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/IntercomContacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public function nextSearch(array $query, $pages)
public function nextCursor($pages)
{
$path = 'contacts';
$starting_after = $pages->next->starting_after;
return $this->client->nextCursorPage($path, $starting_after);
return $this->client->nextCursorPage($path, $pages);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/IntercomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ public function testPaginationHelper()
}
}

public function testCursorHelper()
{
$httpClient = new Client();
$httpClient->addResponse(
new Response(200, ['X-Foo' => 'Bar'], "{\"foo\":\"bar\"}")
);

$client = new IntercomClient('u', 'p');
$client->setHttpClient($httpClient);

$pages = new stdClass;
$pages->per_page = 150;
$pages->next = new stdClass;
$pages->next->starting_after = "abc";

$client->nextCursorPage('path', $pages);

foreach ($httpClient->getRequests() as $request) {
$query = $request->getUri()->getQuery();
$this->assertSame("starting_after=abc&per_page=150", $query);
}
}

public function testRateLimitDetails()
{
date_default_timezone_set('UTC');
Expand Down
3 changes: 2 additions & 1 deletion tests/IntercomContactsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public function testContactNextSearch()
$this->assertSame('foo', $contacts->nextSearch([], $pages));
}

public function testConversationNextCursor()
public function testContactNextCursor()
{
$this->client->method('nextCursorPage')->willReturn('foo');
$query = [];
$pages = new stdClass;
$pages->per_page = 150;
$pages->next = new stdClass;
$pages->next->starting_after = "abc";

Expand Down