Commit 10ea91c2 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-65: Database drop methods

parent b85b91fb
......@@ -2,8 +2,7 @@
namespace MongoDB;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
......@@ -35,12 +34,17 @@ class Client
/**
* Drop a database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @param string $databaseName
* @return Result
*/
public function dropDatabase($databaseName)
{
// TODO
$databaseName = (string) $databaseName;
$command = new Command(array('dropDatabase' => 1));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
return $this->manager->executeCommand($databaseName, $command, $readPreference);
}
/**
......
......@@ -3,6 +3,7 @@
namespace MongoDB;
use MongoDB\Collection;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
......@@ -51,11 +52,15 @@ class Database
/**
* Drop this database.
*
* @see http://docs.mongodb.org/manual/reference/command/dropDatabase/
* @return Result
*/
public function drop()
{
// TODO
$command = new Command(array('dropDatabase' => 1));
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
return $this->manager->executeCommand($this->databaseName, $command, $readPreference);
}
/**
......
<?php
namespace MongoDB\Tests;
use MongoDB\Client;
/**
* Functional tests for the Client class.
*/
class ClientFunctionalTest extends FunctionalTestCase
{
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());
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
}
<?php
namespace MongoDB\Tests;
use MongoDB\Client;
use MongoDB\Database;
/**
* Functional tests for the Database class.
*/
class DatabaseFunctionalTest extends FunctionalTestCase
{
public function testDrop()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$database = new Database($this->manager, $this->getDatabaseName());
$commandResult = $database->drop();
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
}
......@@ -2,7 +2,9 @@
namespace MongoDB\Tests;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Result;
abstract class FunctionalTestCase extends TestCase
{
......@@ -12,4 +14,22 @@ abstract class FunctionalTestCase extends TestCase
{
$this->manager = new Manager($this->getUri());
}
public function assertCollectionCount($namespace, $count)
{
list($databaseName, $collectionName) = explode('.', $namespace, 2);
$result = $this->manager->executeCommand($databaseName, new Command(array('count' => $collectionName)));
$document = $result->toArray();
$this->assertArrayHasKey('n', $document);
$this->assertEquals($count, $document['n']);
}
public function assertCommandSucceeded(Result $result)
{
$document = $result->toArray();
$this->assertArrayHasKey('ok', $document);
$this->assertEquals(1, $document['ok']);
}
}
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