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

Fix wrong behavior of hasNext() #94

Merged
merged 1 commit into from
Apr 8, 2016
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
2 changes: 2 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ milestone.

* [#90](https://github.com/alcaeus/mongo-php-adapter/pull/90) ensures that database
and collection names are properly cast to string on creation.
* [#94](https://github.com/alcaeus/mongo-php-adapter/pull/94) fixes an error in
`MongoCursor::hasNext` that led to wrong data being returned.

1.0.1 (2016-04-01)
------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/Alcaeus/MongoDbAdapter/AbstractCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function __sleep()
* This is necessary because hasNext() might advance the iterator but we still
* need to be able to return the current object.
*/
private function storeIteratorState()
protected function storeIteratorState()
{
if (! $this->startedIterating) {
$this->current = null;
Expand Down
7 changes: 6 additions & 1 deletion lib/Mongo/MongoCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ public function getNext()
*/
public function hasNext()
{
if ($this->cursorNeedsAdvancing) {
if (! $this->startedIterating) {
$this->ensureIterator();
$this->startedIterating = true;
$this->storeIteratorState();
$this->cursorNeedsAdvancing = false;
} elseif ($this->cursorNeedsAdvancing) {
$this->ensureIterator()->next();
$this->cursorNeedsAdvancing = false;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,28 @@ public function testSelectCollectionWithDatabaseObject()
$collection = $client->selectCollection($database, 'test');
$this->assertSame('mongo-php-adapter.test', (string) $collection);
}

public function testHasNextLoop()
{
$collection = $this->getCollection();
for ($i = 0; $i < 5; $i++) {
$document = ['i' => $i];
$collection->insert($document);
}

$cursor = $collection->find()->sort(['i' => 1]);
$data = [];
$i = 0;
while ($cursor->hasNext()) {
$this->assertSame($i < 5, $cursor->hasNext());
$row = $cursor->getNext();
$this->assertSame($i, $row['i']);
$data[] = $row;
$i++;
}

$this->assertCount(5, $data);
}
}

class PrivatePropertiesStub
Expand Down