Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 756825e

Browse files
committedJul 17, 2020
test case improving
default db is `mongodb` component in across all application. so no need `getConnection` method in everywhere except in special cases
1 parent e0140f8 commit 756825e

34 files changed

+244
-375
lines changed
 

‎tests/ActiveDataProviderTest.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use MongoDB\BSON\ObjectID;
66
use yii\data\ActiveDataProvider;
77
use yii\mongodb\Query;
8-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
98
use yiiunit\extensions\mongodb\data\ar\Customer;
109

1110
class ActiveDataProviderTest extends TestCase
@@ -14,7 +13,6 @@ protected function setUp()
1413
{
1514
parent::setUp();
1615
$this->mockApplication();
17-
ActiveRecord::$db = $this->getConnection();
1816
$this->setUpTestRows();
1917
}
2018

@@ -29,7 +27,7 @@ protected function tearDown()
2927
*/
3028
protected function setUpTestRows()
3129
{
32-
$collection = $this->getConnection()->getCollection('customer');
30+
$collection = yii::$app->mongodb->getCollection('customer');
3331
$rows = [];
3432
for ($i = 1; $i <= 10; $i++) {
3533
$rows[] = [
@@ -51,14 +49,12 @@ public function testQuery()
5149

5250
$provider = new ActiveDataProvider([
5351
'query' => $query,
54-
'db' => $this->getConnection(),
5552
]);
5653
$models = $provider->getModels();
5754
$this->assertEquals(10, count($models));
5855

5956
$provider = new ActiveDataProvider([
6057
'query' => $query,
61-
'db' => $this->getConnection(),
6258
'pagination' => [
6359
'pageSize' => 5,
6460
]

‎tests/ActiveFixtureTest.php‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testLoadCollection()
3232
$fixture = $this->getMockBuilder(ActiveFixture::className())
3333
->setConstructorArgs([
3434
[
35-
'db' => $this->getConnection(),
35+
'db' => yii::$app->mongodb,
3636
'collectionName' => Customer::collectionName()
3737
]
3838
])
@@ -45,7 +45,7 @@ public function testLoadCollection()
4545

4646
$fixture->load();
4747

48-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
48+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
4949
$this->assertCount(2, $rows);
5050
}
5151

@@ -55,7 +55,7 @@ public function testLoadClass()
5555
$fixture = $this->getMockBuilder(ActiveFixture::className())
5656
->setConstructorArgs([
5757
[
58-
'db' => $this->getConnection(),
58+
'db' => yii::$app->mongodb,
5959
'collectionName' => Customer::collectionName()
6060
]
6161
])
@@ -68,7 +68,7 @@ public function testLoadClass()
6868

6969
$fixture->load();
7070

71-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
71+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
7272
$this->assertCount(2, $rows);
7373
}
7474

@@ -83,7 +83,7 @@ public function testLoadEmptyData()
8383
$fixture = $this->getMockBuilder(ActiveFixture::className())
8484
->setConstructorArgs([
8585
[
86-
'db' => $this->getConnection(),
86+
'db' => yii::$app->mongodb,
8787
'collectionName' => Customer::collectionName()
8888
]
8989
])
@@ -95,7 +95,7 @@ public function testLoadEmptyData()
9595

9696
$fixture->load(); // should be no error
9797

98-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
98+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
9999
$this->assertEmpty($rows);
100100
}
101101

@@ -106,7 +106,6 @@ public function testLoadEmptyData()
106106
*/
107107
public function testDefaultDataFile()
108108
{
109-
$db = $this->getConnection();
110109

111110
$fixturePath = Yii::getAlias('@runtime/fixtures');
112111
$fixtureDataPath = $fixturePath . DIRECTORY_SEPARATOR . 'data';
@@ -138,29 +137,27 @@ class {$className} extends \yii\mongodb\ActiveFixture
138137
['name' => 'name2'],
139138
['name' => 'name3'],
140139
];
141-
$fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . $db->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php';
140+
$fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . yii::$app->mongodb->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php';
142141
$fixtureDataContent = '<?php return ' . VarDumper::export($fixtureData) . ';';
143142
file_put_contents($fixtureDataFile, $fixtureDataContent);
144143

145144
/* @var $fixture ActiveFixture */
146145

147146
$fixture = new $className([
148-
'db' => $db,
149147
'collectionName' => Customer::collectionName(),
150148
]);
151149
$fixture->load();
152-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
150+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
153151
$this->assertCount(2, $rows);
154152

155153
$fixture = new $className([
156-
'db' => $db,
157154
'collectionName' => [
158-
$db->getDefaultDatabaseName(),
155+
yii::$app->mongodb->getDefaultDatabaseName(),
159156
Customer::collectionName()
160157
],
161158
]);
162159
$fixture->load();
163-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
160+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
164161
$this->assertCount(3, $rows);
165162
}
166163
}

‎tests/ActiveRecordTest.php‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use MongoDB\BSON\ObjectID;
77
use MongoDB\BSON\Regex;
88
use yii\mongodb\ActiveQuery;
9-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
109
use yiiunit\extensions\mongodb\data\ar\Customer;
1110
use yiiunit\extensions\mongodb\data\ar\Animal;
1211
use yiiunit\extensions\mongodb\data\ar\Dog;
@@ -22,7 +21,6 @@ class ActiveRecordTest extends TestCase
2221
protected function setUp()
2322
{
2423
parent::setUp();
25-
ActiveRecord::$db = $this->getConnection();
2624
$this->setUpTestRows();
2725
}
2826

