From f486a69a95cd1bad1a4a34c7ae7218aff4477257 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Wed, 31 Jan 2024 20:39:44 +1000 Subject: [PATCH] Improve tests with dataset and cases with 0 as first page --- tests/Feature/PaginationTest.php | 35 ++++++------------- tests/Feature/StructureTest.php | 14 ++++++++ tests/fixtures/lengthAware/page1.json | 3 +- tests/fixtures/lengthAware/page2.json | 3 +- tests/fixtures/lengthAware/page3.json | 3 +- .../fixtures/lengthAwareFirstPage0/page0.json | 24 +++++++++++++ .../fixtures/lengthAwareFirstPage0/page1.json | 24 +++++++++++++ .../fixtures/lengthAwareFirstPage0/page2.json | 21 +++++++++++ 8 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 tests/fixtures/lengthAwareFirstPage0/page0.json create mode 100644 tests/fixtures/lengthAwareFirstPage0/page1.json create mode 100644 tests/fixtures/lengthAwareFirstPage0/page2.json diff --git a/tests/Feature/PaginationTest.php b/tests/Feature/PaginationTest.php index 1ee9e58..662141a 100644 --- a/tests/Feature/PaginationTest.php +++ b/tests/Feature/PaginationTest.php @@ -2,23 +2,9 @@ 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([ @@ -26,20 +12,19 @@ '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') diff --git a/tests/Feature/StructureTest.php b/tests/Feature/StructureTest.php index 440d18b..8acb1b3 100644 --- a/tests/Feature/StructureTest.php +++ b/tests/Feature/StructureTest.php @@ -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() diff --git a/tests/fixtures/lengthAware/page1.json b/tests/fixtures/lengthAware/page1.json index 55c3fb4..5fa0fe8 100644 --- a/tests/fixtures/lengthAware/page1.json +++ b/tests/fixtures/lengthAware/page1.json @@ -18,6 +18,7 @@ ], "meta": { "total_pages": 3, - "total_items": 14 + "total_items": 14, + "last_page": 3 } } diff --git a/tests/fixtures/lengthAware/page2.json b/tests/fixtures/lengthAware/page2.json index 0f13b6b..9765209 100644 --- a/tests/fixtures/lengthAware/page2.json +++ b/tests/fixtures/lengthAware/page2.json @@ -18,6 +18,7 @@ ], "meta": { "total_pages": 3, - "total_items": 14 + "total_items": 14, + "last_page": 3 } } diff --git a/tests/fixtures/lengthAware/page3.json b/tests/fixtures/lengthAware/page3.json index 9199d0a..de48011 100644 --- a/tests/fixtures/lengthAware/page3.json +++ b/tests/fixtures/lengthAware/page3.json @@ -15,6 +15,7 @@ ], "meta": { "total_pages": 3, - "total_items": 14 + "total_items": 14, + "last_page": 3 } } diff --git a/tests/fixtures/lengthAwareFirstPage0/page0.json b/tests/fixtures/lengthAwareFirstPage0/page0.json new file mode 100644 index 0000000..65197c5 --- /dev/null +++ b/tests/fixtures/lengthAwareFirstPage0/page0.json @@ -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 + } +} diff --git a/tests/fixtures/lengthAwareFirstPage0/page1.json b/tests/fixtures/lengthAwareFirstPage0/page1.json new file mode 100644 index 0000000..5b3bc68 --- /dev/null +++ b/tests/fixtures/lengthAwareFirstPage0/page1.json @@ -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 + } +} diff --git a/tests/fixtures/lengthAwareFirstPage0/page2.json b/tests/fixtures/lengthAwareFirstPage0/page2.json new file mode 100644 index 0000000..129c482 --- /dev/null +++ b/tests/fixtures/lengthAwareFirstPage0/page2.json @@ -0,0 +1,21 @@ +{ + "data": [ + { + "name": "item11" + }, + { + "name": "item12" + }, + { + "name": "item13" + }, + { + "name": "item14" + } + ], + "meta": { + "total_pages": 3, + "total_items": 14, + "last_page": 2 + } +}