Browse Source

Don't pass a batch size of 0 by default

Andreas Braun 8 years ago
parent
commit
9c37267383

+ 3 - 3
lib/Alcaeus/MongoDbAdapter/AbstractCursor.php

@@ -27,9 +27,9 @@ abstract class AbstractCursor
     use ReadPreference;
 
     /**
-     * @var int
+     * @var int|null
      */
-    protected $batchSize = 0;
+    protected $batchSize = null;
 
     /**
      * @var Collection
@@ -205,7 +205,7 @@ abstract class AbstractCursor
      * Limits the number of elements returned in one batch.
      *
      * @link http://docs.php.net/manual/en/mongocursor.batchsize.php
-     * @param int $batchSize The number of results to return per batch
+     * @param int|null $batchSize The number of results to return per batch
      * @return $this Returns this cursor.
      */
     public function batchSize($batchSize)

+ 1 - 1
lib/Mongo/MongoCursor.php

@@ -468,7 +468,7 @@ class MongoCursor extends AbstractCursor implements Iterator
         return [
             'ns' => $this->ns,
             'limit' => $this->limit,
-            'batchSize' => $this->batchSize,
+            'batchSize' => (int) $this->batchSize,
             'skip' => $this->skip,
             'flags' => $this->flags,
             'query' => $this->query,

+ 40 - 2
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php

@@ -332,7 +332,7 @@ class MongoCursorTest extends TestCase
             'started_iterating' => false,
         ];
 
-        $this->assertEquals($expected, $cursor->info());
+        $this->assertSame($expected, $cursor->info());
 
         // Ensure cursor started iterating
         iterator_to_array($cursor);
@@ -348,7 +348,45 @@ class MongoCursorTest extends TestCase
             'connection_type_desc' => 'STANDALONE'
         ];
 
-        $this->assertEquals($expected, $cursor->info());
+        $this->assertSame($expected, $cursor->info());
+    }
+
+    public function testCursorInfoWithBatchSize()
+    {
+        $this->prepareData();
+
+        $collection = $this->getCollection();
+        $cursor = $collection->find(['foo' => 'bar'], ['_id' => false])->skip(1)->limit(3);
+        $cursor->batchSize(1);
+
+        $expected = [
+            'ns' => 'mongo-php-adapter.test',
+            'limit' => 3,
+            'batchSize' => 1,
+            'skip' => 1,
+            'flags' => 0,
+            'query' => ['foo' => 'bar'],
+            'fields' => ['_id' => false],
+            'started_iterating' => false,
+        ];
+
+        $this->assertSame($expected, $cursor->info());
+
+        // Ensure cursor started iterating
+        iterator_to_array($cursor);
+
+        $expected['started_iterating'] = true;
+        $expected += [
+            'id' => 0,
+            'at' => 1,
+            'numReturned' => 1,
+            'server' => 'localhost:27017;-;.;' . getmypid(),
+            'host' => 'localhost',
+            'port' => 27017,
+            'connection_type_desc' => 'STANDALONE'
+        ];
+
+        $this->assertSame($expected, $cursor->info());
     }
 
     public function testReadPreferenceIsInherited()