Explorar el Código

Add tests for MongoCommandCursor

Andreas Braun hace 10 años
padre
commit
59166c7be1

+ 1 - 1
lib/Mongo/MongoCommandCursor.php

@@ -67,7 +67,7 @@ class MongoCommandCursor extends AbstractCursor implements MongoCursorInterface
         return [
             'ns' => $this->ns,
             'limit' => 0,
-            'batchSize' => 0,
+            'batchSize' => $this->batchSize,
             'skip' => 0,
             'flags' => 0,
             'query' => $this->command,

+ 77 - 0
tests/Alcaeus/MongoDbAdapter/MongoCommandCursorTest.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace Alcaeus\MongoDbAdapter\Tests;
+use MongoDB\Driver\ReadPreference;
+use MongoDB\Operation\Find;
+
+/**
+ * @author alcaeus <alcaeus@alcaeus.org>
+ */
+class MongoCommandCursorTest extends TestCase
+{
+    public function testInfo()
+    {
+        $this->prepareData();
+        $cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
+
+        $expected = [
+            'ns' => 'mongo-php-adapter.test',
+            'limit' => 0,
+            'batchSize' => null,
+            'skip' => 0,
+            'flags' => 0,
+            'query' => [
+                'aggregate' => 'test',
+                'pipeline' => [
+                    [
+                        '$match' => ['foo' => 'bar']
+                    ]
+                ],
+                'cursor' => new \stdClass()
+            ],
+            'fields' => null,
+            'started_iterating' => false,
+        ];
+        $this->assertEquals($expected, $cursor->info());
+
+        // Ensure cursor started iterating
+        iterator_to_array($cursor);
+
+        $expected['started_iterating'] = true;
+        $expected += [
+            'id' => '0',
+            'at' => null,
+            'numReturned' => null,
+            'server' => null,
+            'host' => 'localhost',
+            'port' => 27017,
+            'connection_type_desc' => 'STANDALONE'
+        ];
+
+        $this->assertEquals($expected, $cursor->info());
+    }
+
+    /**
+     * @param string $name
+     * @return \MongoCollection
+     */
+    protected function getCollection($name = 'test')
+    {
+        $client = new \MongoClient();
+
+        return $client->selectCollection('mongo-php-adapter', $name);
+    }
+
+    /**
+     * @return \MongoCollection
+     */
+    protected function prepareData()
+    {
+        $collection = $this->getCollection();
+
+        $collection->insert(['foo' => 'bar']);
+        $collection->insert(['foo' => 'bar']);
+        $collection->insert(['foo' => 'foo']);
+        return $collection;
+    }
+}