diff --git a/src/Database.php b/src/Database.php index 2c272783c2bf71c0703d26d18bbf6c1cbc7f11b5..2e244e83f4b1a30df5e152562aef826782b4e4a9 100644 --- a/src/Database.php +++ b/src/Database.php @@ -51,7 +51,11 @@ class Database */ public function createCollection($collectionName, array $options = array()) { - // TODO + $collectionName = (string) $collectionName; + $command = new Command(array('create' => $collectionName) + $options); + $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY); + + return $this->manager->executeCommand($this->databaseName, $command, $readPreference); } /** diff --git a/tests/DatabaseFunctionalTest.php b/tests/DatabaseFunctionalTest.php index 19bafcce752c9a866fa4e4cb40441d85dc721127..607eff6590b3935428f7e0588a1f9f5bad4f7868 100644 --- a/tests/DatabaseFunctionalTest.php +++ b/tests/DatabaseFunctionalTest.php @@ -4,6 +4,8 @@ namespace MongoDB\Tests; use MongoDB\Client; use MongoDB\Database; +use MongoDB\Model\CollectionInfo; +use InvalidArgumentException; /** * Functional tests for the Database class. @@ -20,6 +22,33 @@ class DatabaseFunctionalTest extends FunctionalTestCase $this->database->drop(); } + public function testCreateCollection() + { + $that = $this; + $basicCollectionName = $this->getCollectionName() . '.basic'; + + $commandResult = $this->database->createCollection($basicCollectionName); + $this->assertCommandSucceeded($commandResult); + $this->assertCollectionExists($basicCollectionName, function(CollectionInfo $info) use ($that) { + $that->assertFalse($info->isCapped()); + }); + + $cappedCollectionName = $this->getCollectionName() . '.capped'; + $cappedCollectionOptions = array( + 'capped' => true, + 'max' => 100, + 'size' => 1048576, + ); + + $commandResult = $this->database->createCollection($cappedCollectionName, $cappedCollectionOptions); + $this->assertCommandSucceeded($commandResult); + $this->assertCollectionExists($cappedCollectionName, function(CollectionInfo $info) use ($that) { + $that->assertTrue($info->isCapped()); + $that->assertEquals(100, $info->getCappedMax()); + $that->assertEquals(1048576, $info->getCappedSize()); + }); + } + public function testDrop() { $writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1)); @@ -48,15 +77,42 @@ class DatabaseFunctionalTest extends FunctionalTestCase $collections = $this->database->listCollections(); $this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections); + foreach ($collections as $collection) { + $this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection); + } + } + + /** + * Asserts that a collection with the given name exists in the database. + * + * An optional $callback may be provided, which should take a CollectionInfo + * argument as its first and only parameter. If a CollectionInfo matching + * the given name is found, it will be passed to the callback, which may + * perform additional assertions. + * + * @param callable $callback + */ + private function assertCollectionExists($collectionName, $callback = null) + { + if ($callback !== null && ! is_callable($callback)) { + throw new InvalidArgumentException('$callback is not a callable'); + } + + $collections = $this->database->listCollections(); + $foundCollection = null; foreach ($collections as $collection) { - if ($collection->getName() === $this->getCollectionName()) { + if ($collection->getName() === $collectionName) { $foundCollection = $collection; break; } } - $this->assertNotNull($foundCollection, 'Found test collection in list of collection'); + $this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName)); + + if ($callback !== null) { + call_user_func($callback, $foundCollection); + } } }