Commit 793bb8bf authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-72: Use model class when listing databases

parent b5732710
...@@ -7,7 +7,8 @@ use MongoDB\Driver\Cursor; ...@@ -7,7 +7,8 @@ use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use ArrayIterator; use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Model\DatabaseInfoLegacyIterator;
use stdClass; use stdClass;
use UnexpectedValueException; use UnexpectedValueException;
...@@ -54,7 +55,7 @@ class Client ...@@ -54,7 +55,7 @@ class Client
* List databases. * List databases.
* *
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/ * @see http://docs.mongodb.org/manual/reference/command/listDatabases/
* @return Traversable * @return DatabaseInfoIterator
* @throws UnexpectedValueException if the command result is malformed * @throws UnexpectedValueException if the command result is malformed
*/ */
public function listDatabases() public function listDatabases()
...@@ -73,13 +74,13 @@ class Client ...@@ -73,13 +74,13 @@ class Client
$result['databases'] $result['databases']
); );
/* Return a Traversable instead of an array in case listDatabases is /* Return an Iterator instead of an array in case listDatabases is
* eventually changed to return a command cursor, like the collection * eventually changed to return a command cursor, like the collection
* and index enumeration commands. This makes the "totalSize" command * and index enumeration commands. This makes the "totalSize" command
* field inaccessible, but users can manually invoke the command if they * field inaccessible, but users can manually invoke the command if they
* need that value. * need that value.
*/ */
return new ArrayIterator($databases); return new DatabaseInfoLegacyIterator($databases);
} }
/** /**
......
<?php
namespace MongoDB\Model;
class DatabaseInfo
{
private $empty;
private $name;
private $sizeOnDisk;
/**
* Constructor.
*
* @param array $info Database info
*/
public function __construct(array $info)
{
$this->name = (string) $info['name'];
$this->empty = (boolean) $info['empty'];
$this->sizeOnDisk = (integer) $info['sizeOnDisk'];
}
/**
* Return the database name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Return the databases size on disk (in bytes).
*
* @return integer
*/
public function getSizeOnDisk()
{
return $this->sizeOnDisk;
}
/**
* Return whether the database is empty.
*
* @return boolean
*/
public function isEmpty()
{
return $this->empty;
}
}
<?php
namespace MongoDB\Model;
use Iterator;
interface DatabaseInfoIterator extends Iterator
{
/**
* Return the current element as a DatabaseInfo instance.
*
* @return DatabaseInfo
*/
public function current();
}
<?php
namespace MongoDB\Model;
class DatabaseInfoLegacyIterator implements DatabaseInfoIterator
{
private $databases;
private $index = 0;
/**
* Constructor.
*
* @param array $databases
*/
public function __construct(array $databases)
{
$this->databases = $databases;
}
/**
* Return the current element as a DatabaseInfo instance.
*
* @see DatabaseInfoIterator::current()
* @see http://php.net/iterator.current
* @return DatabaseInfo
*/
public function current()
{
return new DatabaseInfo(current($this->databases));
}
/**
* Return the key of the current element.
*
* @see http://php.net/iterator.key
* @return integer
*/
public function key()
{
return key($this->databases);
}
/**
* Move forward to next element.
*
* @see http://php.net/iterator.next
*/
public function next()
{
next($this->databases);
}
/**
* Rewind the Iterator to the first element.
*
* @see http://php.net/iterator.rewind
*/
public function rewind()
{
reset($this->databases);
}
/**
* Checks if current position is valid.
*
* @see http://php.net/iterator.valid
* @return boolean
*/
public function valid()
{
return key($this->databases) !== null;
}
}
...@@ -3,19 +3,30 @@ ...@@ -3,19 +3,30 @@
namespace MongoDB\Tests; namespace MongoDB\Tests;
use MongoDB\Client; use MongoDB\Client;
use MongoDB\Driver\Command;
use MongoDB\Model\DatabaseInfo;
/** /**
* Functional tests for the Client class. * Functional tests for the Client class.
*/ */
class ClientFunctionalTest extends FunctionalTestCase class ClientFunctionalTest extends FunctionalTestCase
{ {
private $client;
public function setUp()
{
parent::setUp();
$this->client = new Client($this->getUri());
$this->client->dropDatabase($this->getDatabaseName());
}
public function testDropDatabase() public function testDropDatabase()
{ {
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1)); $writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount()); $this->assertEquals(1, $writeResult->getInsertedCount());
$client = new Client($this->getUri()); $commandResult = $this->client->dropDatabase($this->getDatabaseName());
$commandResult = $client->dropDatabase($this->getDatabaseName());
$this->assertCommandSucceeded($commandResult); $this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0); $this->assertCollectionCount($this->getNamespace(), 0);
} }
...@@ -25,22 +36,52 @@ class ClientFunctionalTest extends FunctionalTestCase ...@@ -25,22 +36,52 @@ class ClientFunctionalTest extends FunctionalTestCase
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1)); $writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount()); $this->assertEquals(1, $writeResult->getInsertedCount());
$client = new Client($this->getUri()); $databases = $this->client->listDatabases();
$databases = $client->listDatabases();
$this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);
foreach ($databases as $database) {
$this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
}
$that = $this;
$this->assertDatabaseExists($this->getDatabaseName(), function(DatabaseInfo $info) use ($that) {
$that->assertFalse($info->isEmpty());
$that->assertGreaterThan(0, $info->getSizeOnDisk());
});
}
/**
* Asserts that a database with the given name exists on the server.
*
* An optional $callback may be provided, which should take a DatabaseInfo
* argument as its first and only parameter. If a DatabaseInfo matching
* the given name is found, it will be passed to the callback, which may
* perform additional assertions.
*
* @param callable $callback
*/
private function assertDatabaseExists($databaseName, $callback = null)
{
if ($callback !== null && ! is_callable($callback)) {
throw new InvalidArgumentException('$callback is not a callable');
}
$this->assertInstanceOf('Traversable', $databases); $databases = $this->client->listDatabases();
$foundDatabase = null; $foundDatabase = null;
foreach ($databases as $database) { foreach ($databases as $database) {
if ($database['name'] === $this->getDatabaseName()) { if ($database->getName() === $databaseName) {
$foundDatabase = $database; $foundDatabase = $database;
break; break;
} }
} }
$this->assertNotNull($foundDatabase, 'Found test database in list of databases'); $this->assertNotNull($foundDatabase, sprintf('Found %s database on the server', $databaseName));
$this->assertFalse($foundDatabase['empty'], 'Test database is not empty');
$this->assertGreaterThan(0, $foundDatabase['sizeOnDisk'], 'Test database takes up disk space'); if ($callback !== null) {
call_user_func($callback, $foundDatabase);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment