Commit fde9d8ed authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-64: Collection creation method

parent 0ffbae83
......@@ -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);
}
/**
......
......@@ -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);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment