Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public function valid(): bool
*/
public function rewind(): void
{
$this->lastFiveBodyChecksums = [];
$this->currentPage = max(0, $this->startPage - 1);
$this->page = $this->startPage;
$this->currentResponse = null;
Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/CollectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Saloon\Http\Response;
use Illuminate\Support\Collection;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\PaginationPlugin\Tests\Fixtures\Connectors\PagedConnector;
use Saloon\PaginationPlugin\Tests\Fixtures\Requests\SuperheroPagedRequest;

Expand All @@ -29,3 +31,38 @@ function toIds(array $items): array

expect($paginator->getTotalResults())->toEqual(20);
});

/**
* @see https://github.com/saloonphp/saloon/issues/464
*/
test('multiple collect calls on same LazyCollection do not trigger infinite loop detection', function () {
$singlePageResponse = [
'data' => [['id' => 1], ['id' => 2]],
'next_page_url' => null,
];

$connector = new PagedConnector;
$connector->withMockClient(new MockClient([
MockResponse::make($singlePageResponse),
MockResponse::make($singlePageResponse),
MockResponse::make($singlePageResponse),
MockResponse::make($singlePageResponse),
MockResponse::make($singlePageResponse),
MockResponse::make($singlePageResponse),
]));

$request = new SuperheroPagedRequest;
$lazyCollection = $connector->paginate($request)->collect();

$first = $lazyCollection->collect();
expect($first)->toBeInstanceOf(Collection::class);
expect($first->pluck('id')->all())->toBe([1, 2]);

$lazyCollection->collect();
$lazyCollection->collect();
$lazyCollection->collect();
$lazyCollection->collect();

$again = $lazyCollection->collect();
expect($again->pluck('id')->all())->toBe([1, 2]);
});