Skip to content

PHPLIB-107: Do not throw when dropping nonexistent collection #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Operation/DropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\RuntimeException;

/**
Expand Down Expand Up @@ -40,7 +41,16 @@ public function __construct($databaseName, $collectionName)
*/
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'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the $result used for here?
Is the throw below supposed to be in an else {} ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been a return statement. Thanks for catching it: #33

}

throw $e;
}

$result = current($cursor->toArray());

if (empty($result->ok)) {
Expand Down
3 changes: 2 additions & 1 deletion tests/ClientFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function testListDatabases()
* 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)
Expand All @@ -78,7 +79,7 @@ private function assertDatabaseExists($databaseName, $callback = null)
}
}

$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);
Expand Down
60 changes: 60 additions & 0 deletions tests/Operation/DropCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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));
}
}
58 changes: 58 additions & 0 deletions tests/Operation/DropDatabaseFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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));
}
}
17 changes: 17 additions & 0 deletions tests/Operation/FunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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));
}
}