Skip to content

Commit e593635

Browse files
specialtacticsMax Snow
andauthored
Adds pagination type feature & tests (#78)
Co-authored-by: Max Snow <[email protected]>
1 parent 8067f06 commit e593635

File tree

7 files changed

+90
-5
lines changed

7 files changed

+90
-5
lines changed

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<testsuites>
44
<testsuite name="API Test Suite">
55
<directory suffix="Test.php">./test/tests</directory>
6-
<exclude>./test/tests/TestCase.php</exclude>
76
</testsuite>
87
</testsuites>
98
<php>

src/Enums/PaginationType.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Specialtactics\L5Api\Enums;
3+
4+
/**
5+
* This denotes the type of Laravel pagination to use, for example in controllers
6+
*
7+
* By default, and for compatibility reasons with previous versions of the boilerplate, we will use LengthAware pagination
8+
*/
9+
enum PaginationType: string
10+
{
11+
case SIMPLE = 'simple';
12+
case LENGTH_AWARE = 'length-aware';
13+
case CURSOR = 'cursor';
14+
15+
/**
16+
* Attempt to get enum from string value, and set a default if we can't.
17+
*/
18+
public static function getFromValue(?string $value): self
19+
{
20+
if (! $value) {
21+
return self::LENGTH_AWARE;
22+
}
23+
24+
return self::tryFrom($value) ?? self::LENGTH_AWARE;
25+
}
26+
}

src/Http/Controllers/Features/RestfulControllerTrait.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@
33
namespace Specialtactics\L5Api\Http\Controllers\Features;
44

55
use App\Transformers\BaseTransformer;
6+
use Illuminate\Contracts\Pagination\CursorPaginator;
7+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8+
use Illuminate\Contracts\Pagination\Paginator;
9+
use Illuminate\Database\Eloquent\Builder;
610
use Illuminate\Http\Request;
11+
use Specialtactics\L5Api\Enums\PaginationType;
712
use Specialtactics\L5Api\Helpers;
813

914
trait RestfulControllerTrait
1015
{
16+
public PaginationType $paginationType = PaginationType::LENGTH_AWARE;
17+
18+
/**
19+
* Get the correct paginator instance based on the controller's paginator type param
20+
*/
21+
public function getPaginator(Builder $query, int $perPage): Paginator|LengthAwarePaginator|CursorPaginator
22+
{
23+
if ($this->paginationType === PaginationType::SIMPLE) {
24+
return $query->simplePaginate($perPage);
25+
} else {
26+
return $query->paginate($perPage);
27+
}
28+
}
29+
1130
/**
1231
* Figure out which transformer to use
1332
*

src/Http/Controllers/RestfulController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public function getAll()
5858
$perPage = intval(request()->input('per_page'));
5959
}
6060

61-
$paginator = $query->paginate($perPage);
62-
63-
return $this->response->paginator($paginator, $this->getTransformer());
61+
return $this->response->paginator(
62+
$this->getPaginator($query, $perPage),
63+
$this->getTransformer()
64+
);
6465
} else {
6566
$resources = $query->get();
6667

test/app/Http/Controllers/ForumController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use App\Models\Forum;
7+
use Specialtactics\L5Api\Enums\PaginationType;
78

89
class ForumController extends Controller
910
{
@@ -16,4 +17,6 @@ class ForumController extends Controller
1617
* @var null|BaseTransformer The transformer this controller should use, if overriding the model & default
1718
*/
1819
public static $transformer = null;
20+
21+
public PaginationType $paginationType = PaginationType::LENGTH_AWARE;
1922
}

test/routes/api-routes.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
* Test
4444
*/
4545
$api->group(['prefix' => 'posts'], function ($api) {
46-
$api->get('/', 'App\Http\Controllers\PostController@getAll');
4746
$api->post('/', 'App\Http\Controllers\PostController@post');
4847
});
4948

test/tests/Unit/PaginationTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Specialtactics\L5Api\Tests\Unit;
4+
5+
use App\Http\Controllers\ForumController;
6+
use Specialtactics\L5Api\Enums\PaginationType;
7+
use Specialtactics\L5Api\Tests\AppTestCase;
8+
9+
class PaginationTest extends AppTestCase
10+
{
11+
public function testLengthAwarePagination()
12+
{
13+
$jsonResponse = $this->actingAsAdmin()
14+
->json('GET', '/forums');
15+
16+
$jsonResponse->assertStatus(200);
17+
$response = $jsonResponse->decodeResponseJson();
18+
19+
$this->assertEquals(1, $response['meta']['pagination']['total']);
20+
$this->assertEquals(1, $response['meta']['pagination']['totalPages']);
21+
}
22+
23+
public function testSimplePagination()
24+
{
25+
$mockForumController = $this->partialMock(ForumController::class);
26+
$mockForumController->paginationType = PaginationType::SIMPLE;
27+
28+
$jsonResponse = $this->actingAsAdmin()
29+
->json('GET', '/forums');
30+
31+
$jsonResponse->assertStatus(200);
32+
$response = $jsonResponse->decodeResponseJson();
33+
34+
// League's Serealiser contract needs to return something, so at the moment it's zero for simple pagination
35+
$this->assertEquals(0, $response['meta']['pagination']['total']);
36+
$this->assertEquals(0, $response['meta']['pagination']['totalPages']);
37+
}
38+
}

0 commit comments

Comments
 (0)