Skip to content

Commit 916f1b6

Browse files
committed
[Feature] Add multi-pagination support
1 parent aa6eb5e commit 916f1b6

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## Unreleased
7+
8+
### Added
9+
10+
- New `MultiPaginator` that allows a schema to offer multiple different pagination strategies.
11+
612
## [2.5.2] - 2023-01-25
713

814
### Fixed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
"php": "^7.4|^8.0",
2727
"ext-json": "*",
2828
"laravel-json-api/core": "^2.4",
29-
"laravel-json-api/eloquent": "^2.2.1",
29+
"laravel-json-api/eloquent": "dev-release/2.3.0 as 2.3.0",
3030
"laravel-json-api/encoder-neomerx": "^2.0.1",
3131
"laravel-json-api/exceptions": "^1.1.1",
3232
"laravel-json-api/spec": "^1.2",
3333
"laravel-json-api/validation": "^2.1.2",
3434
"laravel/framework": "^8.76|^9.0"
3535
},
3636
"require-dev": {
37+
"laravel-json-api/cursor-pagination": "^2.1",
3738
"laravel-json-api/testing": "^1.1.2",
3839
"orchestra/testbench": "^6.23|^7.0",
3940
"phpunit/phpunit": "^9.5.10"

tests/dummy/app/JsonApi/V1/Posts/PostSchema.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace App\JsonApi\V1\Posts;
2121

2222
use App\Models\Post;
23+
use LaravelJsonApi\CursorPagination\CursorPagination;
2324
use LaravelJsonApi\Eloquent\Fields\DateTime;
2425
use LaravelJsonApi\Eloquent\Fields\ID;
2526
use LaravelJsonApi\Eloquent\Fields\Relations\BelongsTo;
@@ -32,14 +33,14 @@
3233
use LaravelJsonApi\Eloquent\Filters\Scope;
3334
use LaravelJsonApi\Eloquent\Filters\Where;
3435
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
36+
use LaravelJsonApi\Eloquent\Pagination\MultiPagination;
3537
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
3638
use LaravelJsonApi\Eloquent\Schema;
3739
use LaravelJsonApi\Eloquent\SoftDeletes;
3840
use LaravelJsonApi\Eloquent\Sorting\SortCountable;
3941

4042
class PostSchema extends Schema
4143
{
42-
4344
use SoftDeletes;
4445

4546
/**
@@ -112,9 +113,11 @@ public function sortables(): iterable
112113
/**
113114
* @inheritDoc
114115
*/
115-
public function pagination(): PagePagination
116+
public function pagination(): MultiPagination
116117
{
117-
return PagePagination::make()->withoutNestedMeta();
118+
return new MultiPagination(
119+
PagePagination::make()->withoutNestedMeta(),
120+
CursorPagination::make($this->id())->withoutNestedMeta(),
121+
);
118122
}
119-
120123
}

tests/dummy/tests/Api/V1/Posts/IndexTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function testWithUser(): void
7979
$response->assertFetchedMany($expected);
8080
}
8181

82-
public function testPaginated(): void
82+
public function testPagePagination(): void
8383
{
8484
$posts = Post::factory()->count(5)->create();
8585

@@ -116,7 +116,51 @@ public function testPaginated(): void
116116
->page(['number' => 1, 'size' => 3])
117117
->get('/api/v1/posts');
118118

119-
$response->assertFetchedMany($expected)
119+
$response
120+
->assertFetchedMany($expected)
121+
->assertMeta($meta)
122+
->assertLinks($links);
123+
}
124+
125+
public function testCursorPagination(): void
126+
{
127+
$posts = Post::factory()->count(5)->create([
128+
'created_at' => fn() => fake()->dateTime(),
129+
])->sortByDesc('created_at')->values();
130+
131+
$expected = $this->identifiersFor('posts', $posts->take(3));
132+
133+
$meta = [
134+
'from' => $expected[0]['id'],
135+
'hasMore' => true,
136+
'perPage' => 3,
137+
'to' => $expected[2]['id'],
138+
];
139+
140+
$links = [
141+
'first' => 'http://localhost/api/v1/posts?' . Arr::query([
142+
'page' => ['limit' => 3],
143+
'sort' => '-createdAt',
144+
]),
145+
'next' => 'http://localhost/api/v1/posts?' . Arr::query([
146+
'page' => ['after' => $expected[2]['id'], 'limit' => 3],
147+
'sort' => '-createdAt',
148+
]),
149+
'prev' => 'http://localhost/api/v1/posts?' . Arr::query([
150+
'page' => ['before' => $expected[0]['id'], 'limit' => 3],
151+
'sort' => '-createdAt',
152+
]),
153+
];
154+
155+
$response = $this
156+
->withoutExceptionHandling()
157+
->jsonApi()
158+
->expects('posts')
159+
->page(['limit' => 3])
160+
->get('/api/v1/posts');
161+
162+
$response
163+
->assertFetchedMany($expected)
120164
->assertMeta($meta)
121165
->assertLinks($links);
122166
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy