Просмотр исходного кода

Fix erorr when counting a closed cursor

Fixes #95.
Andreas Braun 9 лет назад
Родитель
Сommit
124a4a7dc2

+ 12 - 0
CHANGELOG-1.0.md

@@ -7,6 +7,18 @@ This changelog references the relevant changes done in minor version updates.
 ------------------
 
 All issues and pull requests under this release may be found under the
+[1.0.3](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.3)
+milestone.
+
+ * [#96](https://github.com/alcaeus/mongo-php-adapter/pull/96) fixes errors when
+ calling `count` on a cursor that has been iterated fully. The fix removes a
+ performance improvement when calling `count` on a cursor that has been opened.
+ `MongoCursor::count` now always re-issues a `count` command to the server.
+
+1.0.2 (2016-04-08)
+------------------
+
+All issues and pull requests under this release may be found under the
 [1.0.2](https://github.com/alcaeus/mongo-php-adapter/issues?q=milestone%3A1.0.2)
 milestone.
 

+ 0 - 4
lib/Mongo/MongoCursor.php

@@ -135,10 +135,6 @@ class MongoCursor extends AbstractCursor implements Iterator
      */
     public function count($foundOnly = false)
     {
-        if ($foundOnly && $this->cursor !== null) {
-            return iterator_count($this->ensureIterator());
-        }
-
         $optionNames = ['hint', 'maxTimeMS'];
         if ($foundOnly) {
             $optionNames = array_merge($optionNames, ['limit', 'skip']);

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

@@ -60,6 +60,18 @@ class MongoCursorTest extends TestCase
         $cursor->count();
     }
 
+    public function testCountAfterIteration()
+    {
+        $this->prepareData();
+
+        $collection = $this->getCollection();
+        $cursor = $collection->find(['foo' => 'bar']);
+
+        // Ensure the generator is consumed and thus closed
+        iterator_to_array($cursor);
+        $this->assertSame(2, $cursor->count(true));
+    }
+
     public function testNextStartsWithFirstItem()
     {
         $this->prepareData();