Skip to content

Commit 61e732c

Browse files
committed
Merge branch 'test-case-db-problem' into match-with-enableLogging-and-Profiling
2 parents dd7c555 + 9a3ea1f commit 61e732c

36 files changed

+272
-417
lines changed

tests/ActiveDataProviderTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
namespace yiiunit\extensions\mongodb;
44

55
use MongoDB\BSON\ObjectID;
6+
use yii;
67
use yii\data\ActiveDataProvider;
78
use yii\mongodb\Query;
8-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
99
use yiiunit\extensions\mongodb\data\ar\Customer;
1010

1111
class ActiveDataProviderTest extends TestCase
1212
{
1313
protected function setUp()
1414
{
1515
parent::setUp();
16-
$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: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010

1111
class ActiveFixtureTest extends TestCase
1212
{
13-
protected function setUp()
14-
{
15-
parent::setUp();
16-
$this->mockApplication();
17-
}
1813

1914
protected function tearDown()
2015
{
@@ -32,7 +27,7 @@ public function testLoadCollection()
3227
$fixture = $this->getMockBuilder(ActiveFixture::className())
3328
->setConstructorArgs([
3429
[
35-
'db' => $this->getConnection(),
30+
'db' => Yii::$app->mongodb,
3631
'collectionName' => Customer::collectionName()
3732
]
3833
])
@@ -45,7 +40,7 @@ public function testLoadCollection()
4540

4641
$fixture->load();
4742

48-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
43+
$rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName()));
4944
$this->assertCount(2, $rows);
5045
}
5146

@@ -55,7 +50,7 @@ public function testLoadClass()
5550
$fixture = $this->getMockBuilder(ActiveFixture::className())
5651
->setConstructorArgs([
5752
[
58-
'db' => $this->getConnection(),
53+
'db' => yii::$app->mongodb,
5954
'collectionName' => Customer::collectionName()
6055
]
6156
])
@@ -68,7 +63,7 @@ public function testLoadClass()
6863

6964
$fixture->load();
7065

71-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
66+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
7267
$this->assertCount(2, $rows);
7368
}
7469

@@ -83,7 +78,7 @@ public function testLoadEmptyData()
8378
$fixture = $this->getMockBuilder(ActiveFixture::className())
8479
->setConstructorArgs([
8580
[
86-
'db' => $this->getConnection(),
81+
'db' => Yii::$app->mongodb,
8782
'collectionName' => Customer::collectionName()
8883
]
8984
])
@@ -95,7 +90,7 @@ public function testLoadEmptyData()
9590

9691
$fixture->load(); // should be no error
9792

98-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
93+
$rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName()));
9994
$this->assertEmpty($rows);
10095
}
10196

@@ -106,7 +101,6 @@ public function testLoadEmptyData()
106101
*/
107102
public function testDefaultDataFile()
108103
{
109-
$db = $this->getConnection();
110104

111105
$fixturePath = Yii::getAlias('@runtime/fixtures');
112106
$fixtureDataPath = $fixturePath . DIRECTORY_SEPARATOR . 'data';
@@ -138,29 +132,27 @@ class {$className} extends \yii\mongodb\ActiveFixture
138132
['name' => 'name2'],
139133
['name' => 'name3'],
140134
];
141-
$fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . $db->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php';
135+
$fixtureDataFile = $fixtureDataPath . DIRECTORY_SEPARATOR . Yii::$app->mongodb->getDefaultDatabaseName() . '.' . Customer::collectionName() . '.php';
142136
$fixtureDataContent = '<?php return ' . VarDumper::export($fixtureData) . ';';
143137
file_put_contents($fixtureDataFile, $fixtureDataContent);
144138

145139
/* @var $fixture ActiveFixture */
146140

147141
$fixture = new $className([
148-
'db' => $db,
149142
'collectionName' => Customer::collectionName(),
150143
]);
151144
$fixture->load();
152-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
145+
$rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName()));
153146
$this->assertCount(2, $rows);
154147

155148
$fixture = new $className([
156-
'db' => $db,
157149
'collectionName' => [
158-
$db->getDefaultDatabaseName(),
150+
Yii::$app->mongodb->getDefaultDatabaseName(),
159151
Customer::collectionName()
160152
],
161153
]);
162154
$fixture->load();
163-
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
155+
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
164156
$this->assertCount(3, $rows);
165157
}
166-
}
158+
}

tests/ActiveRecordTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use MongoDB\BSON\Binary;
66
use MongoDB\BSON\ObjectID;
77
use MongoDB\BSON\Regex;
8+
use yii;
89
use yii\mongodb\ActiveQuery;
9-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
1010
use yiiunit\extensions\mongodb\data\ar\Customer;
1111
use yiiunit\extensions\mongodb\data\ar\Animal;
1212
use yiiunit\extensions\mongodb\data\ar\Dog;
@@ -22,7 +22,6 @@ class ActiveRecordTest extends TestCase
2222
protected function setUp()
2323
{
2424
parent::setUp();
25-
ActiveRecord::$db = $this->getConnection();
2625
$this->setUpTestRows();
2726
}
2827

