Skip to content

Commit

Permalink
Fix for dead cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 19, 2024
1 parent 153ea2a commit db1f368
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Iterator/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public function __construct(Traversable $iterator)
{
$this->iterator = new IteratorIterator($iterator);
$this->iterator->rewind();
$this->storeCurrentItem();
if ($this->iterator->valid()) {
$this->storeCurrentItem();
} else {
$this->iterator = null;
}
}

/** @see https://php.net/countable.count */
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Iterator/HydratingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class HydratingIterator implements Iterator
public function __construct(Traversable $traversable, private UnitOfWork $unitOfWork, private ClassMetadata $class, private array $unitOfWorkHints = [])
{
$this->iterator = new IteratorIterator($traversable);
$this->iterator->rewind();
}

public function __destruct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public function testIterationWithEmptySet(): void
self::assertFalse($iterator->valid());
}

public function testIterationWithInvalidIterator(): void
{
$mock = $this->createMock(Iterator::class);
// The method next() should not be called on a dead cursor.
$mock->expects(self::never())->method('next');
// The method valid() return false on a dead cursor.
$mock->expects(self::once())->method('valid')->willReturn(false);

$iterator = new CachingIterator($mock);

$this->assertEquals([], $iterator->toArray());
}

public function testPartialIterationDoesNotExhaust(): void
{
$traversable = $this->getTraversableThatThrows([1, 2, new Exception()]);
Expand Down

0 comments on commit db1f368

Please sign in to comment.