|
| 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