From 87d275a7efae74204f402a558f2e0fcd9deefd69 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Thu, 8 Aug 2024 19:47:42 +0100 Subject: [PATCH] tests: add missing cursor pagination tests --- .../Pagination/CursorPaginationTest.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/lib/Acceptance/Pagination/CursorPaginationTest.php b/tests/lib/Acceptance/Pagination/CursorPaginationTest.php index 1362f0b..0b6b975 100644 --- a/tests/lib/Acceptance/Pagination/CursorPaginationTest.php +++ b/tests/lib/Acceptance/Pagination/CursorPaginationTest.php @@ -260,6 +260,45 @@ public function testWithoutCursor(): void $this->assertPage($posts->reverse()->take(3), $page); } + /** + * @return void + */ + public function testWithAscending(): void + { + $this->paginator->withAscending(); + + $posts = Post::factory()->count(4)->create(); + + $meta = [ + 'from' => $this->encodeCursor(["id" => "1"], pointsToNextItems: false), + 'hasMore' => true, + 'perPage' => 3, + 'to' => $this->encodeCursor(["id" => "3"], pointsToNextItems: true), + ]; + + $links = [ + 'first' => [ + 'href' => 'http://localhost/api/v1/posts?' . Arr::query([ + 'page' => ['limit' => '3'] + ]), + ], + 'next' => [ + 'href' => 'http://localhost/api/v1/posts?' . Arr::query([ + 'page' => [ + 'after' => $this->encodeCursor(["id" => "3"], pointsToNextItems: true), + 'limit' => '3', + ], + ]), + ], + ]; + + $page = $this->posts->repository()->queryAll()->paginate(['limit' => '3']); + + $this->assertSame(['page' => $meta], $page->meta()); + $this->assertSame($links, $page->links()->toArray()); + $this->assertPage($posts->take(3), $page); + } + /** * @return void */ @@ -569,6 +608,22 @@ public function testPageWithReverseKey(): void $this->assertPage($posts->take(3), $page); } + /** + * @return void + */ + public function testPageWithReverseKeyWhenAscending(): void + { + $this->paginator->withAscending(); + + $posts = Post::factory()->count(4)->create(); + + $page = $this->posts->repository()->queryAll() + ->sort('-id') + ->paginate(['limit' => '3']); + + $this->assertPage($posts->reverse()->take(3), $page); + } + /** * If we are sorting by a column that might not be unique, we expect * the page to always be returned in a particular order i.e. by the