Skip to content

Commit f1b1cd4

Browse files
committed
PHPLIB-72: Database enumeration method
1 parent 30ced49 commit f1b1cd4

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Client.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use MongoDB\Driver\ReadPreference;
88
use MongoDB\Driver\Result;
99
use MongoDB\Driver\WriteConcern;
10+
use ArrayIterator;
11+
use stdClass;
12+
use UnexpectedValueException;
1013

1114
class Client
1215
{
@@ -47,6 +50,39 @@ public function dropDatabase($databaseName)
4750
return $this->manager->executeCommand($databaseName, $command, $readPreference);
4851
}
4952

53+
/**
54+
* List databases.
55+
*
56+
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
57+
* @return Traversable
58+
* @throws UnexpectedValueException if the command result is malformed
59+
*/
60+
public function listDatabases()
61+
{
62+
$command = new Command(array('listDatabases' => 1));
63+
64+
$result = $this->manager->executeCommand('admin', $command);
65+
$result = iterator_to_array($result);
66+
$result = current($result);
67+
68+
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
69+
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
70+
}
71+
72+
$databases = array_map(
73+
function(stdClass $database) { return (array) $database; },
74+
$result['databases']
75+
);
76+
77+
/* Return a Traversable instead of an array in case listDatabases is
78+
* eventually changed to return a command cursor, like the collection
79+
* and index enumeration commands. This makes the "totalSize" command
80+
* field inaccessible, but users can manually invoke the command if they
81+
* need that value.
82+
*/
83+
return new ArrayIterator($databases);
84+
}
85+
5086
/**
5187
* Select a database.
5288
*

tests/ClientFunctionalTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,28 @@ public function testDropDatabase()
1919
$this->assertCommandSucceeded($commandResult);
2020
$this->assertCollectionCount($this->getNamespace(), 0);
2121
}
22+
23+
public function testListDatabases()
24+
{
25+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
26+
$this->assertEquals(1, $writeResult->getInsertedCount());
27+
28+
$client = new Client($this->getUri());
29+
$databases = $client->listDatabases();
30+
31+
$this->assertInstanceOf('Traversable', $databases);
32+
33+
$foundDatabase = null;
34+
35+
foreach ($databases as $database) {
36+
if ($database['name'] === $this->getDatabaseName()) {
37+
$foundDatabase = $database;
38+
break;
39+
}
40+
}
41+
42+
$this->assertNotNull($foundDatabase, 'Found test database in list of databases');
43+
$this->assertFalse($foundDatabase['empty'], 'Test database is not empty');
44+
$this->assertGreaterThan(0, $foundDatabase['sizeOnDisk'], 'Test database takes up disk space');
45+
}
2246
}

0 commit comments

Comments
 (0)