Skip to content
Merged
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
7 changes: 6 additions & 1 deletion src/Twilio/TokenPaginationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ abstract class TokenPaginationPage extends Page {
protected $nextToken;
protected $previousToken;
protected $url;
protected $queryParams;
protected $previousPageUrl;
protected $nextPageUrl;

Expand All @@ -56,11 +57,15 @@ public function __construct(Version $version, Response $response) {
$httpClient = $version->getDomain()->getClient()->getHttpClient();

$this->url = '';
$this->queryParams = [];
if ($httpClient->lastRequest) {
$fullUrl = $httpClient->lastRequest[CURLOPT_URL];
// remove query parameters from url
$parts = explode('?', $fullUrl);
$this->url = $parts[0];
if (count($parts) > 1) {
parse_str($parts[1], $this->queryParams); // store existing query params
}
}

$this->key = $this->getMeta('key');
Expand Down Expand Up @@ -91,7 +96,7 @@ protected function loadPage(): array {
* @return string The constructed query string.
*/
protected function getQueryString(?string $pageToken): string {
$params = [];
$params = $this->queryParams; // initialize with existing query params
if ($this->pageSize) {
$params['pageSize'] = $this->pageSize;
}
Expand Down
52 changes: 51 additions & 1 deletion tests/Twilio/Unit/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public function testStream(string $message, ?int $limit, ?int $pageLimit, int $e
* @dataProvider streamProvider
*/
public function testStreamWithTokenPagination(string $message, ?int $limit, ?int $pageLimit, int $expectedCount): void {
$uri = '/Accounts/AC123/Messages.json';

$this->curlClient->lastRequest = [
CURLOPT_URL => $uri
];

$this->curlClient
->method('request')
->will(self::onConsecutiveCalls(
Expand All @@ -218,7 +224,51 @@ public function testStreamWithTokenPagination(string $message, ?int $limit, ?int
}')
));

$response = $this->version->page('GET', '/Accounts/AC123/Messages.json');
$response = $this->version->page('GET', $uri);
$page = new TestTokenPage($this->version, $response);
$messages = $this->version->stream($page, $limit, $pageLimit);

self::assertEquals($expectedCount, \iterator_count($messages), "$message: Count does not match");
}

/**
* @param string $message Case message to display on assertion error
* @param int|null $limit Limit provided by the user
* @param int|null $pageLimit Page limit provided by the user
* @param int $expectedCount Expected record count returned by stream
* @dataProvider streamProvider
*/
public function testStreamWithTokenPaginationWithQueryParams(string $message, ?int $limit, ?int $pageLimit, int $expectedCount): void {
$uri = '/Accounts/AC123/Messages.json?param1=value1';

$this->curlClient->lastRequest = [
CURLOPT_URL => $uri
];

$this->curlClient
->method('request')
->will(self::onConsecutiveCalls(
new Response(
200,
'{
"meta": {"key": "messages", "pageSize": 2, "nextToken": "token1", "previousToken": null},
"messages": [{"body": "payload0"}, {"body": "payload1"}]
}'),
new Response(
200,
'{
"meta": {"key": "messages", "pageSize": 2, "nextToken": "token2", "previousToken": "token0"},
"messages": [{"body": "payload2"}, {"body": "payload3"}]
}'),
new Response(
200,
'{
"meta": {"key": "messages", "pageSize": 1, "nextToken": null, "previousToken": "token1"},
"messages": [{"body": "payload4"}]
}')
));

$response = $this->version->page('GET', $uri);
$page = new TestTokenPage($this->version, $response);
$messages = $this->version->stream($page, $limit, $pageLimit);

Expand Down
Loading