From 09143121fe8948baebf077fd54fa83434667db9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Sat, 5 Mar 2022 14:29:46 +0100 Subject: [PATCH 1/3] Fix takeUntilTimeout --- src/Illuminate/Collections/LazyCollection.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index c4822754e41e..f818a0075fd9 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -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 ($this->now() > $timeout) { + return; + } + + yield $iterator->key() => $iterator->current(); + + while ($iterator->valid() && $this->now() < $timeout) { + $iterator->next(); + + yield $iterator->key() => $iterator->current(); + } }); } From b1351bf736ca9b717bd20fdab88fed7e37da44b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Sat, 5 Mar 2022 14:36:30 +0100 Subject: [PATCH 2/3] Add extra test --- tests/Support/SupportLazyCollectionTest.php | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Support/SupportLazyCollectionTest.php b/tests/Support/SupportLazyCollectionTest.php index 51002dad66a9..b34438a2666d 100644 --- a/tests/Support/SupportLazyCollectionTest.php +++ b/tests/Support/SupportLazyCollectionTest.php @@ -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); From d0ea83a278e829a9acaf88dfd1fcc6957e5109f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Debrauwer?= Date: Sat, 5 Mar 2022 14:56:41 +0100 Subject: [PATCH 3/3] Small fix --- src/Illuminate/Collections/LazyCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index f818a0075fd9..6ccd10abb1e1 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1416,7 +1416,7 @@ public function takeUntilTimeout(DateTimeInterface $timeout) return new static(function () use ($timeout) { $iterator = $this->getIterator(); - if ($this->now() > $timeout) { + if (! $iterator->valid() || $this->now() > $timeout) { return; }