Skip to content

Commit cf35132

Browse files
committed
PHPLIB-107: Do not throw when dropping nonexistent collection
1 parent c9f4c85 commit cf35132

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/Operation/DropCollection.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
7+
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
78
use MongoDB\Exception\RuntimeException;
89

910
/**
@@ -40,7 +41,16 @@ public function __construct($databaseName, $collectionName)
4041
*/
4142
public function execute(Server $server)
4243
{
43-
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
44+
try {
45+
$cursor = $server->executeCommand($this->databaseName, new Command(array('drop' => $this->collectionName)));
46+
} catch (DriverRuntimeException $e) {
47+
if ($e->getMessage() === 'ns not found') {
48+
$result = (object) ['ok' => 0, 'errmsg' => 'ns not found'];
49+
}
50+
51+
throw $e;
52+
}
53+
4454
$result = current($cursor->toArray());
4555

4656
if (empty($result->ok)) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Driver\Server;
6+
use MongoDB\Operation\DropCollection;
7+
use MongoDB\Operation\ListCollections;
8+
9+
class DropCollectionFunctionalTest extends FunctionalTestCase
10+
{
11+
public function testDropExistingCollection()
12+
{
13+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
14+
$this->assertEquals(1, $writeResult->getInsertedCount());
15+
16+
$server = $this->getPrimaryServer();
17+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
18+
$operation->execute($server);
19+
20+
$this->assertCollectionDoesNotExist($server, $this->getDatabaseName(), $this->getCollectionName());
21+
}
22+
23+
/**
24+
* @depends testDropExistingCollection
25+
*/
26+
public function testDropNonexistentCollection()
27+
{
28+
$server = $this->getPrimaryServer();
29+
30+
$this->assertCollectionDoesNotExist($server, $this->getDatabaseName(), $this->getCollectionName());
31+
32+
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
33+
$operation->execute($server);
34+
}
35+
36+
/**
37+
* Asserts that a collection with the given name does not exist on the
38+
* server.
39+
*
40+
* @param Server $server
41+
* @param string $databaseName
42+
* @param string $collectionName
43+
*/
44+
private function assertCollectionDoesNotExist(Server $server, $databaseName, $collectionName)
45+
{
46+
$operation = new ListCollections($databaseName);
47+
$collections = $operation->execute($server);
48+
49+
$foundCollection = null;
50+
51+
foreach ($collections as $collection) {
52+
if ($collection->getName() === $collectionName) {
53+
$foundCollection = $collection;
54+
break;
55+
}
56+
}
57+
58+
$this->assertNull($foundCollection, sprintf('Collection %s exists on the server', $collectionName));
59+
}
60+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Driver\ReadPreference;
6+
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
7+
8+
/**
9+
* Base class for Operation functional tests.
10+
*/
11+
abstract class FunctionalTestCase extends BaseFunctionalTestCase
12+
{
13+
public function getPrimaryServer()
14+
{
15+
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
16+
}
17+
}

0 commit comments

Comments
 (0)