Skip to content

Commit 9c6cc69

Browse files
committed
Merge branch 'feature/multi-pagination' into develop
2 parents aa6eb5e + 5ec899e commit 9c6cc69

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
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",

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
use LaravelJsonApi\Eloquent\Filters\Scope;
3333
use LaravelJsonApi\Eloquent\Filters\Where;
3434
use LaravelJsonApi\Eloquent\Filters\WhereIdIn;
35+
use LaravelJsonApi\Eloquent\Pagination\MultiPagination;
3536
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
3637
use LaravelJsonApi\Eloquent\Schema;
3738
use LaravelJsonApi\Eloquent\SoftDeletes;
3839
use LaravelJsonApi\Eloquent\Sorting\SortCountable;
3940

4041
class PostSchema extends Schema
4142
{
42-
4343
use SoftDeletes;
4444

4545
/**
@@ -112,9 +112,15 @@ public function sortables(): iterable
112112
/**
113113
* @inheritDoc
114114
*/
115-
public function pagination(): PagePagination
115+
public function pagination(): MultiPagination
116116
{
117-
return PagePagination::make()->withoutNestedMeta();
117+
return new MultiPagination(
118+
PagePagination::make()->withoutNestedMeta(),
119+
PagePagination::make()
120+
->withoutNestedMeta()
121+
->withSimplePagination()
122+
->withPageKey('current-page')
123+
->withPerPageKey('per-page')
124+
);
118125
}
119-
120126
}

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

Lines changed: 46 additions & 3 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,9 +116,52 @@ 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)
120121
->assertMeta($meta)
121-
->assertLinks($links);
122+
->assertExactLinks($links);
123+
}
124+
125+
public function testMultiPagination(): void
126+
{
127+
$posts = Post::factory()->count(6)->create([
128+
'created_at' => fn() => fake()->dateTime(),
129+
])->sortByDesc('created_at')->values();
130+
131+
$expected = $this->identifiersFor('posts', $posts->skip(2)->take(2));
132+
133+
$meta = [
134+
'currentPage' => 2,
135+
'from' => 3,
136+
'perPage' => 2,
137+
'to' => 4,
138+
];
139+
140+
$links = [
141+
'first' => 'http://localhost/api/v1/posts?' . Arr::query([
142+
'page' => ['current-page' => 1, 'per-page' => 2],
143+
'sort' => '-createdAt',
144+
]),
145+
'next' => 'http://localhost/api/v1/posts?' . Arr::query([
146+
'page' => ['current-page' => 3, 'per-page' => 2],
147+
'sort' => '-createdAt',
148+
]),
149+
'prev' => 'http://localhost/api/v1/posts?' . Arr::query([
150+
'page' => ['current-page' => 1, 'per-page' => 2],
151+
'sort' => '-createdAt',
152+
]),
153+
];
154+
155+
$response = $this
156+
->jsonApi()
157+
->expects('posts')
158+
->page(['current-page' => 2, 'per-page' => 2])
159+
->get('/api/v1/posts');
160+
161+
$response
162+
->assertFetchedMany($expected)
163+
->assertMeta($meta)
164+
->assertExactLinks($links);
122165
}
123166

124167
public function testIncludeAuthor(): void

0 commit comments

Comments
 (0)