Skip to content

Commit b82c13d

Browse files
committed
[Tests] Add test for a filtered and paginated request
1 parent 848e63e commit b82c13d

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function rules(): array
4343
'array',
4444
JsonApiRule::filter()->forget('id'),
4545
],
46+
'filter.published' => ['boolean'],
4647
'include' => [
4748
'nullable',
4849
'string',

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use LaravelJsonApi\Eloquent\Fields\Relations\BelongsToMany;
2828
use LaravelJsonApi\Eloquent\Fields\Relations\HasMany;
2929
use LaravelJsonApi\Eloquent\Fields\Str;
30+
use LaravelJsonApi\Eloquent\Filters\Scope;
3031
use LaravelJsonApi\Eloquent\Filters\Where;
3132
use LaravelJsonApi\Eloquent\Filters\WhereIn;
3233
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
@@ -69,6 +70,7 @@ public function filters(): array
6970
{
7071
return [
7172
WhereIn::make('id', $this->idColumn())->delimiter(','),
73+
Scope::make('published', 'wherePublished')->asBoolean(),
7274
Where::make('slug')->singular(),
7375
];
7476
}

tests/dummy/app/Models/Post.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace App\Models;
1919

2020
use App\Models\User;
21+
use Illuminate\Database\Eloquent\Builder;
2122
use Illuminate\Database\Eloquent\Factories\HasFactory;
2223
use Illuminate\Database\Eloquent\Model;
2324
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -70,4 +71,20 @@ public function tags(): MorphToMany
7071
{
7172
return $this->morphToMany(Tag::class, 'taggable');
7273
}
74+
75+
/**
76+
* @param Builder $query
77+
* @param bool $published
78+
* @return Builder
79+
*/
80+
public function scopeWherePublished(Builder $query, bool $published): Builder
81+
{
82+
$column = $this->qualifyColumn('published_at');
83+
84+
if ($published) {
85+
return $query->whereNotNull($column);
86+
}
87+
88+
return $query->whereNull($column);
89+
}
7390
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,44 @@ public function testSlugFilter(): void
155155
$response->assertFetchedOneExact($expected);
156156
}
157157

158+
public function testFilteredAndPaginated(): void
159+
{
160+
$published = Post::factory()->count(5)->create(['published_at' => now()]);
161+
Post::factory()->count(2)->create(['published_at' => null]);
162+
163+
$meta = [
164+
'currentPage' => 1,
165+
'from' => 1,
166+
'lastPage' => 1,
167+
'perPage' => 10,
168+
'to' => 5,
169+
'total' => 5,
170+
];
171+
172+
$links = [
173+
'first' => 'http://localhost/api/v1/posts?' . Arr::query([
174+
'filter' => ['published' => 'true'],
175+
'page' => ['number' => 1, 'size' => 10],
176+
]),
177+
'last' => 'http://localhost/api/v1/posts?' . Arr::query([
178+
'filter' => ['published' => 'true'],
179+
'page' => ['number' => 1, 'size' => 10],
180+
]),
181+
];
182+
183+
$response = $this
184+
->withoutExceptionHandling()
185+
->jsonApi()
186+
->expects('posts')
187+
->filter(['published' => 'true'])
188+
->page(['number' => 1, 'size' => 10])
189+
->get('/api/v1/posts');
190+
191+
$response->assertFetchedMany($published)
192+
->assertMeta($meta)
193+
->assertLinks($links);
194+
}
195+
158196
public function testInvalidMediaType(): void
159197
{
160198
$this->jsonApi()

0 commit comments

Comments
 (0)