Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fix takeUntilTimeout method of LazyCollection #41354

Merged
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually still pulling that extra element. The iterator's valid method always pulls the next item 😞

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I did not know that 😞

return;
}

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

while ($iterator->valid() && $this->now() < $timeout) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. It is always pulling an extra item.

$iterator->next();

yield $iterator->key() => $iterator->current();
}
Comment on lines +1423 to +1429
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified into a single foreach block.

});
}

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();
}
Comment on lines +199 to +223
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't actually test that it's lazy. It simply tests the returned result. The actual values pulled out of the original generator are not inspected here.

Look at the SupportLazyCollectionIsLazyTest class for how to ensure it is actually lazy.

In fact, creating a laziness test for the code in this PR would fail, since it still always pulls an extra unnecessary item from the original generator.


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