Skip to content

Commit 64e6470

Browse files
Adds Resource helpers to cursor paginator (#55879)
* feat: adds toResource helpers to cursor paginator * styling * fix tests
1 parent 3d084e4 commit 64e6470

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/Illuminate/Pagination/AbstractCursorPaginator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Support\Str;
1515
use Illuminate\Support\Traits\ForwardsCalls;
1616
use Illuminate\Support\Traits\Tappable;
17+
use Illuminate\Support\Traits\TransformsToResourceCollection;
1718
use Stringable;
1819
use Traversable;
1920

@@ -26,7 +27,7 @@
2627
*/
2728
abstract class AbstractCursorPaginator implements Htmlable, Stringable
2829
{
29-
use ForwardsCalls, Tappable;
30+
use ForwardsCalls, Tappable, TransformsToResourceCollection;
3031

3132
/**
3233
* All of the items being paginated.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Pagination;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
use Illuminate\Pagination\CursorPaginator;
7+
use Illuminate\Tests\Pagination\Fixtures\Models\CursorResourceTestModel;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class CursorResourceTest extends TestCase
11+
{
12+
public function testItCanTransformToExplicitResource()
13+
{
14+
$paginator = new CursorResourceTestPaginator([
15+
new CursorResourceTestModel(),
16+
], 1);
17+
18+
$resource = $paginator->toResourceCollection(CursorResourceTestResource::class);
19+
20+
$this->assertInstanceOf(JsonResource::class, $resource);
21+
}
22+
23+
public function testItThrowsExceptionWhenResourceCannotBeFound()
24+
{
25+
$this->expectException(\LogicException::class);
26+
$this->expectExceptionMessage('Failed to find resource class for model [Illuminate\Tests\Pagination\Fixtures\Models\CursorResourceTestModel].');
27+
28+
$paginator = new CursorResourceTestPaginator([
29+
new CursorResourceTestModel(),
30+
], 1);
31+
32+
$paginator->toResourceCollection();
33+
}
34+
35+
public function testItCanGuessResourceWhenNotProvided()
36+
{
37+
$paginator = new CursorResourceTestPaginator([
38+
new CursorResourceTestModel(),
39+
], 1);
40+
41+
class_alias(CursorResourceTestResource::class, 'Illuminate\Tests\Pagination\Fixtures\Http\Resources\CursorResourceTestModelResource');
42+
43+
$resource = $paginator->toResourceCollection();
44+
45+
$this->assertInstanceOf(JsonResource::class, $resource);
46+
}
47+
}
48+
49+
class CursorResourceTestResource extends JsonResource
50+
{
51+
//
52+
}
53+
54+
class CursorResourceTestPaginator extends CursorPaginator
55+
{
56+
//
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Pagination\Fixtures\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class CursorResourceTestModel extends Model
8+
{
9+
//
10+
}

0 commit comments

Comments
 (0)