Skip to content

Commit

Permalink
Fill coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Nov 2, 2019
1 parent 14a9835 commit a3d4831
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
5 changes: 4 additions & 1 deletion tests/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}
}
78 changes: 73 additions & 5 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
*/
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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()
Expand All @@ -105,7 +121,7 @@ public function testStructuredArrayOutput()
/**
* @test
*/
public function testPaginationOutput()
public function testLampagerPaginationOutput()
{
$expected1 = [
'data' => [
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a3d4831

Please sign in to comment.