From 0b27c0ee07ff81b7ae207d6bde9f12441ed6adfa Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 8 Apr 2016 06:03:39 +0200 Subject: [PATCH] Fix wrong behavior of hasNext() Fixes #91. --- CHANGELOG-1.0.md | 2 ++ lib/Alcaeus/MongoDbAdapter/AbstractCursor.php | 2 +- lib/Mongo/MongoCursor.php | 7 +++++- .../Mongo/MongoCollectionTest.php | 22 +++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG-1.0.md b/CHANGELOG-1.0.md index c5c12298..ca45dd44 100644 --- a/CHANGELOG-1.0.md +++ b/CHANGELOG-1.0.md @@ -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) ------------------ diff --git a/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php b/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php index 02c82a5a..93e9a944 100644 --- a/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php +++ b/lib/Alcaeus/MongoDbAdapter/AbstractCursor.php @@ -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; diff --git a/lib/Mongo/MongoCursor.php b/lib/Mongo/MongoCursor.php index 90977260..b5a6da2d 100644 --- a/lib/Mongo/MongoCursor.php +++ b/lib/Mongo/MongoCursor.php @@ -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; } diff --git a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php index 8043e990..dd86a7d2 100644 --- a/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php +++ b/tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php @@ -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