Skip to content

Commit abffc31

Browse files
committed
Merge pull request #32
2 parents c2f4e5e + 3f12bc6 commit abffc31

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-2
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)) {

tests/ClientFunctionalTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function testListDatabases()
5959
* the given name is found, it will be passed to the callback, which may
6060
* perform additional assertions.
6161
*
62+
* @param string $databaseName
6263
* @param callable $callback
6364
*/
6465
private function assertDatabaseExists($databaseName, $callback = null)
@@ -78,7 +79,7 @@ private function assertDatabaseExists($databaseName, $callback = null)
7879
}
7980
}
8081

81-
$this->assertNotNull($foundDatabase, sprintf('Found %s database on the server', $databaseName));
82+
$this->assertNotNull($foundDatabase, sprintf('Database %s does not exist on the server', $databaseName));
8283

8384
if ($callback !== null) {
8485
call_user_func($callback, $foundDatabase);
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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Operation;
4+
5+
use MongoDB\Driver\Server;
6+
use MongoDB\Operation\DropDatabase;
7+
use MongoDB\Operation\ListDatabases;
8+
9+
class DropDatabaseFunctionalTest extends FunctionalTestCase
10+
{
11+
public function testDropExistingDatabase()
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 DropDatabase($this->getDatabaseName());
18+
$operation->execute($server);
19+
20+
$this->assertDatabaseDoesNotExist($server, $this->getDatabaseName());
21+
}
22+
23+
/**
24+
* @depends testDropExistingDatabase
25+
*/
26+
public function testDropNonexistentDatabase()
27+
{
28+
$server = $this->getPrimaryServer();
29+
30+
$this->assertDatabaseDoesNotExist($server, $this->getDatabaseName());
31+
32+
$operation = new DropDatabase($this->getDatabaseName());
33+
$operation->execute($server);
34+
}
35+
36+
/**
37+
* Asserts that a database with the given name does not exist on the server.
38+
*
39+
* @param Server $server
40+
* @param string $databaseName
41+
*/
42+
private function assertDatabaseDoesNotExist(Server $server, $databaseName)
43+
{
44+
$operation = new ListDatabases();
45+
$databases = $operation->execute($server);
46+
47+
$foundDatabase = null;
48+
49+
foreach ($databases as $database) {
50+
if ($database->getName() === $databaseName) {
51+
$foundDatabase = $database;
52+
break;
53+
}
54+
}
55+
56+
$this->assertNull($foundDatabase, sprintf('Database %s exists on the server', $databaseName));
57+
}
58+
}
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)