소스 검색

Implement MongoDB::command()

Andreas Braun 10 년 전
부모
커밋
dc81b19bd0
2개의 변경된 파일30개의 추가작업 그리고 2개의 파일을 삭제
  1. 12 2
      lib/Mongo/MongoDB.php
  2. 18 0
      tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

+ 12 - 2
lib/Mongo/MongoDB.php

@@ -377,9 +377,19 @@ class MongoDB
      * The resulting document's structure depends on the command,
      * but most results will have the ok field to indicate success or failure and results containing an array of each of the resulting documents.
      */
-    public function command(array $data, $options)
+    public function command(array $data, $options, &$hash)
     {
-        $this->notImplemented();
+        try {
+            $cursor = new \MongoCommandCursor($this->connection, $this->name, $data);
+
+            return iterator_to_array($cursor)[0];
+        } catch (\MongoDB\Driver\Exception\RuntimeException $e) {
+            return [
+                'ok' => 0,
+                'errmsg' => $e->getMessage(),
+                'code' => $e->getCode(),
+            ];
+        }
     }
 
     /**

+ 18 - 0
tests/Alcaeus/MongoDbAdapter/MongoDBTest.php

@@ -23,6 +23,24 @@ class MongoDBTest extends TestCase
         $this->assertSame('mongo-php-adapter.test', (string) $collection);
     }
 
+    public function testCommand()
+    {
+        $db = $this->getDatabase();
+        $this->assertEquals(['ok' => 1], $db->command(['ping' => 1], [], $hash));
+    }
+
+    public function testCommandError()
+    {
+        $db = $this->getDatabase();
+        $expected = [
+            'ok' => 0,
+            'errmsg' => 'listDatabases may only be run against the admin database.',
+            'code' => 13,
+        ];
+
+        $this->assertEquals($expected, $db->command(['listDatabases' => 1], [], $hash));
+    }
+
     /**
      * @return \MongoDB
      */