@@ -16,7 +16,7 @@ protected function tearDown()
1616
1717 public function testCreateCollection ()
1818 {
19- $ command = $ this -> getConnection () ->createCommand ();
19+ $ command = yii:: $ app -> mongodb ->createCommand ();
2020 $ this ->assertTrue ($ command ->createCollection ('customer ' ));
2121 }
2222
@@ -25,20 +25,20 @@ public function testCreateCollection()
2525 */
2626 public function testDropCollection ()
2727 {
28- $ command = $ this -> getConnection () ->createCommand ();
28+ $ command = yii:: $ app -> mongodb ->createCommand ();
2929 $ command ->createCollection ('customer ' );
3030 $ this ->assertTrue ($ command ->dropCollection ('customer ' ));
3131 }
3232
3333 public function testCount ()
3434 {
35- $ command = $ this -> getConnection () ->createCommand ();
35+ $ command = yii:: $ app -> mongodb ->createCommand ();
3636 $ this ->assertEquals (0 , $ command ->count ('customer ' ));
3737 }
3838
3939 public function testCreateIndexes ()
4040 {
41- $ command = $ this -> getConnection () ->createCommand ();
41+ $ command = yii:: $ app -> mongodb ->createCommand ();
4242 $ this ->assertTrue ($ command ->createIndexes ('customer ' , [
4343 [
4444 'key ' => ['name ' => +1 ],
@@ -57,7 +57,7 @@ public function testCreateIndexes()
5757 */
5858 public function testListIndexes ()
5959 {
60- $ command = $ this -> getConnection () ->createCommand ();
60+ $ command = yii:: $ app -> mongodb ->createCommand ();
6161 $ command ->createIndexes ('customer ' , [
6262 [
6363 'key ' => ['name ' => +1 ],
@@ -75,7 +75,7 @@ public function testListIndexes()
7575 */
7676 public function testDropIndexes ()
7777 {
78- $ command = $ this -> getConnection () ->createCommand ();
78+ $ command = yii:: $ app -> mongodb ->createCommand ();
7979 $ command ->createIndexes ('customer ' , [
8080 [
8181 'key ' => ['name ' => +1 ],
@@ -100,7 +100,7 @@ public function testDropIndexes()
100100
101101 public function testInsert ()
102102 {
103- $ command = $ this -> getConnection () ->createCommand ();
103+ $ command = yii:: $ app -> mongodb ->createCommand ();
104104 $ insertedId = $ command ->insert ('customer ' , ['name ' => 'John ' ]);
105105 $ this ->assertTrue ($ insertedId instanceof ObjectID);
106106 }
@@ -110,7 +110,7 @@ public function testInsert()
110110 */
111111 public function testBatchInsert ()
112112 {
113- $ command = $ this -> getConnection () ->createCommand ();
113+ $ command = yii:: $ app -> mongodb ->createCommand ();
114114 $ insertedIds = $ command ->batchInsert ('customer ' , [
115115 ['name ' => 'John ' ],
116116 ['name ' => 'Sara ' ],
@@ -124,11 +124,10 @@ public function testBatchInsert()
124124 */
125125 public function testUpdate ()
126126 {
127- $ connection = $ this ->getConnection ();
128127
129- $ newRecordId = $ connection ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
128+ $ newRecordId = yii:: $ app -> mongodb ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
130129
131- $ result = $ connection ->createCommand ()->update ('customer ' , ['_id ' => $ newRecordId ], ['name ' => 'Mike ' ]);
130+ $ result = yii:: $ app -> mongodb ->createCommand ()->update ('customer ' , ['_id ' => $ newRecordId ], ['name ' => 'Mike ' ]);
132131
133132 $ this ->assertEquals (1 , $ result ->getModifiedCount ());
134133 }
@@ -138,11 +137,10 @@ public function testUpdate()
138137 */
139138 public function testDelete ()
140139 {
141- $ connection = $ this ->getConnection ();
142140
143- $ newRecordId = $ connection ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
141+ $ newRecordId = yii:: $ app -> mongodb ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
144142
145- $ result = $ connection ->createCommand ()->delete ('customer ' , ['_id ' => $ newRecordId ]);
143+ $ result = yii:: $ app -> mongodb ->createCommand ()->delete ('customer ' , ['_id ' => $ newRecordId ]);
146144
147145 $ this ->assertEquals (1 , $ result ->getDeletedCount ());
148146 }
@@ -152,11 +150,10 @@ public function testDelete()
152150 */
153151 public function testFind ()
154152 {
155- $ connection = $ this ->getConnection ();
156153
157- $ connection ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
154+ yii:: $ app -> mongodb ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
158155
159- $ cursor = $ connection ->createCommand ()->find ('customer ' , []);
156+ $ cursor = yii:: $ app -> mongodb ->createCommand ()->find ('customer ' , []);
160157 $ rows = $ cursor ->toArray ();
161158 $ this ->assertCount (1 , $ rows );
162159 $ this ->assertEquals ('John ' , $ rows [0 ]['name ' ]);
@@ -167,7 +164,6 @@ public function testFind()
167164 */
168165 public function testFindAndModify ()
169166 {
170- $ connection = $ this ->getConnection ();
171167 $ rows = [
172168 [
173169 'name ' => 'customer 1 ' ,
@@ -180,20 +176,20 @@ public function testFindAndModify()
180176 'amount ' => 200 ,
181177 ],
182178 ];
183- $ command = $ connection ->createCommand ();
179+ $ command = yii:: $ app -> mongodb ->createCommand ();
184180 $ command ->batchInsert ('customer ' , $ rows );
185181
186182 // increment field
187- $ result = $ connection ->createCommand ()->findAndModify ('customer ' , ['name ' => 'customer 1 ' ], ['$inc ' => ['status ' => 1 ]]);
183+ $ result = yii:: $ app -> mongodb ->createCommand ()->findAndModify ('customer ' , ['name ' => 'customer 1 ' ], ['$inc ' => ['status ' => 1 ]]);
188184 $ this ->assertEquals ('customer 1 ' , $ result ['name ' ]);
189185 $ this ->assertEquals (1 , $ result ['status ' ]);
190186
191- $ cursor = $ connection ->createCommand ()->find ('customer ' , ['name ' => 'customer 1 ' ]);
187+ $ cursor = yii:: $ app -> mongodb ->createCommand ()->find ('customer ' , ['name ' => 'customer 1 ' ]);
192188 $ newResult = current ($ cursor ->toArray ());
193189 $ this ->assertEquals (2 , $ newResult ['status ' ]);
194190
195191 // $set and return modified document
196- $ result = $ connection ->createCommand ()->findAndModify (
192+ $ result = yii:: $ app -> mongodb ->createCommand ()->findAndModify (
197193 'customer ' ,
198194 ['name ' => 'customer 2 ' ],
199195 ['$set ' => ['status ' => 2 ]],
@@ -207,7 +203,7 @@ public function testFindAndModify()
207203 'name ' => 'customer 3 ' ,
208204 'city ' => 'Minsk '
209205 ];
210- $ result = $ connection ->createCommand ()->findAndModify (
206+ $ result = yii:: $ app -> mongodb ->createCommand ()->findAndModify (
211207 'customer ' ,
212208 ['name ' => 'customer 2 ' ],
213209 $ data ,
@@ -219,15 +215,14 @@ public function testFindAndModify()
219215
220216 // Test exceptions
221217 $ this ->expectException ('\yii\mongodb\Exception ' );
222- $ connection ->createCommand ()->findAndModify ('customer ' ,['name ' => 'customer 1 ' ], ['$wrongOperator ' => ['status ' => 1 ]]);
218+ yii:: $ app -> mongodb ->createCommand ()->findAndModify ('customer ' ,['name ' => 'customer 1 ' ], ['$wrongOperator ' => ['status ' => 1 ]]);
223219 }
224220
225221 /**
226222 * @depends testBatchInsert
227223 */
228224 public function testAggregate ()
229225 {
230- $ connection = $ this ->getConnection ();
231226 $ rows = [
232227 [
233228 'name ' => 'customer 1 ' ,
@@ -240,7 +235,7 @@ public function testAggregate()
240235 'amount ' => 200 ,
241236 ],
242237 ];
243- $ command = $ connection ->createCommand ();
238+ $ command = yii:: $ app -> mongodb ->createCommand ();
244239 $ command ->batchInsert ('customer ' , $ rows );
245240
246241 $ pipelines = [
@@ -256,7 +251,7 @@ public function testAggregate()
256251 ]
257252 ]
258253 ];
259- $ result = $ connection ->createCommand ()->aggregate ('customer ' , $ pipelines );
254+ $ result = yii:: $ app -> mongodb ->createCommand ()->aggregate ('customer ' , $ pipelines );
260255
261256 $ this ->assertEquals (['_id ' => '1 ' , 'total ' => 300 ], $ result [0 ]);
262257 }
@@ -268,7 +263,6 @@ public function testAggregate()
268263 */
269264 public function testAggregateCursor ()
270265 {
271- $ connection = $ this ->getConnection ();
272266 $ rows = [
273267 [
274268 'name ' => 'customer 1 ' ,
@@ -291,7 +285,7 @@ public function testAggregateCursor()
291285 'amount ' => 200 ,
292286 ],
293287 ];
294- $ command = $ connection ->createCommand ();
288+ $ command = yii:: $ app -> mongodb ->createCommand ();
295289 $ command ->batchInsert ('customer ' , $ rows );
296290
297291 $ pipelines = [
@@ -307,7 +301,7 @@ public function testAggregateCursor()
307301 ]
308302 ]
309303 ];
310- $ result = $ connection ->createCommand ()->aggregate ('customer ' , $ pipelines , ['cursor ' => ['batchSize ' => 2 ]]);
304+ $ result = yii:: $ app -> mongodb ->createCommand ()->aggregate ('customer ' , $ pipelines , ['cursor ' => ['batchSize ' => 2 ]]);
311305 $ this ->assertTrue ($ result instanceof Cursor);
312306
313307 $ this ->assertEquals (['_id ' => '1 ' , 'total ' => 600 ], $ result ->toArray ()[0 ]);
@@ -318,11 +312,10 @@ public function testAggregateCursor()
318312 */
319313 public function testExplain ()
320314 {
321- $ connection = $ this ->getConnection ();
322315
323- $ connection ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
316+ yii:: $ app -> mongodb ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
324317
325- $ result = $ connection ->createCommand ()->explain ('customer ' , [
318+ $ result = yii:: $ app -> mongodb ->createCommand ()->explain ('customer ' , [
326319 'filter ' => [
327320 'name ' => 'John '
328321 ],
@@ -337,11 +330,10 @@ public function testExplain()
337330 */
338331 public function testListCollections ()
339332 {
340- $ connection = $ this ->getConnection ();
341333
342- $ connection ->createCommand ()->createCollection ('customer ' );
334+ yii:: $ app -> mongodb ->createCommand ()->createCollection ('customer ' );
343335
344- $ collections = $ connection ->createCommand ()->listCollections ();
336+ $ collections = yii:: $ app -> mongodb ->createCommand ()->listCollections ();
345337 $ collectionNames = ArrayHelper::getColumn ($ collections , 'name ' );
346338 $ this ->assertContains ('customer ' , $ collectionNames );
347339 }
@@ -354,22 +346,21 @@ public function testListCollections()
354346 */
355347 public function testUpdateUpsert ()
356348 {
357- $ connection = $ this ->getConnection ();
358349
359- $ connection ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
350+ yii:: $ app -> mongodb ->createCommand ()->insert ('customer ' , ['name ' => 'John ' ]);
360351
361- $ result = $ connection ->createCommand ()
352+ $ result = yii:: $ app -> mongodb ->createCommand ()
362353 ->update ('customer ' , ['name ' => 'Mike ' ], ['name ' => 'Jack ' ]);
363354
364355 $ this ->assertEquals (0 , $ result ->getModifiedCount ());
365356 $ this ->assertEquals (0 , $ result ->getUpsertedCount ());
366- $ this ->assertEquals (1 , $ connection ->createCommand ()->count ('customer ' ));
357+ $ this ->assertEquals (1 , yii:: $ app -> mongodb ->createCommand ()->count ('customer ' ));
367358
368- $ result = $ connection ->createCommand ()
359+ $ result = yii:: $ app -> mongodb ->createCommand ()
369360 ->update ('customer ' , ['name ' => 'Mike ' ], ['name ' => 'Jack ' ], ['upsert ' => true ]);
370361
371362 $ this ->assertEquals (0 , $ result ->getModifiedCount ());
372363 $ this ->assertEquals (1 , $ result ->getUpsertedCount ());
373- $ this ->assertEquals (2 , $ connection ->createCommand ()->count ('customer ' ));
364+ $ this ->assertEquals (2 , yii:: $ app -> mongodb ->createCommand ()->count ('customer ' ));
374365 }
375366}
0 commit comments