Skip to content

Commit 756825e

Browse files
committed
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

0 commit comments

Comments
 (0)