Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit cbc5739

Browse files
committed
Add jsonPaginate to Builder (query builder) + tests
1 parent 9839290 commit cbc5739

File tree

8 files changed

+108
-21
lines changed

8 files changed

+108
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
composer.lock
33
.phpunit.result.cache
4+
phpunit.xml

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
</testsuites>
2525
<php>
2626
<ini name="memory_limit" value="2048M"/>
27+
<server name="DB_CONNECTION" value="sqlite" force="true"/>
28+
<server name="DB_DATABASE" value=":memory:" force="true"/>
2729
</php>
2830
</phpunit>

src/Builder.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
namespace SkoreLabs\JsonApi;
44

5+
/**
6+
* @mixin \Illuminate\Database\Query\Builder
7+
*/
58
class Builder
69
{
7-
/**
8-
* Paginate the given query using Json API.
9-
*
10-
* @param array $columns
11-
*
12-
* @throws \InvalidArgumentException
13-
*
14-
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
15-
*/
1610
public function jsonPaginate()
1711
{
12+
/**
13+
* Paginate the given query using JSON:API.
14+
*
15+
* @param int|string $perPage
16+
* @param array $columns
17+
*
18+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
19+
*/
1820
return function ($perPage = null, $columns = ['*']) {
19-
return function () use ($columns, $perPage) {
20-
$perPage = $perPage ?: $this->model->getPerPage();
21-
$clientPerPage = (int) request('page.size', config('json-api.pagination.default_size'));
21+
$perPage = $perPage ?: $this->model->getPerPage();
22+
$clientPerPage = (int) request('page.size', config('json-api.pagination.default_size'));
23+
24+
if (!$perPage || $perPage < $clientPerPage) {
25+
$perPage = $clientPerPage;
26+
}
2227

23-
if (!$perPage || $perPage < $clientPerPage) {
24-
$perPage = $clientPerPage;
25-
}
26-
27-
return $this->paginate($perPage, $columns, 'page[number]', (int) request('page.number'));
28-
};
28+
return $this->paginate($perPage, $columns, 'page[number]', (int) request('page.number'));
2929
};
3030
}
3131
}

src/JsonApiServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SkoreLabs\JsonApi;
44

5-
use Illuminate\Database\Query\Builder;
5+
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Foundation\Testing\TestResponse;
77
use Illuminate\Support\ServiceProvider;
88
use SkoreLabs\JsonApi\Builder as JsonApiBuilder;
@@ -22,8 +22,6 @@ public function boot()
2222
__DIR__.'/../config/json-api.php' => config_path('json-api.php'),
2323
], 'config');
2424
}
25-
26-
Builder::mixin(new JsonApiBuilder());
2725
}
2826

2927
/**
@@ -33,6 +31,8 @@ public function boot()
3331
*/
3432
public function register()
3533
{
34+
Builder::mixin(new JsonApiBuilder());
35+
3636
$this->mergeConfigFrom(__DIR__.'/../config/json-api.php', 'json-api');
3737

3838
if (class_exists(TestResponse::class)) {

src/Testing/Assert.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static function fromTestResponse($response)
6565
{
6666
try {
6767
$content = json_decode($response->getContent(), true);
68+
PHPUnit::assertArrayHasKey('data', $content);
6869
$data = $content['data'];
6970
$collection = [];
7071

tests/JsonApiCollectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function testCollectionsAtHasAttribute()
5959
{
6060
$this->get('/', ['Accept' => 'application/json'])->assertJsonApi(function (Assert $json) {
6161
$json->at(0)->hasAttribute('title', 'Test Title');
62+
6263
$json->at(1)->hasAttribute('title', 'Test Title 2');
6364
});
6465
}

tests/JsonApiPaginationTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace SkoreLabs\JsonApi\Tests;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Illuminate\Support\Facades\Route;
7+
use SkoreLabs\JsonApi\Http\Resources\JsonApiCollection;
8+
use SkoreLabs\JsonApi\Testing\Assert;
9+
use SkoreLabs\JsonApi\Tests\Fixtures\Post;
10+
11+
/**
12+
* @group requiresDatabase
13+
*/
14+
class JsonApiPaginationTest extends TestCase
15+
{
16+
use RefreshDatabase;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
Route::get('/posts', function () {
23+
Post::create(['title' => 'Test Title']);
24+
Post::create(['title' => 'Test Title 2']);
25+
Post::create(['title' => 'Test Title 3']);
26+
Post::create(['title' => 'Test Title 4']);
27+
28+
return JsonApiCollection::make(Post::jsonPaginate(1), true);
29+
});
30+
}
31+
32+
protected function defineDatabaseMigrations()
33+
{
34+
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
35+
}
36+
37+
public function testJsonApiPaginationWithPageSize()
38+
{
39+
$response = $this->getJson('/posts?page[size]=2');
40+
41+
$response->assertJsonApi(function (Assert $json) {
42+
$json->hasSize(2);
43+
});
44+
45+
$response->assertStatus(200);
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePostsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('posts', static function (Blueprint $table) {
17+
$table->bigInteger('id')->primary();
18+
$table->string('title');
19+
$table->unsignedBigInteger('parent_id')->nullable();
20+
$table->timestamps();
21+
22+
$table->foreign('parent_id')->references('id')->on('posts');
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('posts');
34+
}
35+
}

0 commit comments

Comments
 (0)