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 LazyCollection#takeUntilTimeout #41370

Merged
merged 1 commit into from
Mar 7, 2022
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
14 changes: 6 additions & 8 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1414,18 +1414,16 @@ public function takeUntilTimeout(DateTimeInterface $timeout)
$timeout = $timeout->getTimestamp();

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

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

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

while ($iterator->valid() && $this->now() < $timeout) {
$iterator->next();
foreach ($this as $key => $value) {
yield $key => $value;

yield $iterator->key() => $iterator->current();
if ($this->now() >= $timeout) {
break;
}
}
});
}
Expand Down
68 changes: 68 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Illuminate\Tests\Support;

use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\MultipleItemsFoundException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;

Expand Down Expand Up @@ -1214,6 +1216,72 @@ public function testTakeUntilIsLazy()
});
}

public function testTakeUntilTimeoutIsLazy()
{
tap(m::mock(LazyCollection::class.'[now]')->times(100), function ($mock) {
$this->assertDoesNotEnumerateCollection($mock, function ($mock) {
$timeout = Carbon::now();

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

tap(m::mock(LazyCollection::class.'[now]')->times(100), function ($mock) {
$this->assertEnumeratesCollection($mock, 1, function ($mock) {
$timeout = Carbon::now();

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

tap(m::mock(LazyCollection::class.'[now]')->times(100), function ($mock) {
$this->assertEnumeratesCollectionOnce($mock, function ($mock) {
$timeout = Carbon::now();

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

m::close();
}

public function testTakeWhileIsLazy()
{
$this->assertDoesNotEnumerate(function ($collection) {
Expand Down
26 changes: 0 additions & 26 deletions tests/Support/SupportLazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,6 @@ 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