Created
March 27, 2020 10:00
-
-
Save devmsh/6e1546fc46899776fdb44ae9c61002c1 to your computer and use it in GitHub Desktop.
Laravel assertPaginated macro
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
<?php | |
// Usgae | |
// test that posts api return a 15 post/page paginated response. | |
$this->get('api/posts')->assertPaginated(15); | |
/** | |
* If you use Laravel API resources and want test if an endpoint | |
* is paginated use can use: $this->get('...')->assertPaginated(); | |
* | |
* @param $perPage optional param to check the pagination page size. | |
*/ | |
TestResponse::macro('assertPaginated', function ($perPage = null) { | |
if ($perPage) { | |
$this->assertJsonPath('meta.per_page', $perPage); | |
} | |
return $this->assertJsonStructure([ | |
'meta' => [ | |
'current_page', | |
'from', | |
'last_page', | |
'path', | |
'per_page', | |
'to', | |
'total', | |
], | |
'links' => [ | |
'first', | |
'last', | |
'next', | |
'prev', | |
], | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment