Ver código fonte

Merge pull request #191 from robwil/fix_mongocursor_iterator

Fix mongocursor iterator
Andreas 8 anos atrás
pai
commit
2c0e8db888

+ 13 - 0
README.md

@@ -122,3 +122,16 @@ unserializing them.
  fields always return 0 for compatibility to MongoCursor. The `firstBatchAt` and
  `firstBatchNumReturned` fields will contain the same value, which is the internal
  position of the iterator.
+
+# Development
+
+If you are working on patches to this driver, you can run the unit tests by following these steps from the root of the repo directory:
+
+    $ composer install
+    $ vendor/phpunit/phpunit/phpunit --verbose
+
+It assumes that the the `localhost` is running a mongod server. Here is a sample command to start mongod for these tests:
+
+    $ mongod --smallfiles --fork --logpath /var/log/mongod.log --setParameter enableTestCommands=1
+
+The tests also assume PHP 5.6+ and the `ext-mongodb` extension being available.

+ 1 - 0
lib/Alcaeus/MongoDbAdapter/AbstractCursor.php

@@ -381,6 +381,7 @@ abstract class AbstractCursor
     protected function reset()
     {
         $this->startedIterating = false;
+        $this->cursorNeedsAdvancing = true;
         $this->cursor = null;
         $this->iterator = null;
         $this->storeIteratorState();

+ 16 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

@@ -28,6 +28,22 @@ class MongoCursorTest extends TestCase
         $cursor = $collection->find(['foo' => 'bar']);
         $this->assertCount(2, $cursor);
 
+        $this->assertCursorIteration($cursor);
+    }
+
+    public function testCursorHandlesHasNextBeforeIteration()
+    {
+        $this->prepareData();
+
+        $collection = $this->getCollection();
+        $cursor = $collection->find(['foo' => 'bar']);
+        $this->assertTrue($cursor->hasNext());
+
+        $this->assertCursorIteration($cursor);
+    }
+
+    private function assertCursorIteration($cursor)
+    {
         $iterated = 0;
         foreach ($cursor as $key => $item) {
             $this->assertSame($iterated, $cursor->info()['at']);