4
4
5
5
use MongoDB \Client ;
6
6
use MongoDB \Database ;
7
+ use MongoDB \Model \CollectionInfo ;
8
+ use InvalidArgumentException ;
7
9
8
10
/**
9
11
* Functional tests for the Database class.
@@ -20,6 +22,33 @@ public function setUp()
20
22
$ this ->database ->drop ();
21
23
}
22
24
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
+
23
52
public function testDrop ()
24
53
{
25
54
$ writeResult = $ this ->manager ->executeInsert ($ this ->getNamespace (), array ('x ' => 1 ));
@@ -48,15 +77,42 @@ public function testListCollections()
48
77
$ collections = $ this ->database ->listCollections ();
49
78
$ this ->assertInstanceOf ('MongoDB\Model\CollectionInfoIterator ' , $ collections );
50
79
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
+
51
103
$ foundCollection = null ;
52
104
53
105
foreach ($ collections as $ collection ) {
54
- if ($ collection ->getName () === $ this -> getCollectionName () ) {
106
+ if ($ collection ->getName () === $ collectionName ) {
55
107
$ foundCollection = $ collection ;
56
108
break ;
57
109
}
58
110
}
59
111
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
+ }
61
117
}
62
118
}
0 commit comments