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

fix: keyName column may not be present in pagination cursor #41

Merged
merged 2 commits into from
Oct 31, 2024
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: 4 additions & 3 deletions src/Pagination/Cursor/CursorParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public function __construct(private IdParser $idParser, private string $keyName)
*/
public function encode(LaravelCursor $cursor): string
{
$key = $cursor->parameter($this->keyName);

if ($key) {
try {
$key = $cursor->parameter($this->keyName);
$parameters = $this->withoutPrivate($cursor->toArray());
$parameters[$this->keyName] = $this->idParser->encode($key);
$cursor = new LaravelCursor($parameters, $cursor->pointsToNextItems());
} catch (\UnexpectedValueException $ex) {
// Do nothing as the cursor does not contain the key.
}

return $cursor->encode();
Expand Down
51 changes: 51 additions & 0 deletions tests/lib/Acceptance/Pagination/CursorPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,57 @@ public function testWithoutCursor(): void
$this->assertPage($posts->reverse()->take(3), $page);
}

/**
* @return void
*/
public function testWithIdEncodingWithoutKeySort(): void
{
$this->withIdEncoding();
$this->paginator->withoutKeySort();

$posts = Post::factory()->count(4)->sequence(['title' => 'd'], ['title' => 'c'], ['title' => 'b'], ['title' => 'a'])->create();

$meta = [
'from' => $this->encodeCursor(
["posts.title"=> "a"],
pointsToNextItems: false,
),
'hasMore' => true,
'perPage' => 3,
'to' => $this->encodeCursor(
["posts.title"=> "c"],
pointsToNextItems: true,
),
];

$links = [
'first' => [
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
'page' => ['limit' => '3'],
'sort' => 'title',
]),
],
'next' => [
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
'page' => [
'after' => $this->encodeCursor(
["posts.title"=> "c"],
pointsToNextItems: true,
),
'limit' => '3',
],
'sort' => 'title',
]),
],
];

$page = $this->posts->repository()->queryAll()->sort('title')->paginate(['limit' => '3']);

$this->assertSame(['page' => $meta], $page->meta());
$this->assertSame($links, $page->links()->toArray());
$this->assertPage($posts->reverse()->take(3), $page);
}

/**
* @return void
*/
Expand Down