Skip to content

Commit 3f12bc6

Browse files
committed
DropDatabase functional tests
1 parent cf35132 commit 3f12bc6

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
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+
}

0 commit comments

Comments
 (0)