Commit cf351325 authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-107: Do not throw when dropping nonexistent collection

parent c9f4c850
......@@ -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)
{
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)) {
......
<?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\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