Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Fixes `getNextPage` and `getNextPageOfChildren` requesting the same page again when the previous request params already contained a cursor, and ignoring the `pageSize` argument when the previous params had a `page_size`, and sending both `before_id` and `after_id` when the previous params already contained the other cursor (thanks @pcbeingused33)

## v8.10.0 (2026-11-09)

- Adds `delivery_evidence` to `Tracker` and a `DeliveryEvidence` model
Expand Down
11 changes: 6 additions & 5 deletions lib/EasyPost/Service/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ protected function getNextPageResources(string $class, mixed $collection, ?int $
throw new EndOfPaginationException();
}

$params = [
'page_size' => $pageSize,
// The new cursor (and page size, if given) must win over the params of the previous request
$params = array_merge($userParams ?? [], [
'before_id' => $collectionArray[count($collectionArray) - 1]['id']
];
]);
unset($params['after_id']);

if (isset($userParams)) {
$params = array_merge($params, $userParams);
if (isset($pageSize)) {
$params['page_size'] = $pageSize;
}

$response = $this->allResources($class, $params);
Expand Down
12 changes: 7 additions & 5 deletions lib/EasyPost/Service/ReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function all(mixed $params = null): mixed
unset($params['type']);
$response = Requestor::request($this->client, HttpMethod::GET, $url, $params);
$response['type'] = $type;
$response['_params'] = $params;

return InternalUtil::convertToEasyPostObject($this->client, $response);
}
Expand All @@ -64,13 +65,14 @@ public function getNextPage(mixed $reports, ?int $pageSize = null): mixed
throw new EndOfPaginationException();
}

$params = [
'page_size' => $pageSize,
// The new cursor (and page size, if given) must win over the params of the previous request
$params = array_merge($userParams ?? [], [
'before_id' => $reportArray[count($reportArray) - 1]['id']
];
]);
unset($params['after_id']);

if (isset($userParams)) {
$params = array_merge($params, $userParams);
if (isset($pageSize)) {
$params['page_size'] = $pageSize;
}

$url = self::reportUrl($reportArray[0]->object);
Expand Down
11 changes: 6 additions & 5 deletions lib/EasyPost/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ public function getNextPageOfChildren(mixed $children, ?int $pageSize = null): m
throw new EndOfPaginationException();
}

$params = [
'page_size' => $pageSize,
// The new cursor (and page size, if given) must win over the params of the previous request
$params = array_merge($userParams ?? [], [
'after_id' => $userArray[count($userArray) - 1]['id']
];
]);
unset($params['before_id']);

if (isset($userParams)) {
$params = array_merge($params, $userParams);
if (isset($pageSize)) {
$params['page_size'] = $pageSize;
}

// Retrieve a page of users from the server
Expand Down
106 changes: 106 additions & 0 deletions test/EasyPost/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace EasyPost\Test;

use EasyPost\EasyPostClient;
use EasyPost\Test\Mocking\MockingUtility;
use EasyPost\Test\Mocking\MockRequest;
use EasyPost\Test\Mocking\MockRequestMatchRule;
use EasyPost\Test\Mocking\MockRequestResponseInfo;
use PHPUnit\Framework\TestCase;

class PaginationTest extends TestCase
{
/** @var array<int, mixed> */
private array $sentParams = [];

/**
* Build a mocked client that records the query params of every request.
*/
private function client(string $urlPattern, string $responseBody): EasyPostClient
{
$client = new EasyPostClient('test_key', 60.0, 'https://api.easypost.com', new MockingUtility([
new MockRequest(
new MockRequestMatchRule('get', $urlPattern),
new MockRequestResponseInfo(200, $responseBody)
),
]));
$client->subscribeToRequestHook(function (array $args): void {
$this->sentParams[] = $args['request_body'];
});

return $client;
}

/**
* Test that the new cursor and page size win over the params of the previous request.
*/
public function testGetNextPageCursorOverridesPreviousParams(): void
{
$client = $this->client('/v2\\/shipments$/', '{"shipments": [{"id": "shp_last"}], "has_more": true}');

$shipments = $client->shipment->all(['after_id' => 'shp_old', 'page_size' => 5, 'purchased' => false]);
$nextPage = $client->shipment->getNextPage($shipments, 2);

$this->assertEquals('shp_last', $this->sentParams[1]['before_id']);
$this->assertArrayNotHasKey('after_id', $this->sentParams[1]);
$this->assertEquals(2, $this->sentParams[1]['page_size']);
$this->assertFalse($this->sentParams[1]['purchased']);

$client->shipment->getNextPage($nextPage);

$this->assertEquals('shp_last', $this->sentParams[2]['before_id']);
$this->assertEquals(5, $this->sentParams[2]['page_size']);
}

/**
* Test that the new cursor wins over the params of the previous request for child users.
*/
public function testGetNextPageOfChildrenCursorOverridesPreviousParams(): void
{
$client = $this->client('/v2\\/users\\/children$/', '{"children": [{"id": "user_last"}], "has_more": true}');

$children = $client->user->allChildren(['before_id' => 'user_old', 'page_size' => 5]);
$client->user->getNextPageOfChildren($children, 2);

$this->assertEquals('user_last', $this->sentParams[1]['after_id']);
$this->assertArrayNotHasKey('before_id', $this->sentParams[1]);
$this->assertEquals(2, $this->sentParams[1]['page_size']);
}

/**
* Test that the cursor keeps advancing when the first request had no params.
*/
public function testGetNextPageAdvancesWithoutInitialParams(): void
{
$client = $this->client('/v2\\/shipments$/', '{"shipments": [{"id": "shp_last"}], "has_more": true}');

$firstPage = $client->shipment->all();
$secondPage = $client->shipment->getNextPage($firstPage);
$lastShipment = $secondPage['shipments'][0];
$lastShipment->id = 'shp_second';
$client->shipment->getNextPage($secondPage);

$this->assertEquals('shp_second', $this->sentParams[2]['before_id']);
}

/**
* Test that the params of the first request carry over to the next page of reports.
*/
public function testGetNextPageOfReportsKeepsPreviousParams(): void
{
$client = $this->client(
'/v2\\/reports\\/shipment$/',
'{"reports": [{"id": "shprep_last", "object": "ShipmentReport"}], "has_more": true}'
);

$reports = $client->report->all([
'type' => 'shipment',
'page_size' => 5,
]);
$client->report->getNextPage($reports);

$this->assertEquals('shprep_last', $this->sentParams[1]['before_id']);
$this->assertEquals(5, $this->sentParams[1]['page_size']);
}
}
Loading