Skip to content

Commit

Permalink
Improve tests with dataset and cases with 0 as first page
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 31, 2024
1 parent c3d9329 commit f486a69
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 28 deletions.
35 changes: 10 additions & 25 deletions tests/Feature/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,29 @@

use Cerbero\LazyJsonPages\Exceptions\InvalidPaginationException;
use Cerbero\LazyJsonPages\LazyJsonPages;
use Cerbero\LazyJsonPages\Paginations\TotalPagesAwarePagination;

it('supports paginations aware of their total pages', function () {
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users')
->totalPages('meta.total_pages')
->collect('data.*');

expect($lazyCollection)->toLoadItemsViaRequests([
'https://example.com/api/v1/users' => 'lengthAware/page1.json',
'https://example.com/api/v1/users?page=2' => 'lengthAware/page2.json',
'https://example.com/api/v1/users?page=3' => 'lengthAware/page3.json',
]);
});

it('supports paginations aware of their total items', function () {
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users')
->totalItems('meta.total_items')
it('supports length-aware paginations', function (Closure $configure) {
$lazyCollection = $configure(LazyJsonPages::from('https://example.com/api/v1/users'))
->collect('data.*');

expect($lazyCollection)->toLoadItemsViaRequests([
'https://example.com/api/v1/users' => 'lengthAware/page1.json',
'https://example.com/api/v1/users?page=2' => 'lengthAware/page2.json',
'https://example.com/api/v1/users?page=3' => 'lengthAware/page3.json',
]);
});
})->with('length-aware');

it('supports custom paginations', function () {
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users')
->pagination(TotalPagesAwarePagination::class)
->totalPages('meta.total_pages')
it('supports length-aware paginations having 0 as first page', function (Closure $configure) {
$lazyCollection = $configure(LazyJsonPages::from('https://example.com/api/v1/users'))
->firstPage(0)
->collect('data.*');

expect($lazyCollection)->toLoadItemsViaRequests([
'https://example.com/api/v1/users' => 'lengthAware/page1.json',
'https://example.com/api/v1/users?page=2' => 'lengthAware/page2.json',
'https://example.com/api/v1/users?page=3' => 'lengthAware/page3.json',
'https://example.com/api/v1/users' => 'lengthAwareFirstPage0/page0.json',
'https://example.com/api/v1/users?page=1' => 'lengthAwareFirstPage0/page1.json',
'https://example.com/api/v1/users?page=2' => 'lengthAwareFirstPage0/page2.json',
]);
});
})->with('length-aware');

it('fails if an invalid custom pagination is provided', function () {
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users')
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/StructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@
]);
});

it('supports paginations with offset and 0 as first page', function () {
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users')
->offset()
->firstPage(0)
->totalPages('meta.total_pages')
->collect('data.*');

expect($lazyCollection)->toLoadItemsViaRequests([
'https://example.com/api/v1/users' => 'lengthAwareFirstPage0/page0.json',
'https://example.com/api/v1/users?offset=5' => 'lengthAwareFirstPage0/page1.json',
'https://example.com/api/v1/users?offset=10' => 'lengthAwareFirstPage0/page2.json',
]);
});

it('supports paginations with limit and offset', function () {
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users?limit=5')
->offset()
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/lengthAware/page1.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"meta": {
"total_pages": 3,
"total_items": 14
"total_items": 14,
"last_page": 3
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/lengthAware/page2.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"meta": {
"total_pages": 3,
"total_items": 14
"total_items": 14,
"last_page": 3
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/lengthAware/page3.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"meta": {
"total_pages": 3,
"total_items": 14
"total_items": 14,
"last_page": 3
}
}
24 changes: 24 additions & 0 deletions tests/fixtures/lengthAwareFirstPage0/page0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data": [
{
"name": "item1"
},
{
"name": "item2"
},
{
"name": "item3"
},
{
"name": "item4"
},
{
"name": "item5"
}
],
"meta": {
"total_pages": 3,
"total_items": 14,
"last_page": 2
}
}
24 changes: 24 additions & 0 deletions tests/fixtures/lengthAwareFirstPage0/page1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"data": [
{
"name": "item6"
},
{
"name": "item7"
},
{
"name": "item8"
},
{
"name": "item9"
},
{
"name": "item10"
}
],
"meta": {
"total_pages": 3,
"total_items": 14,
"last_page": 2
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/lengthAwareFirstPage0/page2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"data": [
{
"name": "item11"
},
{
"name": "item12"
},
{
"name": "item13"
},
{
"name": "item14"
}
],
"meta": {
"total_pages": 3,
"total_items": 14,
"last_page": 2
}
}

0 comments on commit f486a69

Please sign in to comment.