Commit abffc315 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #32

parents c2f4e5e7 3f12bc6a
......@@ -4,6 +4,7 @@ namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\RuntimeException;
/**
......@@ -40,7 +41,16 @@ class DropCollection implements Executable
*/
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
try {
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
} catch (DriverRuntimeException $e) {
if ($e->getMessage() === 'ns not found') {
$result = (object) ['ok' => 0, 'errmsg' => 'ns not found'];
}
throw $e;
}
$result = current($cursor->toArray());
if (empty($result->ok)) {
......
......@@ -59,6 +59,7 @@ class ClientFunctionalTest extends FunctionalTestCase
* the given name is found, it will be passed to the callback, which may
* perform additional assertions.
*
* @param string $databaseName
* @param callable $callback
*/
private function assertDatabaseExists($databaseName, $callback = null)
......@@ -78,7 +79,7 @@ class ClientFunctionalTest extends FunctionalTestCase
}
}
$this->assertNotNull($foundDatabase, sprintf('Found %s database on the server', $databaseName));
$this->assertNotNull($foundDatabase, sprintf('Database %s does not exist on the server', $databaseName));
if ($callback !== null) {
call_user_func($callback, $foundDatabase);
......
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\Server;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\ListCollections;
class DropCollectionFunctionalTest extends FunctionalTestCase
{
public function testDropExistingCollection()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$server = $this->getPrimaryServer();
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);
$this->assertCollectionDoesNotExist($server, $this->getDatabaseName(), $this->getCollectionName());
}
/**
* @depends testDropExistingCollection
*/
public function testDropNonexistentCollection()
{
$server = $this->getPrimaryServer();
$this->assertCollectionDoesNotExist($server, $this->getDatabaseName(), $this->getCollectionName());
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
$operation->execute($server);
}
/**
* Asserts that a collection with the given name does not exist on the
* server.
*
* @param Server $server
* @param string $databaseName
* @param string $collectionName
*/
private function assertCollectionDoesNotExist(Server $server, $databaseName, $collectionName)
{
$operation = new ListCollections($databaseName);
$collections = $operation->execute($server);
$foundCollection = null;
foreach ($collections as $collection) {
if ($collection->getName() === $collectionName) {
$foundCollection = $collection;
break;
}
}
$this->assertNull($foundCollection, sprintf('Collection %s exists on the server', $collectionName));
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\Server;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListDatabases;
class DropDatabaseFunctionalTest extends FunctionalTestCase
{
public function testDropExistingDatabase()
{
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
$this->assertEquals(1, $writeResult->getInsertedCount());
$server = $this->getPrimaryServer();
$operation = new DropDatabase($this->getDatabaseName());
$operation->execute($server);
$this->assertDatabaseDoesNotExist($server, $this->getDatabaseName());
}
/**
* @depends testDropExistingDatabase
*/
public function testDropNonexistentDatabase()
{
$server = $this->getPrimaryServer();
$this->assertDatabaseDoesNotExist($server, $this->getDatabaseName());
$operation = new DropDatabase($this->getDatabaseName());
$operation->execute($server);
}
/**
* Asserts that a database with the given name does not exist on the server.
*
* @param Server $server
* @param string $databaseName
*/
private function assertDatabaseDoesNotExist(Server $server, $databaseName)
{
$operation = new ListDatabases();
$databases = $operation->execute($server);
$foundDatabase = null;
foreach ($databases as $database) {
if ($database->getName() === $databaseName) {
$foundDatabase = $database;
break;
}
}
$this->assertNull($foundDatabase, sprintf('Database %s exists on the server', $databaseName));
}
}
<?php
namespace MongoDB\Tests\Operation;
use MongoDB\Driver\ReadPreference;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
/**
* Base class for Operation functional tests.
*/
abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
public function getPrimaryServer()
{
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
}
}
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