Skip to content

Commit 4d3443e

Browse files
committed
Fix LazyCollection#takeUntilTimeout
1 parent 34bb59a commit 4d3443e

File tree

3 files changed

+74
-34
lines changed

3 files changed

+74
-34
lines changed

src/Illuminate/Collections/LazyCollection.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,18 +1414,16 @@ public function takeUntilTimeout(DateTimeInterface $timeout)
14141414
$timeout = $timeout->getTimestamp();
14151415

14161416
return new static(function () use ($timeout) {
1417-
$iterator = $this->getIterator();
1418-
1419-
if (! $iterator->valid() || $this->now() > $timeout) {
1417+
if ($this->now() >= $timeout) {
14201418
return;
14211419
}
14221420

1423-
yield $iterator->key() => $iterator->current();
1424-
1425-
while ($iterator->valid() && $this->now() < $timeout) {
1426-
$iterator->next();
1421+
foreach ($this as $key => $value) {
1422+
yield $key => $value;
14271423

1428-
yield $iterator->key() => $iterator->current();
1424+
if ($this->now() >= $timeout) {
1425+
break;
1426+
}
14291427
}
14301428
});
14311429
}

tests/Support/SupportLazyCollectionIsLazyTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Illuminate\Tests\Support;
44

55
use Exception;
6+
use Illuminate\Support\Carbon;
67
use Illuminate\Support\ItemNotFoundException;
78
use Illuminate\Support\LazyCollection;
89
use Illuminate\Support\MultipleItemsFoundException;
10+
use Mockery as m;
911
use PHPUnit\Framework\TestCase;
1012
use stdClass;
1113

@@ -1214,6 +1216,72 @@ public function testTakeUntilIsLazy()
12141216
});
12151217
}
12161218

1219+
public function testTakeUntilTimeoutIsLazy()
1220+
{
1221+
tap(m::mock(LazyCollection::class.'[now]')->times(100), function ($mock) {
1222+
$this->assertDoesNotEnumerateCollection($mock, function ($mock) {
1223+
$timeout = Carbon::now();
1224+
1225+
$results = $mock
1226+
->tap(function ($collection) use ($mock, $timeout) {
1227+
tap($collection)
1228+
->mockery_init($mock->mockery_getContainer())
1229+
->shouldAllowMockingProtectedMethods()
1230+
->shouldReceive('now')
1231+
->times(1)
1232+
->andReturn(
1233+
$timeout->getTimestamp()
1234+
);
1235+
})
1236+
->takeUntilTimeout($timeout)
1237+
->all();
1238+
});
1239+
});
1240+
1241+
tap(m::mock(LazyCollection::class.'[now]')->times(100), function ($mock) {
1242+
$this->assertEnumeratesCollection($mock, 1, function ($mock) {
1243+
$timeout = Carbon::now();
1244+
1245+
$results = $mock
1246+
->tap(function ($collection) use ($mock, $timeout) {
1247+
tap($collection)
1248+
->mockery_init($mock->mockery_getContainer())
1249+
->shouldAllowMockingProtectedMethods()
1250+
->shouldReceive('now')
1251+
->times(2)
1252+
->andReturn(
1253+
(clone $timeout)->sub(1, 'minute')->getTimestamp(),
1254+
$timeout->getTimestamp()
1255+
);
1256+
})
1257+
->takeUntilTimeout($timeout)
1258+
->all();
1259+
});
1260+
});
1261+
1262+
tap(m::mock(LazyCollection::class.'[now]')->times(100), function ($mock) {
1263+
$this->assertEnumeratesCollectionOnce($mock, function ($mock) {
1264+
$timeout = Carbon::now();
1265+
1266+
$results = $mock
1267+
->tap(function ($collection) use ($mock, $timeout) {
1268+
tap($collection)
1269+
->mockery_init($mock->mockery_getContainer())
1270+
->shouldAllowMockingProtectedMethods()
1271+
->shouldReceive('now')
1272+
->times(100)
1273+
->andReturn(
1274+
(clone $timeout)->sub(1, 'minute')->getTimestamp()
1275+
);
1276+
})
1277+
->takeUntilTimeout($timeout)
1278+
->all();
1279+
});
1280+
});
1281+
1282+
m::close();
1283+
}
1284+
12171285
public function testTakeWhileIsLazy()
12181286
{
12191287
$this->assertDoesNotEnumerate(function ($collection) {

tests/Support/SupportLazyCollectionTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -196,32 +196,6 @@ public function testTakeUntilTimeout()
196196
m::close();
197197
}
198198

199-
public function testTakeUntilTimeoutWithPastTimeout()
200-
{
201-
$timeout = Carbon::now()->subMinute();
202-
203-
$mock = m::mock(LazyCollection::class.'[now]');
204-
205-
$results = $mock
206-
->times(10)
207-
->tap(function ($collection) use ($mock, $timeout) {
208-
tap($collection)
209-
->mockery_init($mock->mockery_getContainer())
210-
->shouldAllowMockingProtectedMethods()
211-
->shouldReceive('now')
212-
->times(1)
213-
->andReturn(
214-
(clone $timeout)->add(1, 'minute')->getTimestamp(),
215-
);
216-
})
217-
->takeUntilTimeout($timeout)
218-
->all();
219-
220-
$this->assertSame([], $results);
221-
222-
m::close();
223-
}
224-
225199
public function testTapEach()
226200
{
227201
$data = LazyCollection::times(10);

0 commit comments

Comments
 (0)