From a3d4831017f9a23752247f3637172cfe0dc593cc Mon Sep 17 00:00:00 2001 From: mpyw Date: Sun, 3 Nov 2019 05:09:43 +0900 Subject: [PATCH] Fill coverage --- tests/PostResource.php | 5 ++- tests/ResourceTest.php | 78 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/tests/PostResource.php b/tests/PostResource.php index 0511681..ac9ed94 100644 --- a/tests/PostResource.php +++ b/tests/PostResource.php @@ -15,6 +15,9 @@ class PostResource extends Resource */ public function toArray($request) { - return parent::toArray($request) + ['post_resource' => true]; + return parent::toArray($request) + [ + 'post_resource' => true, + 'dummy' => $this->whenLoaded('dummy'), + ]; } } diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index c45a5a6..af26813 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -21,7 +21,7 @@ protected function assertResultSame($expected, $actual) /** * @return \Lampager\Laravel\PaginationResult */ - protected function getPagination() + protected function getLampagerPagination() { return Post::lampager() ->forward()->limit(3) @@ -31,6 +31,18 @@ protected function getPagination() ->paginate(['id' => 3, 'updated_at' => '2017-01-01 10:00:00']); } + /** + * @return \Illuminate\Contracts\Pagination\Paginator + */ + protected function getStandardPagination() + { + return Post::query() + ->where('id', '>', 1) + ->orderBy('updated_at') + ->orderBy('id') + ->simplePaginate(3); + } + /** * @test */ @@ -54,11 +66,13 @@ public function testRawArrayOutput() ], ]; - $pagination = $this->getPagination(); + $pagination = $this->getLampagerPagination(); $records = $pagination->records; + $standardPagination = $this->getStandardPagination(); $this->assertResultSame($expected, (new PostResourceCollection($pagination))->resolve()); $this->assertResultSame($expected, (new PostResourceCollection($records))->resolve()); + $this->assertResultSame($expected, (new PostResourceCollection($standardPagination))->resolve()); } /** @@ -87,11 +101,13 @@ public function testStructuredArrayOutput() 'post_resource_collection' => true, ]; - $pagination = $this->getPagination(); + $pagination = $this->getLampagerPagination(); $records = $pagination->records; + $standardPagination = $this->getStandardPagination(); $this->assertResultSame($expected, (new StructuredPostResourceCollection($pagination))->resolve()); $this->assertResultSame($expected, (new StructuredPostResourceCollection($records))->resolve()); + $this->assertResultSame($expected, (new StructuredPostResourceCollection($standardPagination))->resolve()); $this->assertResultSame($expected, (new StructuredPostResourceCollection($records)) ->toResponse(null)->getData() @@ -105,7 +121,7 @@ public function testStructuredArrayOutput() /** * @test */ - public function testPaginationOutput() + public function testLampagerPaginationOutput() { $expected1 = [ 'data' => [ @@ -134,7 +150,59 @@ public function testPaginationOutput() // different order $expected2 = Arr::except($expected1, 'post_resource_collection') + ['post_resource_collection' => true]; - $pagination = $this->getPagination(); + $pagination = $this->getLampagerPagination(); + + $this->assertResultSame($expected1, (new StructuredPostResourceCollection($pagination)) + ->toResponse(null)->getData() + ); + $this->assertResultSame($expected2, (new PostResourceCollection($pagination)) + ->additional(['post_resource_collection' => true]) + ->toResponse(null)->getData() + ); + } + + /** + * @test + */ + public function testStandardPaginationOutput() + { + $expected1 = [ + 'data' => [ + [ + 'id' => 3, + 'updated_at' => '2017-01-01 10:00:00', + 'post_resource' => true, + ], + [ + 'id' => 5, + 'updated_at' => '2017-01-01 10:00:00', + 'post_resource' => true, + ], + [ + 'id' => 2, + 'updated_at' => '2017-01-01 11:00:00', + 'post_resource' => true, + ], + ], + 'post_resource_collection' => true, + 'links' => [ + 'first' => 'http://localhost?page=1', + 'last' => null, + 'prev' => null, + 'next' => 'http://localhost?page=2', + ], + 'meta' => [ + 'current_page' => 1, + 'from' => 1, + 'path' => 'http://localhost', + 'per_page' => 3, + 'to' => 3, + ], + ]; + // different order + $expected2 = Arr::except($expected1, 'post_resource_collection') + ['post_resource_collection' => true]; + + $pagination = $this->getStandardPagination(); $this->assertResultSame($expected1, (new StructuredPostResourceCollection($pagination)) ->toResponse(null)->getData()