Skip to content

Commit 77d3a70

Browse files
committed
Ensure onComplete is called when both next and last link don't have anything for us anymore
1 parent b605570 commit 77d3a70

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Service/IteratePagesService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function iterate(string $path): Observable
5555
'last' => $parsedLinks->getByRel('last'),
5656
];
5757

58-
if ($links['next'] === null || $links['last'] === null) {
58+
if ($links['next'] === null && $links['last'] === null) {
59+
$paths->onCompleted();
60+
5961
return;
6062
}
6163

tests/Service/IteratePagesServiceTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ public function testHandle()
5353
* Third request.
5454
*/
5555
$thirdRequest = new Request('GET', 'https://api.example.com/2');
56-
$client->request($thirdRequest, Argument::type('array'))->shouldNotBeCalled();
56+
$thirdBody = [];
57+
$thirdStream = new JsonStream($thirdBody);
58+
$thirdHeaders = [
59+
'Link' => [
60+
'<https://api.example.com/1>; rel="prev"',
61+
],
62+
];
63+
$thirdResponse = new Response(200, $thirdHeaders, $thirdStream);
64+
$client->request($thirdRequest, Argument::type('array'))->shouldBeCalled()->willReturn(resolve($thirdResponse));
5765

5866
$requestService = new RequestService($client->reveal());
5967
$testScheduler = new TestScheduler();
@@ -62,8 +70,8 @@ public function testHandle()
6270
$items = [];
6371
$completed = false;
6472

65-
$stream = $iteratePagesService->iterate($path)->take(2)->subscribe(
66-
function ($item) use (&$items, &$stream) {
73+
$iteratePagesService->iterate($path)->subscribe(
74+
function ($item) use (&$items) {
6775
$items[] = $item;
6876
},
6977
function ($t) {
@@ -77,6 +85,6 @@ function () use (&$completed) {
7785
$testScheduler->start();
7886

7987
self::assertTrue($completed);
80-
self::assertSame([$firstBody, $secondBody], $items);
88+
self::assertSame([$firstBody, $secondBody, $thirdBody], $items);
8189
}
8290
}

0 commit comments

Comments
 (0)