Explorar el Código

Fix return of listDBs method

Andreas Braun hace 10 años
padre
commit
ea5d94dfb3
Se han modificado 2 ficheros con 25 adiciones y 1 borrados
  1. 14 1
      lib/Mongo/MongoClient.php
  2. 11 0
      tests/Alcaeus/MongoDbAdapter/MongoClientTest.php

+ 14 - 1
lib/Mongo/MongoClient.php

@@ -231,7 +231,20 @@ class MongoClient
      */
     public function listDBs()
     {
-        return $this->client->listDatabases();
+        $databaseInfoIterator = $this->client->listDatabases();
+
+        $databases = [
+            'databases' => [],
+            'totalSize' => 0,
+            'ok' => 1.0,
+        ];
+
+        foreach ($databaseInfoIterator as $databaseInfo) {
+            $databases['databases'][] = $databaseInfo->getName();
+            $databases['totalSize'] += $databaseInfo->getSizeOnDisk();
+        }
+
+        return $databases;
     }
 
     /**

+ 11 - 0
tests/Alcaeus/MongoDbAdapter/MongoClientTest.php

@@ -79,4 +79,15 @@ class MongoClientTest extends TestCase
         $this->assertTrue($client->setWriteConcern('majority', 100));
         $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $client->getWriteConcern());
     }
+
+    public function testListDBs()
+    {
+        $this->getCollection()->insert(['foo' => 'bar']);
+        $databases = $this->getClient()->listDBs();
+
+        $this->assertSame(1.0, $databases['ok']);
+        $this->assertArrayHasKey('totalSize', $databases);
+        $this->assertArrayHasKey('databases', $databases);
+        $this->assertContains('mongo-php-adapter', $databases['databases']);
+    }
 }