generated from Sammyjo20/package-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from saloonphp/feature/add-start-page-property
Feature | Provide custom start page
- Loading branch information
Showing
3 changed files
with
116 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Saloon\PaginationPlugin\Tests\Fixtures\Connectors\PagedConnector; | ||
use Saloon\PaginationPlugin\Tests\Fixtures\Requests\SuperheroPagedRequest; | ||
use Saloon\PaginationPlugin\Tests\Fixtures\Connectors\CustomStartPagePagedConnector; | ||
|
||
test('you can specify a start page on a paginator class', function () { | ||
$connector = new CustomStartPagePagedConnector; | ||
$request = new SuperheroPagedRequest(); | ||
$paginator = $connector->paginate($request); | ||
|
||
$superheroes = []; | ||
$iteratorCounter = 0; | ||
|
||
foreach ($paginator as $item) { | ||
$iteratorCounter++; | ||
$superheroes = array_merge($superheroes, $item->json('data')); | ||
} | ||
|
||
expect($iteratorCounter)->toBe(2); | ||
expect($paginator->getTotalResults())->toEqual(10); | ||
|
||
$mapped = array_map(static fn (array $superhero) => $superhero['id'], $superheroes); | ||
|
||
expect($mapped)->toEqual([11, 12, 13, 14, 15, 16, 17, 18, 19, 20,]); | ||
}); | ||
|
||
test('you can specify a start page on a paginator instance', function () { | ||
$connector = new PagedConnector; | ||
$request = new SuperheroPagedRequest(); | ||
$paginator = $connector->paginate($request); | ||
|
||
$superheroes = []; | ||
$iteratorCounter = 0; | ||
|
||
$paginator->setStartPage(3); | ||
|
||
foreach ($paginator as $item) { | ||
$iteratorCounter++; | ||
$superheroes = array_merge($superheroes, $item->json('data')); | ||
} | ||
|
||
expect($iteratorCounter)->toBe(2); | ||
expect($paginator->getTotalResults())->toEqual(10); | ||
|
||
$mapped = array_map(static fn (array $superhero) => $superhero['id'], $superheroes); | ||
|
||
expect($mapped)->toEqual([11, 12, 13, 14, 15, 16, 17, 18, 19, 20,]); | ||
}); |
47 changes: 47 additions & 0 deletions
47
tests/Fixtures/Connectors/CustomStartPagePagedConnector.php
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Saloon\PaginationPlugin\Tests\Fixtures\Connectors; | ||
|
||
use Saloon\Http\Request; | ||
use Saloon\Http\Response; | ||
use Saloon\PaginationPlugin\PagedPaginator; | ||
use Saloon\PaginationPlugin\Contracts\HasPagination; | ||
use Saloon\PaginationPlugin\Contracts\HasRequestPagination; | ||
|
||
class CustomStartPagePagedConnector extends TestConnector implements HasPagination | ||
{ | ||
/** | ||
* Paginate over each page | ||
*/ | ||
public function paginate(Request $request): PagedPaginator | ||
{ | ||
if ($request instanceof HasRequestPagination) { | ||
return $request->paginate($this); | ||
} | ||
|
||
return new class(connector: $this, request: $request) extends PagedPaginator { | ||
/** | ||
* Set a custom start page | ||
*/ | ||
protected int $startPage = 3; | ||
|
||
/** | ||
* Check if we are on the last page | ||
*/ | ||
protected function isLastPage(Response $response): bool | ||
{ | ||
return empty($response->json('next_page_url')); | ||
} | ||
|
||
/** | ||
* Get the results from the page | ||
*/ | ||
protected function getPageItems(Response $response, Request $request): array | ||
{ | ||
return $response->json('data') ?? []; | ||
} | ||
}; | ||
} | ||
} |