Skip to content

Instantly share code, notes, and snippets.

@devmsh
Created March 27, 2020 10:00
Show Gist options
  • Save devmsh/6e1546fc46899776fdb44ae9c61002c1 to your computer and use it in GitHub Desktop.
Save devmsh/6e1546fc46899776fdb44ae9c61002c1 to your computer and use it in GitHub Desktop.
Laravel assertPaginated macro
<?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