generated from cerbero90/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support paginations with current page in URI path
- Loading branch information
Showing
7 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Cerbero\LazyJsonPages\Exceptions; | ||
|
||
/** | ||
* The exception thrown when a page cannot be found in the URI path. | ||
*/ | ||
class InvalidPageInPathException extends LazyJsonPagesException | ||
{ | ||
/** | ||
* Instantiate the class. | ||
*/ | ||
public function __construct(public readonly string $path, public readonly string $pattern) | ||
{ | ||
parent::__construct("The pattern [{$pattern}] could not capture any page from the path [{$path}]."); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use Cerbero\LazyJsonPages\Exceptions\InvalidPageInPathException; | ||
use Cerbero\LazyJsonPages\LazyJsonPages; | ||
|
||
it('supports paginations with the current page in the URI path', function () { | ||
$expectedItems = require fixture('items.php'); | ||
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users/page/1') | ||
->pageInPath() | ||
->totalPages('meta.total_pages') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toLoadItemsViaRequests($expectedItems, [ | ||
'https://example.com/api/v1/users/page/1' => '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 a custom pattern for paginations with the current page in the URI path', function () { | ||
$expectedItems = require fixture('items.php'); | ||
$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users/page1') | ||
->pageInPath('~/page(\d+)$~') | ||
->totalPages('meta.total_pages') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toLoadItemsViaRequests($expectedItems, [ | ||
'https://example.com/api/v1/users/page1' => 'lengthAware/page1.json', | ||
'https://example.com/api/v1/users/page2' => 'lengthAware/page2.json', | ||
'https://example.com/api/v1/users/page3' => 'lengthAware/page3.json', | ||
]); | ||
}); | ||
|
||
it('fails if it cannot capture the current page in the URI path', function () { | ||
$expectedItems = require fixture('items.php'); | ||
$lazyCollection = LazyJsonPages::from('https://example.com/users') | ||
->pageInPath() | ||
->totalPages('meta.total_pages') | ||
->collect('data.*'); | ||
|
||
expect($lazyCollection)->toLoadItemsViaRequests($expectedItems, [ | ||
'https://example.com/users' => 'lengthAware/page1.json', | ||
]); | ||
})->throws(InvalidPageInPathException::class, 'The pattern [/(\d+)(?!.*\d)/] could not capture any page from the path [/users].'); |