Skip to content

Commit

Permalink
[9.x] Fix takeUntilTimeout method of LazyCollection (#41354)
Browse files Browse the repository at this point in the history
* Fix takeUntilTimeout

* Add extra test

* Small fix
  • Loading branch information
gdebrauwer authored Mar 6, 2022
1 parent a9098ee commit 34bb59a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,8 +1413,20 @@ public function takeUntilTimeout(DateTimeInterface $timeout)
{
$timeout = $timeout->getTimestamp();

return $this->takeWhile(function () use ($timeout) {
return $this->now() < $timeout;
return new static(function () use ($timeout) {
$iterator = $this->getIterator();

if (! $iterator->valid() || $this->now() > $timeout) {
return;
}

yield $iterator->key() => $iterator->current();

while ($iterator->valid() && $this->now() < $timeout) {
$iterator->next();

yield $iterator->key() => $iterator->current();
}
});
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Support/SupportLazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ public function testTakeUntilTimeout()
m::close();
}

public function testTakeUntilTimeoutWithPastTimeout()
{
$timeout = Carbon::now()->subMinute();

$mock = m::mock(LazyCollection::class.'[now]');

$results = $mock
->times(10)
->tap(function ($collection) use ($mock, $timeout) {
tap($collection)
->mockery_init($mock->mockery_getContainer())
->shouldAllowMockingProtectedMethods()
->shouldReceive('now')
->times(1)
->andReturn(
(clone $timeout)->add(1, 'minute')->getTimestamp(),
);
})
->takeUntilTimeout($timeout)
->all();

$this->assertSame([], $results);

m::close();
}

public function testTapEach()
{
$data = LazyCollection::times(10);
Expand Down

0 comments on commit 34bb59a

Please sign in to comment.