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

PHPLIB-1167: Fix CachingIterator::count() on empty Cursor #1118

Merged
merged 1 commit into from
Jun 26, 2023
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
4 changes: 2 additions & 2 deletions src/Model/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public function next(): void
$this->iterator->next();

$this->storeCurrentItem();

$this->iteratorExhausted = ! $this->iterator->valid();
}

next($this->items);
Expand Down Expand Up @@ -152,6 +150,8 @@ private function exhaustIterator(): void
private function storeCurrentItem(): void
{
if (! $this->iterator->valid()) {
$this->iteratorExhausted = true;
Copy link
Member

Choose a reason for hiding this comment

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

I was looking at this last week but never submitted a review as I couldn't figure out a way to break the patch (^_^).

This addition struck me as a bit odd, since storeCurrentItem() now thinks about iteratorExhausted, but I suppose the alternative would have been copying

$this->iteratorExhausted = ! $this->iterator->valid();

...into the constructor.

I'll assume it's OK as-is, since the regression test definitely covers the original bug.

Copy link
Member Author

@GromNaN GromNaN Jun 26, 2023

Choose a reason for hiding this comment

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

The private method could have been renamed.

In fact, this does 1 less call to $this->iterator->valid() at each next() call.


return;
}

Expand Down
49 changes: 49 additions & 0 deletions tests/Model/CachingIteratorFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace MongoDB\Tests\Model;

use MongoDB\Model\CachingIterator;
use MongoDB\Tests\FunctionalTestCase;

class CachingIteratorFunctionalTest extends FunctionalTestCase
{
/** @see https://jira.mongodb.org/browse/PHPLIB-1167 */
public function testEmptyCursor(): void
{
$collection = $this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
$cursor = $collection->find();
$iterator = new CachingIterator($cursor);

$this->assertSame(0, $iterator->count());
$iterator->rewind();
$this->assertFalse($iterator->valid());
$this->assertFalse($iterator->current());
$this->assertNull($iterator->key());
}

public function testCursor(): void
{
$collection = $this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
$collection->insertOne(['_id' => 1]);
$collection->insertOne(['_id' => 2]);
$cursor = $collection->find();
$iterator = new CachingIterator($cursor);

$this->assertSame(2, $iterator->count());

$iterator->rewind();
$this->assertTrue($iterator->valid());
$this->assertNotNull($iterator->current());
$this->assertSame(0, $iterator->key());

$iterator->next();
$this->assertTrue($iterator->valid());
$this->assertNotNull($iterator->current());
$this->assertSame(1, $iterator->key());

$iterator->next();
$this->assertFalse($iterator->valid());
$this->assertFalse($iterator->current());
$this->assertNull($iterator->key());
}
}
Loading