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

Add test for info() method in cursor

Andreas Braun 10 лет назад
Родитель
Сommit
b708d85b97

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

@@ -344,16 +344,16 @@ abstract class AbstractCursor
 
         if ($this->cursor !== null) {
             switch ($this->cursor->getServer()->getType()) {
-                case \MongoDB\Driver\Server::TYPE_ARBITER:
+                case \MongoDB\Driver\Server::TYPE_RS_ARBITER:
                     $typeString = 'ARBITER';
                     break;
                 case \MongoDB\Driver\Server::TYPE_MONGOS:
                     $typeString = 'MONGOS';
                     break;
-                case \MongoDB\Driver\Server::TYPE_PRIMARY:
+                case \MongoDB\Driver\Server::TYPE_RS_PRIMARY:
                     $typeString = 'PRIMARY';
                     break;
-                case \MongoDB\Driver\Server::TYPE_SECONDARY:
+                case \MongoDB\Driver\Server::TYPE_RS_SECONDARY:
                     $typeString = 'SECONDARY';
                     break;
                 default:
@@ -361,7 +361,7 @@ abstract class AbstractCursor
             }
 
             $iterationInfo += [
-                'id' => (string)$this->cursor->getId(),
+                'id' => (string) $this->cursor->getId(),
                 'at' => null, // @todo Complete info for cursor that is iterating
                 'numReturned' => null, // @todo Complete info for cursor that is iterating
                 'server' => null, // @todo Complete info for cursor that is iterating

+ 1 - 2
lib/Mongo/MongoCursor.php

@@ -63,7 +63,7 @@ class MongoCursor extends AbstractCursor implements Iterator
 
     protected $allowPartialResults;
     protected $awaitData;
-    protected $flags;
+    protected $flags = 0;
     protected $hint;
     protected $limit;
     protected $maxTimeMS;
@@ -144,7 +144,6 @@ class MongoCursor extends AbstractCursor implements Iterator
         return $count;
     }
 
-
     /**
      * Execute the query
      * @link http://www.php.net/manual/en/mongocursor.doquery.php

+ 37 - 0
tests/Alcaeus/MongoDbAdapter/MongoCursorTest.php

@@ -201,6 +201,43 @@ class MongoCursorTest extends TestCase
         return $tests;
     }
 
+    public function testCursorInfo()
+    {
+        $this->prepareData();
+
+        $collection = $this->getCollection();
+        $cursor = $collection->find(['foo' => 'bar'], ['_id' => false])->skip(1)->limit(3);
+
+        $expected = [
+            'ns' => 'mongo-php-adapter.test',
+            'limit' => 3,
+            'batchSize' => null,
+            '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' => null,
+            'numReturned' => null,
+            'server' => null,
+            'host' => 'localhost',
+            'port' => 27017,
+            'connection_type_desc' => 'STANDALONE'
+        ];
+
+        $this->assertSame($expected, $cursor->info());
+    }
+
     /**
      * @param string $name
      * @return \MongoCollection