Skip to content

Merge branch 'v1.16' into master #1119

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

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;

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());
}
}