Skip to content

Commit 8734ac3

Browse files
authored
Fix | Reset body checksums on rewind to fix false infinite-loop (#17)
reset body checksums on rewind to fix false infinite-loop on multiple collect()
1 parent d798a99 commit 8734ac3

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/Paginator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public function valid(): bool
212212
*/
213213
public function rewind(): void
214214
{
215+
$this->lastFiveBodyChecksums = [];
215216
$this->currentPage = max(0, $this->startPage - 1);
216217
$this->page = $this->startPage;
217218
$this->currentResponse = null;

tests/Feature/CollectTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Saloon\Http\Response;
66
use Illuminate\Support\Collection;
7+
use Saloon\Http\Faking\MockClient;
8+
use Saloon\Http\Faking\MockResponse;
79
use Saloon\PaginationPlugin\Tests\Fixtures\Connectors\PagedConnector;
810
use Saloon\PaginationPlugin\Tests\Fixtures\Requests\SuperheroPagedRequest;
911

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

3032
expect($paginator->getTotalResults())->toEqual(20);
3133
});
34+
35+
/**
36+
* @see https://github.com/saloonphp/saloon/issues/464
37+
*/
38+
test('multiple collect calls on same LazyCollection do not trigger infinite loop detection', function () {
39+
$singlePageResponse = [
40+
'data' => [['id' => 1], ['id' => 2]],
41+
'next_page_url' => null,
42+
];
43+
44+
$connector = new PagedConnector;
45+
$connector->withMockClient(new MockClient([
46+
MockResponse::make($singlePageResponse),
47+
MockResponse::make($singlePageResponse),
48+
MockResponse::make($singlePageResponse),
49+
MockResponse::make($singlePageResponse),
50+
MockResponse::make($singlePageResponse),
51+
MockResponse::make($singlePageResponse),
52+
]));
53+
54+
$request = new SuperheroPagedRequest;
55+
$lazyCollection = $connector->paginate($request)->collect();
56+
57+
$first = $lazyCollection->collect();
58+
expect($first)->toBeInstanceOf(Collection::class);
59+
expect($first->pluck('id')->all())->toBe([1, 2]);
60+
61+
$lazyCollection->collect();
62+
$lazyCollection->collect();
63+
$lazyCollection->collect();
64+
$lazyCollection->collect();
65+
66+
$again = $lazyCollection->collect();
67+
expect($again->pluck('id')->all())->toBe([1, 2]);
68+
});

0 commit comments

Comments
 (0)