1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace MongoDB\Tests\Database;
use MongoDB\Driver\BulkWrite;
use MongoDB\Model\CollectionInfo;
use InvalidArgumentException;
/**
* Functional tests for collection management methods.
*/
class CollectionManagementFunctionalTest extends FunctionalTestCase
{
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 = [
'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 testDropCollection()
{
$bulkWrite = new BulkWrite();
$bulkWrite->insert(['x' => 1]);
$writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->database->dropCollection($this->getCollectionName());
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
public function testListCollections()
{
$commandResult = $this->database->createCollection($this->getCollectionName());
$this->assertCommandSucceeded($commandResult);
$collections = $this->database->listCollections();
$this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);
foreach ($collections as $collection) {
$this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
}
}
public function testListCollectionsWithFilter()
{
$commandResult = $this->database->createCollection($this->getCollectionName());
$this->assertCommandSucceeded($commandResult);
$collectionName = $this->getCollectionName();
$options = ['filter' => ['name' => $collectionName]];
$collections = $this->database->listCollections($options);
$this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);
foreach ($collections as $collection) {
$this->assertInstanceOf('MongoDB\Model\CollectionInfo', $collection);
$this->assertEquals($collectionName, $collection->getName());
}
}
/**
* 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() === $collectionName) {
$foundCollection = $collection;
break;
}
}
$this->assertNotNull($foundCollection, sprintf('Found %s collection in the database', $collectionName));
if ($callback !== null) {
call_user_func($callback, $foundCollection);
}
}
}