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;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use ArrayIterator;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Model\DatabaseInfoLegacyIterator;
use stdClass;
use UnexpectedValueException;
......@@ -54,7 +55,7 @@ class Client
* List databases.
*
* @see http://docs.mongodb.org/manual/reference/command/listDatabases/
* @return Traversable
* @return DatabaseInfoIterator
* @throws UnexpectedValueException if the command result is malformed
*/
public function listDatabases()
......@@ -73,13 +74,13 @@ class Client
$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
* and index enumeration commands. This makes the "totalSize" command
* field inaccessible, but users can manually invoke the command if they
* 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 @@
namespace MongoDB\Tests;
use MongoDB\Client;
use MongoDB\Driver\Command;
use MongoDB\Model\DatabaseInfo;
/**
* Functional tests for the Client class.
*/
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()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$client = new Client($this->getUri());
$commandResult = $client->dropDatabase($this->getDatabaseName());
$commandResult = $this->client->dropDatabase($this->getDatabaseName());
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
......@@ -25,22 +36,52 @@ class ClientFunctionalTest extends FunctionalTestCase
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$client = new Client($this->getUri());
$databases = $client->listDatabases();
$databases = $this->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;
foreach ($databases as $database) {
if ($database['name'] === $this->getDatabaseName()) {
if ($database->getName() === $databaseName) {
$foundDatabase = $database;
break;
}
}
$this->assertNotNull($foundDatabase, 'Found test database in list of databases');
$this->assertFalse($foundDatabase['empty'], 'Test database is not empty');
$this->assertGreaterThan(0, $foundDatabase['sizeOnDisk'], 'Test database takes up disk space');
$this->assertNotNull($foundDatabase, sprintf('Found %s database on the server', $databaseName));
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