Skip to content

Commit fde9d8e

Browse files
committed
PHPLIB-64: Collection creation method
1 parent 0ffbae8 commit fde9d8e

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/Database.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public function __construct(Manager $manager, $databaseName, WriteConcern $write
5151
*/
5252
public function createCollection($collectionName, array $options = array())
5353
{
54-
// TODO
54+
$collectionName = (string) $collectionName;
55+
$command = new Command(array('create' => $collectionName) + $options);
56+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
57+
58+
return $this->manager->executeCommand($this->databaseName, $command, $readPreference);
5559
}
5660

5761
/**

tests/DatabaseFunctionalTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use MongoDB\Client;
66
use MongoDB\Database;
7+
use MongoDB\Model\CollectionInfo;
8+
use InvalidArgumentException;
79

810
/**
911
* Functional tests for the Database class.
@@ -20,6 +22,33 @@ public function setUp()
2022
$this->database->drop();
2123
}
2224

25+
public function testCreateCollection()
26+
{
27+
$that = $this;
28+
$basicCollectionName = $this->getCollectionName() . '.basic';
29+
30+
$commandResult = $this->database->createCollection($basicCollectionName);
31+
$this->assertCommandSucceeded($commandResult);
32+
$this->assertCollectionExists($basicCollectionName, function(CollectionInfo $info) use ($that) {
33+
$that->assertFalse($info->isCapped());
34+
});
35+
36+
$cappedCollectionName = $this->getCollectionName() . '.capped';
37+
$cappedCollectionOptions = array(
38+
'capped' => true,
39+
'max' => 100,
40+
'size' => 1048576,
41+
);
42+
43+
$commandResult = $this->database->createCollection($cappedCollectionName, $cappedCollectionOptions);
44+
$this->assertCommandSucceeded($commandResult);
45+
$this->assertCollectionExists($cappedCollectionName, function(CollectionInfo $info) use ($that) {
46+
$that->assertTrue($info->isCapped());
47+
$that->assertEquals(100, $info->getCappedMax());
48+
$that->assertEquals(1048576, $info->getCappedSize());
49+
});
50+
}
51+
2352
public function testDrop()
2453
{
2554
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
@@ -48,15 +77,42 @@ public function testListCollections()
4877
$collections = $this->database->listCollections();
4978
$this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);
5079

80+
foreach ($collections as $collection) {
81+
$this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
82+
}
83+
}
84+
85+
/**
86+
* Asserts that a collection with the given name exists in the database.
87+
*
88+
* An optional $callback may be provided, which should take a CollectionInfo
89+
* argument as its first and only parameter. If a CollectionInfo matching
90+
* the given name is found, it will be passed to the callback, which may
91+
* perform additional assertions.
92+
*
93+
* @param callable $callback
94+
*/
95+
private function assertCollectionExists($collectionName, $callback = null)
96+
{
97+
if ($callback !== null && ! is_callable($callback)) {
98+
throw new InvalidArgumentException('$callback is not a callable');
99+
}
100+
101+
$collections = $this->database->listCollections();
102+
51103
$foundCollection = null;
52104

53105
foreach ($collections as $collection) {
54-
if ($collection->getName() === $this->getCollectionName()) {
106+
if ($collection->getName() === $collectionName) {
55107
$foundCollection = $collection;
56108
break;
57109
}
58110
}
59111

60-
$this->assertNotNull($foundCollection, 'Found test collection in list of collection');
112+
$this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));
113+
114+
if ($callback !== null) {
115+
call_user_func($callback, $foundCollection);
116+
}
61117
}
62118
}

0 commit comments

Comments
 (0)