|
| 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 | +} |
0 commit comments