@@ -37,7 +36,7 @@ protected function tearDown()
3736
*/
3837
protected function setUpTestRows()
3938
{
40-
$collection = $this->getConnection()->getCollection('customer');
39+
$collection = yii::$app->mongodb->getCollection('customer');
4140
$rows = [];
4241
for ($i = 1; $i <= 10; $i++) {
4342
$rows[] = [
@@ -282,66 +281,64 @@ public function testExists()
282281

283282
public function testScalar()
284283
{
285-
$connection = $this->getConnection();
286284

287285
$result = Customer::find()
288286
->select(['name' => true, '_id' => false])
289287
->orderBy(['name' => SORT_ASC])
290288
->limit(1)
291-
->scalar($connection);
289+
->scalar();
292290
$this->assertSame('name1', $result);
293291

294292
$result = Customer::find()
295293
->select(['name' => true, '_id' => false])
296294
->andWhere(['status' => -1])
297-
->scalar($connection);
295+
->scalar();
298296
$this->assertSame(false, $result);
299297

300298
$result = Customer::find()
301299
->select(['name'])
302300
->orderBy(['name' => SORT_ASC])
303301
->limit(1)
304-
->scalar($connection);
302+
->scalar();
305303
$this->assertSame('name1', $result);
306304

307305
$result = Customer::find()
308306
->select(['_id'])
309307
->limit(1)
310-
->scalar($connection);
308+
->scalar();
311309
$this->assertTrue($result instanceof ObjectID);
312310
}
313311

314312
public function testColumn()
315313
{
316-
$connection = $this->getConnection();
317314

318315
$result = Customer::find()
319316
->select(['name' => true, '_id' => false])
320317
->orderBy(['name' => SORT_ASC])
321318
->limit(2)
322-
->column($connection);
319+
->column();
323320
$this->assertEquals(['name1', 'name10'], $result);
324321

325322
$result = Customer::find()
326323
->select(['name' => true, '_id' => false])
327324
->andWhere(['status' => -1])
328325
->orderBy(['name' => SORT_ASC])
329326
->limit(2)
330-
->column($connection);
327+
->column();
331328
$this->assertEquals([], $result);
332329

333330
$result = Customer::find()
334331
->select(['name'])
335332
->orderBy(['name' => SORT_ASC])
336333
->limit(2)
337-
->column($connection);
334+
->column();
338335
$this->assertEquals(['name1', 'name10'], $result);
339336

340337
$result = Customer::find()
341338
->select(['_id'])
342339
->orderBy(['name' => SORT_ASC])
343340
->limit(2)
344-
->column($connection);
341+
->column();
345342
$this->assertTrue($result[0] instanceof ObjectID);
346343
$this->assertTrue($result[1] instanceof ObjectID);
347344
}

tests/ActiveRelationTest.php

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

33
namespace yiiunit\extensions\mongodb;
44

5-
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
5+
use yii;
66
use yiiunit\extensions\mongodb\data\ar\Customer;
77
use yiiunit\extensions\mongodb\data\ar\CustomerOrder;
88
use yiiunit\extensions\mongodb\data\ar\Item;
@@ -12,7 +12,6 @@ class ActiveRelationTest extends TestCase
1212
protected function setUp()
1313
{
1414
parent::setUp();
15-
ActiveRecord::$db = $this->getConnection();
1615
$this->setUpTestRows();
1716
}
1817

@@ -38,7 +37,7 @@ protected function setUpTestRows()
3837
'status' => $i,
3938
];
4039
}
41-
$customerCollection = $this->getConnection()->getCollection('customer');
40+
$customerCollection = yii::$app->mongodb->getCollection('customer');
4241
$customers = $customerCollection->batchInsert($customers);
4342

4443
$items = [];
@@ -48,7 +47,7 @@ protected function setUpTestRows()
4847
'price' => $i,
4948
];
5049
}
51-
$itemCollection = $this->getConnection()->getCollection('item');
50+
$itemCollection = yii::$app->mongodb->getCollection('item');
5251
$items = $itemCollection->batchInsert($items);
5352

5453
$customerOrders = [];
@@ -70,7 +69,7 @@ protected function setUpTestRows()
7069
],
7170
];
7271
}
73-
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
72+
$customerOrderCollection = yii::$app->mongodb->getCollection('customer_order');
7473
$customerOrderCollection->batchInsert($customerOrders);
7574
}
7675

0 commit comments

Comments
 (0)