-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Ability to add cursor pagination Extend model pagination with cursor pagination. ``` @apiResourceModel App\Models\User paginate=10,cursor ``` * Add tests for cursor pagination * fix tests on older laravel versions * fix per_page --------- Co-authored-by: fikri-kompanion <[email protected]>
- Loading branch information
1 parent
018cac9
commit 46e8399
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Knuckles\Scribe\Tests\Strategies\Responses; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Support\Facades\Schema; | ||
use Knuckles\Camel\Extraction\ExtractedEndpointData; | ||
|
@@ -798,4 +799,54 @@ public function can_parse_apiresourcecollection_tags_with_collection_class_pagin | |
], | ||
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function can_parse_apiresourcecollection_tags_with_collection_class_and_cursor_pagination() | ||
{ | ||
$config = new DocumentationConfig([]); | ||
|
||
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]); | ||
|
||
$strategy = new UseApiResourceTags($config); | ||
$tags = [ | ||
new Tag('apiResourceCollection', '\Knuckles\Scribe\Tests\Fixtures\TestUserApiResourceCollection'), | ||
new Tag('apiResourceModel', '\Knuckles\Scribe\Tests\Fixtures\TestUser paginate=1,cursor'), | ||
]; | ||
$results = $strategy->getApiResourceResponseFromTags($strategy->getApiResourceTag($tags), $tags, ExtractedEndpointData::fromRoute($route)); | ||
|
||
$nextCursor = base64_encode(json_encode(['_pointsToNextItems' => true])); | ||
$this->assertArraySubset([ | ||
[ | ||
'status' => 200, | ||
'content' => json_encode([ | ||
'data' => [ | ||
[ | ||
'id' => 4, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
], | ||
], | ||
'links' => [ | ||
'self' => 'link-value', | ||
"first" => null, | ||
"last" => null, | ||
"prev" => null, | ||
"next" => "/?cursor={$nextCursor}", | ||
], | ||
"meta" => match (version_compare(Application::VERSION, '9.0', '>=')) { | ||
false => [ | ||
"path" => '/', | ||
'per_page' => '1', | ||
], | ||
true => [ | ||
"path" => '/', | ||
'per_page' => 1, | ||
'next_cursor' => $nextCursor, | ||
'prev_cursor' => null, | ||
] | ||
}, | ||
]), | ||
], | ||
], $results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Knuckles\Scribe\Tests\Strategies\Responses; | ||
|
||
use Illuminate\Foundation\Application; | ||
use Illuminate\Routing\Route; | ||
use Knuckles\Camel\Extraction\ExtractedEndpointData; | ||
use Knuckles\Scribe\Attributes\Response; | ||
|
@@ -228,6 +229,62 @@ public function can_parse_transformer_attributes() | |
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function can_parse_apiresource_attributes_with_cursor_pagination() | ||
{ | ||
$factory = app(\Illuminate\Database\Eloquent\Factory::class); | ||
$factory->afterMaking(TestUser::class, function (TestUser $user, $faker) { | ||
if ($user->id === 4) { | ||
$child = Utils::getModelFactory(TestUser::class)->make(['id' => 5, 'parent_id' => 4]); | ||
$user->setRelation('children', collect([$child])); | ||
} | ||
}); | ||
|
||
$results = $this->fetch($this->endpoint("apiResourceAttributesWithCursorPaginate")); | ||
|
||
|
||
$nextCursor = base64_encode(json_encode(['_pointsToNextItems' => true])); | ||
$this->assertArraySubset([ | ||
[ | ||
'status' => 200, | ||
'content' => json_encode([ | ||
'data' => [ | ||
[ | ||
'id' => 4, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'children' => [ | ||
[ | ||
'id' => 5, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
], | ||
], | ||
], | ||
], | ||
'links' => [ | ||
"first" => null, | ||
"last" => null, | ||
"prev" => null, | ||
"next" => "/?cursor={$nextCursor}", | ||
], | ||
"meta" => match (version_compare(Application::VERSION, '9.0', '>=')) { | ||
false => [ | ||
"path" => '/', | ||
'per_page' => 1, | ||
], | ||
true => [ | ||
"path" => '/', | ||
'per_page' => 1, | ||
'next_cursor' => $nextCursor, | ||
'prev_cursor' => null, | ||
] | ||
}, | ||
]), | ||
], | ||
], $results); | ||
} | ||
|
||
protected function fetch($endpoint): array | ||
{ | ||
$strategy = new UseResponseAttributes(new DocumentationConfig([])); | ||
|
@@ -282,4 +339,10 @@ public function transformerAttributes() | |
{ | ||
|
||
} | ||
|
||
#[ResponseFromApiResource(TestUserApiResource::class, collection: true, cursorPaginate: 1)] | ||
public function apiResourceAttributesWithCursorPaginate() | ||
{ | ||
|
||
} | ||
} |