@@ -37,7 +35,7 @@ protected function tearDown()
3735
*/
3836
protected function setUpTestRows()
3937
{
40-
$collection = $this->getConnection()->getCollection('customer');
38+
$collection = yii::$app->mongodb->getCollection('customer');
4139
$rows = [];
4240
for ($i = 1; $i <= 10; $i++) {
4341
$rows[] = [
@@ -282,66 +280,64 @@ public function testExists()
282280

283281
public function testScalar()
284282
{
285-
$connection = $this->getConnection();
286283

287284
$result = Customer::find()
288285
->select(['name' => true, '_id' => false])
289286
->orderBy(['name' => SORT_ASC])
290287
->limit(1)
291-
->scalar($connection);
288+
->scalar();
292289
$this->assertSame('name1', $result);
293290

294291
$result = Customer::find()
295292
->select(['name' => true, '_id' => false])
296293
->andWhere(['status' => -1])
297-
->scalar($connection);
294+
->scalar();
298295
$this->assertSame(false, $result);
299296

300297
$result = Customer::find()
301298
->select(['name'])
302299
->orderBy(['name' => SORT_ASC])
303300
->limit(1)
304-
->scalar($connection);
301+
->scalar();
305302
$this->assertSame('name1', $result);
306303

307304
$result = Customer::find()
308305
->select(['_id'])
309306
->limit(1)
310-
->scalar($connection);
307+
->scalar();
311308
$this->assertTrue($result instanceof ObjectID);
312309
}
313310

314311
public function testColumn()
315312
{
316-
$connection = $this->getConnection();
317313

318314
$result = Customer::find()
319315
->select(['name' => true, '_id' => false])
320316
->orderBy(['name' => SORT_ASC])
321317
->limit(2)
322-
->column($connection);
318+
->column();
323319
$this->assertEquals(['name1', 'name10'], $result);
324320

325321
$result = Customer::find()
326322
->select(['name' => true, '_id' => false])
327323
->andWhere(['status' => -1])
328324
->orderBy(['name' => SORT_ASC])
329325
->limit(2)
330-
->column($connection);
326+
->column();
331327
$this->assertEquals([], $result);
332328

333329
$result = Customer::find()
334330
->select(['name'])
335331
->orderBy(['name' => SORT_ASC])
336332
->limit(2)
337-
->column($connection);
333+
->column();
338334
$this->assertEquals(['name1', 'name10'], $result);
339335

340336
$result = Customer::find()
341337
->select(['_id'])
342338
->orderBy(['name' => SORT_ASC])
343339
->limit(2)
344-
->column($connection);
340+
->column();
345341
$this->assertTrue($result[0] instanceof ObjectID);
346342
$this->assertTrue($result[1] instanceof ObjectID);
347343
}

‎tests/ActiveRelationTest.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace yiiunit\extensions\mongodb;
44

5-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
65
use yiiunit\extensions\mongodb\data\ar\Customer;
76
use yiiunit\extensions\mongodb\data\ar\CustomerOrder;
87
use yiiunit\extensions\mongodb\data\ar\Item;
@@ -12,7 +11,6 @@ class ActiveRelationTest extends TestCase
1211
protected function setUp()
1312
{
1413
parent::setUp();
15-
ActiveRecord::$db = $this->getConnection();
1614
$this->setUpTestRows();
1715
}
1816

@@ -38,7 +36,7 @@ protected function setUpTestRows()
3836
'status' => $i,
3937
];
4038
}
41-
$customerCollection = $this->getConnection()->getCollection('customer');
39+
$customerCollection = yii::$app->mongodb->getCollection('customer');
4240
$customers = $customerCollection->batchInsert($customers);
4341

4442
$items = [];
@@ -48,7 +46,7 @@ protected function setUpTestRows()
4846
'price' => $i,
4947
];
5048
}
51-
$itemCollection = $this->getConnection()->getCollection('item');
49+
$itemCollection = yii::$app->mongodb->getCollection('item');
5250
$items = $itemCollection->batchInsert($items);
5351

5452
$customerOrders = [];
@@ -70,7 +68,7 @@ protected function setUpTestRows()
7068
],
7169
];
7270
}
73-
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
71+
$customerOrderCollection = yii::$app->mongodb->getCollection('customer_order');
7472
$customerOrderCollection->batchInsert($customerOrders);
7573
}
7674

‎tests/BatchQueryResultTest.php‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use yii\mongodb\BatchQueryResult;
66
use yii\mongodb\Query;
7-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
87
use yiiunit\extensions\mongodb\data\ar\Customer;
98
use yiiunit\extensions\mongodb\data\ar\CustomerOrder;
109

@@ -13,7 +12,6 @@ class BatchQueryResultTest extends TestCase
1312
protected function setUp()
1413
{
1514
parent::setUp();
16-
ActiveRecord::$db = $this->getConnection();
1715
$this->setUpTestRows();
1816
}
1917

@@ -38,7 +36,7 @@ protected function setUpTestRows()
3836
'status' => $i,
3937
];
4038
}
41-
$customerCollection = $this->getConnection()->getCollection('customer');
39+
$customerCollection = yii::$app->mongodb->getCollection('customer');
4240
$customers = $customerCollection->batchInsert($customers);
4341

4442
$customerOrders = [];
@@ -52,20 +50,19 @@ protected function setUpTestRows()
5250
'number' => $customer['status'] + 100,
5351
];
5452
}
55-
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
53+
$customerOrderCollection = yii::$app->mongodb->getCollection('customer_order');
5654
$customerOrderCollection->batchInsert($customerOrders);
5755
}
5856

5957
// Tests :
6058

6159
public function testQuery()
6260
{
63-
$db = $this->getConnection();
6461

6562
// initialize property test
6663
$query = new Query();
6764
$query->from('customer')->orderBy('id');
68-
$result = $query->batch(2, $db);
65+
$result = $query->batch(2);
6966
$this->assertTrue($result instanceof BatchQueryResult);
7067
$this->assertEquals(2, $result->batchSize);
7168
$this->assertTrue($result->query === $query);
@@ -74,7 +71,7 @@ public function testQuery()
7471
$query = new Query();
7572
$query->from('customer');
7673
$allRows = [];
77-
$batch = $query->batch(2, $db);
74+
$batch = $query->batch(2);
7875
foreach ($batch as $rows) {
7976
$allRows = array_merge($allRows, $rows);
8077
}
@@ -84,7 +81,7 @@ public function testQuery()
8481
$query = new Query();
8582
$query->from('customer')->orderBy('name');
8683
$allRows = [];
87-
$batch = $query->batch(2, $db);
84+
$batch = $query->batch(2);
8885
foreach ($batch as $rows) {
8986
$allRows = array_merge($allRows, $rows);
9087
}
@@ -106,7 +103,7 @@ public function testQuery()
106103
$query = new Query();
107104
$query->from('customer')->where(['name' => 'unexistingName']);
108105
$allRows = [];
109-
$batch = $query->batch(2, $db);
106+
$batch = $query->batch(2);
110107
foreach ($batch as $rows) {
111108
$allRows = array_merge($allRows, $rows);
112109
}
@@ -116,7 +113,7 @@ public function testQuery()
116113
$query = new Query();
117114
$query->from('customer')->indexBy('name');
118115
$allRows = [];
119-
foreach ($query->batch(2, $db) as $rows) {
116+
foreach ($query->batch(2) as $rows) {
120117
$allRows = array_merge($allRows, $rows);
121118
}
122119
$this->assertEquals(9, count($allRows));
@@ -128,7 +125,7 @@ public function testQuery()
128125
$query = new Query();
129126
$query->from('customer')->orderBy('name');
130127
$allRows = [];
131-
foreach ($query->each(100, $db) as $rows) {
128+
foreach ($query->each(100) as $rows) {
132129
$allRows[] = $rows;
133130
}
134131
$this->assertEquals(9, count($allRows));
@@ -140,7 +137,7 @@ public function testQuery()
140137
$query = new Query();
141138
$query->from('customer')->orderBy('name')->indexBy('name');
142139
$allRows = [];
143-
foreach ($query->each(100, $db) as $key => $row) {
140+
foreach ($query->each(100) as $key => $row) {
144141
$allRows[$key] = $row;
145142
}
146143
$this->assertEquals(9, count($allRows));
@@ -151,11 +148,10 @@ public function testQuery()
151148

152149
public function testActiveQuery()
153150
{
154-
$db = $this->getConnection();
155151

156152
$query = Customer::find()->orderBy('id');
157153
$customers = [];
158-
foreach ($query->batch(2, $db) as $models) {
154+
foreach ($query->batch(2) as $models) {
159155
$customers = array_merge($customers, $models);
160156
}
161157
$this->assertEquals(9, count($customers));
@@ -166,7 +162,7 @@ public function testActiveQuery()
166162
// batch with eager loading
167163
$query = Customer::find()->with('orders')->orderBy('id');
168164
$customers = [];
169-
foreach ($query->batch(2, $db) as $models) {
165+
foreach ($query->batch(2) as $models) {
170166
$customers = array_merge($customers, $models);
171167
foreach ($models as $model) {
172168
$this->assertTrue($model->isRelationPopulated('orders'));

‎tests/CacheTest.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ protected function createCache()
2626
{
2727
return Yii::createObject([
2828
'class' => Cache::className(),
29-
'db' => $this->getConnection(),
3029
'cacheCollection' => static::$cacheCollection,
3130
'gcProbability' => 0,
3231
]);

‎tests/CollectionTest.php‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ protected function tearDown()
1919
public function testGetName()
2020
{
2121
$collectionName = 'customer';
22-
$collection = $this->getConnection()->getCollection($collectionName);
22+
$collection = yii::$app->mongodb->getCollection($collectionName);
2323
$this->assertEquals($collectionName, $collection->name);
24-
$this->assertEquals($this->getConnection()->getDefaultDatabaseName() . '.' . $collectionName, $collection->getFullName());
24+
$this->assertEquals(yii::$app->mongodb->getDefaultDatabaseName() . '.' . $collectionName, $collection->getFullName());
2525
}
2626

2727
public function testFind()
2828
{
29-
$collection = $this->getConnection()->getCollection('customer');
29+
$collection = yii::$app->mongodb->getCollection('customer');
3030
$cursor = $collection->find();
3131
$this->assertTrue($cursor instanceof Cursor);
3232
}
3333

3434
public function testInsert()
3535
{
36-
$collection = $this->getConnection()->getCollection('customer');
36+
$collection = yii::$app->mongodb->getCollection('customer');
3737
$data = [
3838
'name' => 'customer 1',
3939
'address' => 'customer 1 address',
@@ -49,7 +49,7 @@ public function testInsert()
4949
*/
5050
public function testFindOne()
5151
{
52-
$collection = $this->getConnection()->getCollection('customer');
52+
$collection = yii::$app->mongodb->getCollection('customer');
5353
$data = [
5454
'name' => 'customer 1',
5555
'address' => 'customer 1 address',
@@ -69,7 +69,7 @@ public function testFindOne()
6969
*/
7070
public function testFindAll()
7171
{
72-
$collection = $this->getConnection()->getCollection('customer');
72+
$collection = yii::$app->mongodb->getCollection('customer');
7373
$data = [
7474
'name' => 'customer 1',
7575
'address' => 'customer 1 address',
@@ -90,7 +90,7 @@ public function testFindAll()
9090
*/
9191
public function testBatchInsert()
9292
{
93-
$collection = $this->getConnection()->getCollection('customer');
93+
$collection = yii::$app->mongodb->getCollection('customer');
9494
$rows = [
9595
[
9696
'name' => 'customer 1',
@@ -109,7 +109,7 @@ public function testBatchInsert()
109109

110110
public function testSave()
111111
{
112-
$collection = $this->getConnection()->getCollection('customer');
112+
$collection = yii::$app->mongodb->getCollection('customer');
113113
$data = [
114114
'name' => 'customer 1',
115115
'address' => 'customer 1 address',
@@ -124,7 +124,7 @@ public function testSave()
124124
*/
125125
public function testUpdateBySave()
126126
{
127-
$collection = $this->getConnection()->getCollection('customer');
127+
$collection = yii::$app->mongodb->getCollection('customer');
128128
$data = [
129129
'name' => 'customer 1',
130130
'address' => 'customer 1 address',
@@ -145,7 +145,7 @@ public function testUpdateBySave()
145145
*/
146146
public function testRemove()
147147
{
148-
$collection = $this->getConnection()->getCollection('customer');
148+
$collection = yii::$app->mongodb->getCollection('customer');
149149
$data = [
150150
'name' => 'customer 1',
151151
'address' => 'customer 1 address',
@@ -165,7 +165,7 @@ public function testRemove()
165165
*/
166166
public function testRemoveComplexCondition()
167167
{
168-
$collection = $this->getConnection()->getCollection('customer');
168+
$collection = yii::$app->mongodb->getCollection('customer');
169169
$collection->batchInsert([
170170
[
171171
'name' => 'customer 1',
@@ -193,7 +193,7 @@ public function testRemoveComplexCondition()
193193
*/
194194
public function testUpdate()
195195
{
196-
$collection = $this->getConnection()->getCollection('customer');
196+
$collection = yii::$app->mongodb->getCollection('customer');
197197
$data = [
198198
'name' => 'customer 1',
199199
'address' => 'customer 1 address',
@@ -215,7 +215,7 @@ public function testUpdate()
215215
*/
216216
public function testGroup()
217217
{
218-
$collection = $this->getConnection()->getCollection('customer');
218+
$collection = yii::$app->mongodb->getCollection('customer');
219219
$rows = [
220220
[
221221
'name' => 'customer 1',
@@ -239,7 +239,7 @@ public function testGroup()
239239

240240
public function testFindAndModify()
241241
{
242-
$collection = $this->getConnection()->getCollection('customer');
242+
$collection = yii::$app->mongodb->getCollection('customer');
243243
$rows = [
244244
[
245245
'name' => 'customer 1',
@@ -294,7 +294,7 @@ public function testFindAndModify()
294294
*/
295295
public function testMapReduce()
296296
{
297-
$collection = $this->getConnection()->getCollection('customer');
297+
$collection = yii::$app->mongodb->getCollection('customer');
298298
$rows = [
299299
[
300300
'name' => 'customer 1',
@@ -327,7 +327,7 @@ public function testMapReduce()
327327
);
328328
$this->assertEquals('mapReduceOut', $result);
329329

330-
$outputCollection = $this->getConnection()->getCollection($result);
330+
$outputCollection = yii::$app->mongodb->getCollection($result);
331331
$rows = $this->findAll($outputCollection);
332332
$expectedRows = [
333333
[
@@ -347,7 +347,7 @@ public function testMapReduce()
347347
*/
348348
public function testMapReduceInline()
349349
{
350-
$collection = $this->getConnection()->getCollection('customer');
350+
$collection = yii::$app->mongodb->getCollection('customer');
351351
$rows = [
352352
[
353353
'name' => 'customer 1',
@@ -393,7 +393,7 @@ public function testMapReduceInline()
393393

394394
public function testCreateIndex()
395395
{
396-
$collection = $this->getConnection()->getCollection('customer');
396+
$collection = yii::$app->mongodb->getCollection('customer');
397397
$columns = [
398398
'name',
399399
'status' => SORT_DESC
@@ -408,7 +408,7 @@ public function testCreateIndex()
408408
*/
409409
public function testDropIndex()
410410
{
411-
$collection = $this->getConnection()->getCollection('customer');
411+
$collection = yii::$app->mongodb->getCollection('customer');
412412

413413
$collection->createIndex('name');
414414
$this->assertTrue($collection->dropIndex('name'));
@@ -425,7 +425,7 @@ public function testDropIndex()
425425
*/
426426
public function testDropIndexWithPlugin()
427427
{
428-
$collection = $this->getConnection()->getCollection('customer');
428+
$collection = yii::$app->mongodb->getCollection('customer');
429429

430430
$columns = [
431431
'name' => 'text'
@@ -442,7 +442,7 @@ public function testDropIndexWithPlugin()
442442
*/
443443
public function testDropAllIndexes()
444444
{
445-
$collection = $this->getConnection()->getCollection('customer');
445+
$collection = yii::$app->mongodb->getCollection('customer');
446446
$collection->createIndex('name');
447447
$this->assertEquals(2, $collection->dropAllIndexes());
448448
$indexInfo = $collection->listIndexes();
@@ -451,7 +451,7 @@ public function testDropAllIndexes()
451451

452452
public function testCreateIndexes()
453453
{
454-
$collection = $this->getConnection()->getCollection('customer');
454+
$collection = yii::$app->mongodb->getCollection('customer');
455455
$columns = [
456456
['key' => ['name']],
457457
['key' => ['status' => SORT_DESC]]
@@ -466,7 +466,7 @@ public function testCreateIndexes()
466466
*/
467467
public function testDropIndexes()
468468
{
469-
$collection = $this->getConnection()->getCollection('customer');
469+
$collection = yii::$app->mongodb->getCollection('customer');
470470
$columns = [
471471
[
472472
'key' => ['name'],
@@ -491,7 +491,7 @@ public function testDropIndexes()
491491
*/
492492
public function testFindByNotObjectId()
493493
{
494-
$collection = $this->getConnection()->getCollection('customer');
494+
$collection = yii::$app->mongodb->getCollection('customer');
495495

496496
$data = [
497497
'name' => 'customer 1',
@@ -516,7 +516,7 @@ public function testFindByNotObjectId()
516516
*/
517517
public function testInsertMongoBin()
518518
{
519-
$collection = $this->getConnection()->getCollection('customer');
519+
$collection = yii::$app->mongodb->getCollection('customer');
520520

521521
$fileName = __FILE__;
522522
$data = [
@@ -534,7 +534,7 @@ public function testInsertMongoBin()
534534
*/
535535
public function testDistinct()
536536
{
537-
$collection = $this->getConnection()->getCollection('customer');
537+
$collection = yii::$app->mongodb->getCollection('customer');
538538

539539
$rows = [
540540
[

‎tests/CommandTest.php‎

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎tests/ConnectionTest.php‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,16 @@ public function testOpenClose()
4646

4747
public function testGetDatabase()
4848
{
49-
$connection = $this->getConnection();
5049

51-
$database = $connection->getDatabase($connection->defaultDatabaseName);
50+
$database = yii::$app->mongodb->getDatabase(yii::$app->mongodb->defaultDatabaseName);
5251
$this->assertTrue($database instanceof Database);
53-
$this->assertSame($connection, $database->connection);
54-
$this->assertSame($connection->defaultDatabaseName, $database->name);
52+
$this->assertSame(yii::$app->mongodb, $database->connection);
53+
$this->assertSame(yii::$app->mongodb->defaultDatabaseName, $database->name);
5554

56-
$database2 = $connection->getDatabase($connection->defaultDatabaseName);
55+
$database2 = yii::$app->mongodb->getDatabase(yii::$app->mongodb->defaultDatabaseName);
5756
$this->assertTrue($database === $database2);
5857

59-
$databaseRefreshed = $connection->getDatabase($connection->defaultDatabaseName, true);
58+
$databaseRefreshed = yii::$app->mongodb->getDatabase(yii::$app->mongodb->defaultDatabaseName, true);
6059
$this->assertFalse($database === $databaseRefreshed);
6160
}
6261

@@ -110,15 +109,14 @@ public function testGetDefaultDatabase()
110109
*/
111110
public function testGetCollection()
112111
{
113-
$connection = $this->getConnection();
114112

115-
$collection = $connection->getCollection('customer');
113+
$collection = yii::$app->mongodb->getCollection('customer');
116114
$this->assertTrue($collection instanceof Collection);
117115

118-
$collection2 = $connection->getCollection('customer');
116+
$collection2 = yii::$app->mongodb->getCollection('customer');
119117
$this->assertTrue($collection === $collection2);
120118

121-
$collection2 = $connection->getCollection('customer', true);
119+
$collection2 = yii::$app->mongodb->getCollection('customer', true);
122120
$this->assertFalse($collection === $collection2);
123121
}
124122

@@ -127,31 +125,28 @@ public function testGetCollection()
127125
*/
128126
public function testGetFileCollection()
129127
{
130-
$connection = $this->getConnection();
131128

132-
$collection = $connection->getFileCollection('testfs');
129+
$collection = yii::$app->mongodb->getFileCollection('testfs');
133130
$this->assertTrue($collection instanceof FileCollection);
134131

135-
$collection2 = $connection->getFileCollection('testfs');
132+
$collection2 = yii::$app->mongodb->getFileCollection('testfs');
136133
$this->assertTrue($collection === $collection2);
137134

138-
$collection2 = $connection->getFileCollection('testfs', true);
135+
$collection2 = yii::$app->mongodb->getFileCollection('testfs', true);
139136
$this->assertFalse($collection === $collection2);
140137
}
141138

142139
public function testGetQueryBuilder()
143140
{
144-
$connection = $this->getConnection();
145141

146-
$this->assertTrue($connection->getQueryBuilder() instanceof QueryBuilder);
142+
$this->assertTrue(yii::$app->mongodb->getQueryBuilder() instanceof QueryBuilder);
147143
}
148144

149145
public function testCreateCommand()
150146
{
151-
$connection = $this->getConnection();
152147

153-
$command = $connection->createCommand();
148+
$command = yii::$app->mongodb->createCommand();
154149
$this->assertTrue($command instanceof Command);
155-
$this->assertSame($connection, $command->db);
150+
$this->assertSame(yii::$app->mongodb, $command->db);
156151
}
157152
}

‎tests/DatabaseTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function tearDown()
1919

2020
public function testGetCollection()
2121
{
22-
$database = $this->getConnection()->getDatabase();
22+
$database = yii::$app->mongodb->getDatabase();
2323

2424
$collection = $database->getCollection('customer');
2525
$this->assertTrue($collection instanceof Collection);
@@ -34,7 +34,7 @@ public function testGetCollection()
3434

3535
public function testGetFileCollection()
3636
{
37-
$database = $this->getConnection()->getDatabase();
37+
$database = yii::$app->mongodb->getDatabase();
3838

3939
$collection = $database->getFileCollection('testfs');
4040
$this->assertTrue($collection instanceof FileCollection);
@@ -49,7 +49,7 @@ public function testGetFileCollection()
4949

5050
public function testCreateCommand()
5151
{
52-
$database = $this->getConnection()->getDatabase();
52+
$database = yii::$app->mongodb->getDatabase();
5353

5454
$command = $database->createCommand();
5555
$this->assertTrue($command instanceof Command);
@@ -58,7 +58,7 @@ public function testCreateCommand()
5858

5959
public function testCreateCollection()
6060
{
61-
$database = $this->getConnection()->getDatabase();
61+
$database = yii::$app->mongodb->getDatabase();
6262
$this->assertTrue($database->createCollection('customer'));
6363
}
6464
}

‎tests/LogBuilderTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function dataProviderEncodeData()
3737
*/
3838
public function testEncodeData($data, $expectedResult)
3939
{
40-
$logBuilder = $this->getConnection()->getLogBuilder();
40+
$logBuilder = yii::$app->mongodb->getLogBuilder();
4141
$this->assertTrue(strcasecmp($expectedResult, $logBuilder->encodeData($data)) === 0);
4242
}
4343
}

‎tests/MigrationTest.php‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@ protected function tearDown()
1212
parent::tearDown();
1313
}
1414

15-
/**
16-
* @return Migration migration instance.
17-
*/
18-
protected function createMigration()
19-
{
20-
return new Migration(['db' => $this->getConnection()]);
21-
}
22-
2315
// Tests :
2416

2517
public function testCollectionOperations()
2618
{
27-
$migration = $this->createMigration();
19+
$migration = new Migration;
2820

2921
$migration->createCollection('customer');
3022
$this->assertNotEmpty($migration->db->getDatabase()->listCollections(['name' => 'customer']));
@@ -35,7 +27,7 @@ public function testCollectionOperations()
3527

3628
public function testIndexOperations()
3729
{
38-
$migration = $this->createMigration();
30+
$migration = new Migration;
3931

4032
$migration->createIndexes('customer', [
4133
['key' => 'name']
@@ -58,7 +50,7 @@ public function testIndexOperations()
5850

5951
public function testDataOperations()
6052
{
61-
$migration = $this->createMigration();
53+
$migration = new Migration;
6254

6355
$id = $migration->insert('customer', ['name' => 'John Doe']);
6456
$this->assertTrue($id instanceof ObjectID);
@@ -85,7 +77,7 @@ public function testDataOperations()
8577
*/
8678
public function testCommandOutput()
8779
{
88-
$migration = $this->createMigration();
80+
$migration = new Migration;
8981

9082
$migration->compact = false;
9183
$migration->createCollection('customer');

‎tests/QueryRunTest.php‎

Lines changed: 67 additions & 95 deletions
Large diffs are not rendered by default.

‎tests/SessionTest.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ protected function createSession()
2626
{
2727
return Yii::createObject([
2828
'class' => Session::className(),
29-
'db' => $this->getConnection(),
3029
'sessionCollection' => static::$sessionCollection,
3130
]);
3231
}

‎tests/TestCase.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function setUp()
3434
if (!empty($config)) {
3535
$this->mongoDbConfig = $config;
3636
}
37+
Yii::$app->setComponents(['mongodb' => $this->getConnection()]);
3738
//$this->mockApplication();
3839
}
3940

‎tests/console/controllers/MigrateControllerTest.php‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ public function setUp()
4949
$this->setUpMigrationPath();
5050

5151
$this->mockApplication();
52-
Yii::$app->setComponents(['mongodb' => $this->getConnection()]);
5352
}
5453

5554
public function tearDown()
5655
{
5756
parent::tearDown();
5857
if (extension_loaded('mongodb')) {
5958
try {
60-
$this->getConnection()->getCollection('migration')->drop();
59+
yii::$app->mongodb->getCollection('migration')->drop();
6160
} catch (Exception $e) {
6261
// shutdown exception
6362
}
@@ -501,17 +500,16 @@ public function testNamespaceTo()
501500
*/
502501
public function testGetMigrationHistory()
503502
{
504-
$connection = $this->getConnection();
505503

506504
$controllerConfig = [
507505
'migrationPath' => null,
508506
'migrationNamespaces' => [$this->migrationNamespace]
509507
];
510508

511509
$controller = $this->createMigrateController($controllerConfig);
512-
$controller->db = $this->getConnection();
510+
$controller->db = yii::$app->mongodb;
513511

514-
$connection->createCommand()->batchInsert('migration', [
512+
yii::$app->mongodb->createCommand()->batchInsert('migration', [
515513
[
516514
'version' => 'app\migrations\M140506102106One',
517515
'apply_time' => 10,
@@ -574,9 +572,7 @@ public function testRefreshMigration()
574572
$this->markTestSkipped('Method "yii\console\controllers\BaseMigrateController::actionFresh()" does not exist in this Yii framework version.');
575573
}
576574

577-
$connection = $this->getConnection();
578-
579-
$collection = $connection->getCollection('hall_of_fame');
575+
$collection = yii::$app->mongodb->getCollection('hall_of_fame');
580576
$collection->insert(['name' => 'Qiang Xue']);
581577
$collection->insert(['name' => 'Alexander Makarov']);
582578

@@ -585,6 +581,6 @@ public function testRefreshMigration()
585581
$this->assertContains('Collection hall_of_fame dropped.', $result);
586582
$this->assertContains('No new migrations found. Your system is up-to-date.', $result);
587583

588-
$this->assertEmpty($connection->getDatabase()->listCollections(['name' => $collection->name]));
584+
$this->assertEmpty(yii::$app->mongodb->getDatabase()->listCollections(['name' => $collection->name]));
589585
}
590586
}

‎tests/data/ar/ActiveRecord.php‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎tests/data/ar/Animal.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace yiiunit\extensions\mongodb\data\ar;
44

5+
use yii\mongodb\ActiveRecord;
6+
57
/**
68
* Animal
79
*

‎tests/data/ar/Customer.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace yiiunit\extensions\mongodb\data\ar;
44

55
use yiiunit\extensions\mongodb\data\ar\file\CustomerFile;
6+
use yii\mongodb\ActiveRecord;
67

78
/**
89
* @property \MongoDB\BSON\ObjectID|string $_id

‎tests/data/ar/CustomerOrder.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace yiiunit\extensions\mongodb\data\ar;
44

5+
use yii\mongodb\ActiveRecord;
6+
57
/**
68
* @property \MongoDB\BSON\ObjectID|string $_id
79
* @property int $number

‎tests/data/ar/Dog.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace yiiunit\extensions\mongodb\data\ar;
44

5+
use yii\mongodb\ActiveRecord;
6+
57
/**
68
* Dog
79
*

‎tests/data/ar/Item.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace yiiunit\extensions\mongodb\data\ar;
44

5+
use \yii\mongodb\ActiveRecord;
6+
57
/**
68
* @property \MongoDB\BSON\ObjectID|string $_id
79
* @property string $name

‎tests/data/ar/file/ActiveRecord.php‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎tests/data/ar/file/CustomerFile.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2-
32
namespace yiiunit\extensions\mongodb\data\ar\file;
43

4+
use yii\mongodb\file\ActiveRecord;
5+
56
class CustomerFile extends ActiveRecord
67
{
78
/**

‎tests/file/ActiveRecordTest.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class ActiveRecordTest extends TestCase
2323
protected function setUp()
2424
{
2525
parent::setUp();
26-
ActiveRecord::$db = $this->getConnection();
2726
$this->dropFileCollection(CustomerFile::collectionName());
2827
$this->setUpTestRows();
2928
$filePath = $this->getTestFilePath();
@@ -55,7 +54,7 @@ protected function getTestFilePath()
5554
*/
5655
protected function setUpTestRows()
5756
{
58-
$collection = $this->getConnection()->getFileCollection(CustomerFile::collectionName());
57+
$collection = yii::$app->mongodb->getFileCollection(CustomerFile::collectionName());
5958
$rows = [];
6059
for ($i = 1; $i <= 10; $i++) {
6160
$record = [

‎tests/file/ActiveRelationTest.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class ActiveRelationTest extends TestCase
1414
protected function setUp()
1515
{
1616
parent::setUp();
17-
\yiiunit\extensions\mongodb\data\ar\ActiveRecord::$db = $this->getConnection();
18-
\yiiunit\extensions\mongodb\data\ar\file\ActiveRecord::$db = $this->getConnection();
1917
$this->setUpTestRows();
2018
}
2119

@@ -31,7 +29,7 @@ protected function tearDown()
3129
*/
3230
protected function setUpTestRows()
3331
{
34-
$fileCollection = $this->getConnection()->getFileCollection(CustomerFile::collectionName());
32+
$fileCollection = yii::$app->mongodb->getFileCollection(CustomerFile::collectionName());
3533
$customers = [];
3634
$files = [];
3735
for ($i = 1; $i <= 5; $i++) {
@@ -52,7 +50,7 @@ protected function setUpTestRows()
5250
'file_id' => $file['_id'],
5351
];
5452
}
55-
$customerCollection = $this->getConnection()->getCollection(Customer::collectionName());
53+
$customerCollection = yii::$app->mongodb->getCollection(Customer::collectionName());
5654
$customers = $customerCollection->batchInsert($customers);
5755
}
5856

‎tests/file/CollectionTest.php‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ protected function tearDown()
2222

2323
public function testGetChunkCollection()
2424
{
25-
$collection = $this->getConnection()->getFileCollection();
25+
$collection = yii::$app->mongodb->getFileCollection();
2626
$chunkCollection = $collection->getChunkCollection();
2727
$this->assertTrue($chunkCollection instanceof \yii\mongodb\Collection);
2828
$this->assertTrue($chunkCollection->database instanceof \yii\mongodb\Database);
2929
}
3030

3131
public function testGetFileCollection()
3232
{
33-
$collection = $this->getConnection()->getFileCollection();
33+
$collection = yii::$app->mongodb->getFileCollection();
3434
$fileCollection = $collection->getFileCollection();
3535
$this->assertTrue($fileCollection instanceof \yii\mongodb\Collection);
3636
$this->assertTrue($fileCollection->database instanceof \yii\mongodb\Database);
3737
}
3838

3939
public function testEnsureIndexes()
4040
{
41-
$collection = $this->getConnection()->getFileCollection();
41+
$collection = yii::$app->mongodb->getFileCollection();
4242

4343
$collection->ensureIndexes();
4444
$this->assertCount(2, $collection->listIndexes());
@@ -54,14 +54,14 @@ public function testEnsureIndexes()
5454

5555
public function testFind()
5656
{
57-
$collection = $this->getConnection()->getFileCollection();
57+
$collection = yii::$app->mongodb->getFileCollection();
5858
$cursor = $collection->find();
5959
$this->assertTrue($cursor instanceof Cursor);
6060
}
6161

6262
public function testInsertFile()
6363
{
64-
$collection = $this->getConnection()->getFileCollection();
64+
$collection = yii::$app->mongodb->getFileCollection();
6565

6666
$filename = __FILE__;
6767
$id = $collection->insertFile($filename);
@@ -77,7 +77,7 @@ public function testInsertFile()
7777

7878
public function testInsertFileContent()
7979
{
80-
$collection = $this->getConnection()->getFileCollection();
80+
$collection = yii::$app->mongodb->getFileCollection();
8181

8282
$bytes = 'Test file content';
8383
$id = $collection->insertFileContent($bytes);
@@ -97,7 +97,7 @@ public function testInsertFileContent()
9797
*/
9898
public function testGet()
9999
{
100-
$collection = $this->getConnection()->getFileCollection();
100+
$collection = yii::$app->mongodb->getFileCollection();
101101

102102
$bytes = 'Test file content';
103103
$id = $collection->insertFileContent($bytes);
@@ -112,7 +112,7 @@ public function testGet()
112112
*/
113113
public function testDeleteFile()
114114
{
115-
$collection = $this->getConnection()->getFileCollection();
115+
$collection = yii::$app->mongodb->getFileCollection();
116116

117117
$bytes = 'Test file content';
118118
$id = $collection->insertFileContent($bytes);
@@ -128,7 +128,7 @@ public function testDeleteFile()
128128
*/
129129
public function testRemove()
130130
{
131-
$collection = $this->getConnection()->getFileCollection();
131+
$collection = yii::$app->mongodb->getFileCollection();
132132

133133
for ($i = 1; $i <=10; $i++) {
134134
$bytes = 'Test file content ' . $i;

‎tests/file/DownloadTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function tearDown()
1919

2020
public function testToStream()
2121
{
22-
$collection = $this->getConnection()->getFileCollection();
22+
$collection = yii::$app->mongodb->getFileCollection();
2323

2424
$upload = $collection->createUpload();
2525
$document = $upload->addContent('test content')->complete();
@@ -34,7 +34,7 @@ public function testToStream()
3434

3535
public function testToFile()
3636
{
37-
$collection = $this->getConnection()->getFileCollection();
37+
$collection = yii::$app->mongodb->getFileCollection();
3838

3939
$upload = $collection->createUpload();
4040
$document = $upload->addContent('test content')->complete();
@@ -49,7 +49,7 @@ public function testToFile()
4949

5050
public function testSubstr()
5151
{
52-
$collection = $this->getConnection()->getFileCollection();
52+
$collection = yii::$app->mongodb->getFileCollection();
5353

5454
$upload = $collection->createUpload();
5555
$upload->chunkSize = 10;

‎tests/file/QueryTest.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function tearDown()
2828
*/
2929
protected function setUpTestRows()
3030
{
31-
$collection = $this->getConnection()->getFileCollection();
31+
$collection = yii::$app->mongodb->getFileCollection();
3232
for ($i = 1; $i <= 10; $i++) {
3333
$collection->insertFileContent('content' . $i, [
3434
'filename' => 'name' . $i,
@@ -41,28 +41,25 @@ protected function setUpTestRows()
4141

4242
public function testAll()
4343
{
44-
$connection = $this->getConnection();
4544
$query = new Query();
46-
$rows = $query->from('fs')->all($connection);
45+
$rows = $query->from('fs')->all();
4746
$this->assertEquals(10, count($rows));
4847
}
4948

5049
public function testOne()
5150
{
52-
$connection = $this->getConnection();
5351
$query = new Query();
54-
$row = $query->from('fs')->one($connection);
52+
$row = $query->from('fs')->one();
5553
$this->assertTrue(is_array($row));
5654
$this->assertTrue($row['file'] instanceof Download);
5755
}
5856

5957
public function testDirectMatch()
6058
{
61-
$connection = $this->getConnection();
6259
$query = new Query();
6360
$rows = $query->from('fs')
6461
->where(['file_index' => 5])
65-
->all($connection);
62+
->all();
6663
$this->assertEquals(1, count($rows));
6764

6865
$file = $rows[0];

‎tests/file/StreamWrapperTest.php‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ class StreamWrapperTest extends TestCase
88
{
99
protected function tearDown()
1010
{
11-
$connection = $this->getConnection();
12-
if (in_array($connection->fileStreamProtocol, stream_get_wrappers())) {
13-
stream_wrapper_unregister($connection->fileStreamProtocol);
11+
if (in_array(yii::$app->mongodb->fileStreamProtocol, stream_get_wrappers())) {
12+
stream_wrapper_unregister(yii::$app->mongodb->fileStreamProtocol);
1413
}
1514

1615
$this->dropFileCollection('fs');
@@ -22,7 +21,7 @@ protected function tearDown()
2221

2322
public function testCreateFromDownload()
2423
{
25-
$collection = $this->getConnection()->getFileCollection();
24+
$collection = yii::$app->mongodb->getFileCollection();
2625

2726
$upload = $collection->createUpload();
2827
$document = $upload->addContent('test content')->complete();
@@ -36,23 +35,22 @@ public function testCreateFromDownload()
3635

3736
public function testWriteResource()
3837
{
39-
$connection = $this->getConnection();
4038
$this->mockApplication([
4139
'components' => [
42-
'mongodb' => $connection
40+
'mongodb' => yii::$app->mongodb
4341
],
4442
]);
4543

46-
$connection->registerFileStreamWrapper(true);
47-
$databaseName = $connection->getDefaultDatabaseName();
44+
yii::$app->mongodb->registerFileStreamWrapper(true);
45+
$databaseName = yii::$app->mongodb->getDefaultDatabaseName();
4846

4947
$url = "gridfs://{$databaseName}.fs?filename=test.txt";
5048
$resource = fopen($url, 'w');
5149
fwrite($resource, 'begin ');
5250
fwrite($resource, 'end');
5351
fclose($resource);
5452

55-
$collection = $connection->getFileCollection();
53+
$collection = yii::$app->mongodb->getFileCollection();
5654
$document = $collection->findOne(['filename' => 'test.txt']);
5755
$this->assertNotEmpty($document);
5856

@@ -61,19 +59,18 @@ public function testWriteResource()
6159

6260
public function testReadResource()
6361
{
64-
$connection = $this->getConnection();
6562
$this->mockApplication([
6663
'components' => [
67-
'mongodb' => $connection
64+
'mongodb' => yii::$app->mongodb
6865
],
6966
]);
7067

71-
$collection = $connection->getFileCollection();
68+
$collection = yii::$app->mongodb->getFileCollection();
7269
$upload = $collection->createUpload();
7370
$document = $upload->addContent('test content')->complete();
7471

75-
$connection->registerFileStreamWrapper(true);
76-
$databaseName = $connection->getDefaultDatabaseName();
72+
yii::$app->mongodb->registerFileStreamWrapper(true);
73+
$databaseName = yii::$app->mongodb->getDefaultDatabaseName();
7774

7875
$url = "gridfs://{$databaseName}.fs?_id=" . $document['_id'];
7976
$resource = fopen($url, 'r');
@@ -83,15 +80,14 @@ public function testReadResource()
8380

8481
public function testSeek()
8582
{
86-
$connection = $this->getConnection();
8783
$this->mockApplication([
8884
'components' => [
89-
'mongodb' => $connection
85+
'mongodb' => yii::$app->mongodb
9086
],
9187
]);
9288

93-
$connection->registerFileStreamWrapper(true);
94-
$databaseName = $connection->getDefaultDatabaseName();
89+
yii::$app->mongodb->registerFileStreamWrapper(true);
90+
$databaseName = yii::$app->mongodb->getDefaultDatabaseName();
9591

9692
$url = "gridfs://{$databaseName}.fs?filename=test.txt";
9793
$resource = fopen($url, 'w');

‎tests/file/UploadTest.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function tearDown()
2020

2121
public function testAddContent()
2222
{
23-
$collection = $this->getConnection()->getFileCollection();
23+
$collection = yii::$app->mongodb->getFileCollection();
2424

2525
$upload = $collection->createUpload();
2626
$document = $upload->addContent('content line 1')
@@ -37,7 +37,7 @@ public function testAddContent()
3737
*/
3838
public function testAddContentChunk()
3939
{
40-
$collection = $this->getConnection()->getFileCollection();
40+
$collection = yii::$app->mongodb->getFileCollection();
4141

4242
$upload = $collection->createUpload();
4343
$upload->chunkSize = 10;
@@ -50,7 +50,7 @@ public function testAddContentChunk()
5050

5151
public function testAddStream()
5252
{
53-
$collection = $this->getConnection()->getFileCollection();
53+
$collection = yii::$app->mongodb->getFileCollection();
5454

5555
$upload = $collection->createUpload();
5656

@@ -68,7 +68,7 @@ public function testAddStream()
6868
*/
6969
public function testCancel()
7070
{
71-
$collection = $this->getConnection()->getFileCollection();
71+
$collection = yii::$app->mongodb->getFileCollection();
7272

7373
$upload = $collection->createUpload();
7474
$document = $upload->addContent('content line 1');
@@ -85,7 +85,7 @@ public function testCancel()
8585
*/
8686
public function testCustomId()
8787
{
88-
$collection = $this->getConnection()->getFileCollection();
88+
$collection = yii::$app->mongodb->getFileCollection();
8989

9090
$id = new ObjectID();
9191
$upload = $collection->createUpload([

‎tests/i18n/MongoDbMessageSourceTest.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ protected function tearDown()
3333
*/
3434
protected function setupTestRows()
3535
{
36-
$db = $this->getConnection();
37-
$collection = $db->getCollection('message');
36+
$collection = yii::$app->mongodb->getCollection('message');
3837
$collection->batchInsert([
3938
[
4039
'language' => 'de',
@@ -104,7 +103,6 @@ protected function setupI18N()
104103
$this->i18n = new I18N([
105104
'translations' => [
106105
'*' => new MongoDbMessageSource([
107-
'db' => $this->getConnection(),
108106
'sourceLanguage' => 'en-US',
109107
])
110108
]

‎tests/log/MongoDbTargetTest.php‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,11 @@ protected function tearDown()
1414
parent::tearDown();
1515
}
1616

17-
/**
18-
* @return MongoDbTarget test log target
19-
*/
20-
protected function createLogTarget()
21-
{
22-
return new MongoDbTarget([
23-
'db' => $this->getConnection(),
24-
]);
25-
}
26-
2717
// Tests :
2818

2919
public function testExport()
3020
{
31-
$target = $this->createLogTarget();
21+
$target = new MongoDbTarget();
3222

3323
$target->messages = [
3424
[

‎tests/rbac/MongoDbManagerTest.php‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MongoDbManagerTest extends TestCase
2323
protected function setUp()
2424
{
2525
parent::setUp();
26-
$this->auth = $this->createManager();
26+
$this->auth = new MongoDbManager();
2727
}
2828

2929
protected function tearDown()
@@ -34,14 +34,6 @@ protected function tearDown()
3434
parent::tearDown();
3535
}
3636

37-
/**
38-
* @return MongoDbManager
39-
*/
40-
protected function createManager()
41-
{
42-
return new MongoDbManager(['db' => $this->getConnection()]);
43-
}
44-
4537
// Tests :
4638

4739
public function testCreateRole()
@@ -354,7 +346,7 @@ public function testAssignMultipleRoles()
354346
$this->auth->assign($reader, 'readingAuthor');
355347
$this->auth->assign($author, 'readingAuthor');
356348

357-
$this->auth = $this->createManager();
349+
$this->auth = new MongoDbManager();
358350

359351
$roles = $this->auth->getRolesByUser('readingAuthor');
360352
$roleNames = [];
@@ -376,7 +368,7 @@ public function testAssignmentsToIntegerId()
376368
$this->auth->assign($author, 1337);
377369
$this->auth->assign($reader, 1337);
378370

379-
$this->auth = $this->createManager();
371+
$this->auth = new MongoDbManager();
380372

381373
$this->assertEquals(0, count($this->auth->getAssignments(0)));
382374
$this->assertEquals(1, count($this->auth->getAssignments(42)));
@@ -389,7 +381,7 @@ public function testGetAssignmentsByRole()
389381
$reader = $this->auth->getRole('reader');
390382
$this->auth->assign($reader, 123);
391383

392-
$this->auth = $this->createManager();
384+
$this->auth = new MongoDbManager();
393385

394386
$this->assertEquals([], $this->auth->getUserIdsByRole('nonexisting'));
395387
$this->assertEquals(['reader A', '123'], $this->auth->getUserIdsByRole('reader'), '', 0.0, 10, true);

0 commit comments

Comments
 (0)
Please sign in to comment.