Skip to content

Commit

Permalink
Merge branch 'test-case-db-problem' into match-with-enableLogging-and…
Browse files Browse the repository at this point in the history
…-Profiling
  • Loading branch information
ziaratban committed Oct 31, 2020
2 parents dd7c555 + 9a3ea1f commit 61e732c
Show file tree
Hide file tree
Showing 36 changed files with 272 additions and 417 deletions.
8 changes: 2 additions & 6 deletions tests/ActiveDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
namespace yiiunit\extensions\mongodb;

use MongoDB\BSON\ObjectID;
use yii;
use yii\data\ActiveDataProvider;
use yii\mongodb\Query;
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
use yiiunit\extensions\mongodb\data\ar\Customer;

class ActiveDataProviderTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
ActiveRecord::$db = $this->getConnection();
$this->setUpTestRows();
}

Expand All @@ -29,7 +27,7 @@ protected function tearDown()
*/
protected function setUpTestRows()
{
$collection = $this->getConnection()->getCollection('customer');
$collection = Yii::$app->mongodb->getCollection('customer');
$rows = [];
for ($i = 1; $i <= 10; $i++) {
$rows[] = [
Expand All @@ -51,14 +49,12 @@ public function testQuery()

$provider = new ActiveDataProvider([
'query' => $query,
'db' => $this->getConnection(),
]);
$models = $provider->getModels();
$this->assertEquals(10, count($models));

$provider = new ActiveDataProvider([
'query' => $query,
'db' => $this->getConnection(),
'pagination' => [
'pageSize' => 5,
]
Expand Down
30 changes: 11 additions & 19 deletions tests/ActiveFixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

class ActiveFixtureTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}

protected function tearDown()
{
Expand All @@ -32,7 +27,7 @@ public function testLoadCollection()
$fixture = $this->getMockBuilder(ActiveFixture::className())
->setConstructorArgs([
[
'db' => $this->getConnection(),
'db' => Yii::$app->mongodb,
'collectionName' => Customer::collectionName()
]
])
Expand All @@ -45,7 +40,7 @@ public function testLoadCollection()

$fixture->load();

$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
$rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName()));
$this->assertCount(2, $rows);
}

Expand All @@ -55,7 +50,7 @@ public function testLoadClass()
$fixture = $this->getMockBuilder(ActiveFixture::className())
->setConstructorArgs([
[
'db' => $this->getConnection(),
'db' => yii::$app->mongodb,
'collectionName' => Customer::collectionName()
]
])
Expand All @@ -68,7 +63,7 @@ public function testLoadClass()

$fixture->load();

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

Expand All @@ -83,7 +78,7 @@ public function testLoadEmptyData()
$fixture = $this->getMockBuilder(ActiveFixture::className())
->setConstructorArgs([
[
'db' => $this->getConnection(),
'db' => Yii::$app->mongodb,
'collectionName' => Customer::collectionName()
]
])
Expand All @@ -95,7 +90,7 @@ public function testLoadEmptyData()

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

$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
$rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName()));
$this->assertEmpty($rows);
}

Expand All @@ -106,7 +101,6 @@ public function testLoadEmptyData()
*/
public function testDefaultDataFile()
{
$db = $this->getConnection();

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

/* @var $fixture ActiveFixture */

$fixture = new $className([
'db' => $db,
'collectionName' => Customer::collectionName(),
]);
$fixture->load();
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
$rows = $this->findAll(Yii::$app->mongodb->getCollection(Customer::collectionName()));
$this->assertCount(2, $rows);

$fixture = new $className([
'db' => $db,
'collectionName' => [
$db->getDefaultDatabaseName(),
Yii::$app->mongodb->getDefaultDatabaseName(),
Customer::collectionName()
],
]);
$fixture->load();
$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
$this->assertCount(3, $rows);
}
}
}
23 changes: 10 additions & 13 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
use yii;
use yii\mongodb\ActiveQuery;
use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
use yiiunit\extensions\mongodb\data\ar\Customer;
use yiiunit\extensions\mongodb\data\ar\Animal;
use yiiunit\extensions\mongodb\data\ar\Dog;
Expand All @@ -22,7 +22,6 @@ class ActiveRecordTest extends TestCase
protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
$this->setUpTestRows();
}

Expand All @@ -37,7 +36,7 @@ protected function tearDown()
*/
protected function setUpTestRows()
{
$collection = $this->getConnection()->getCollection('customer');
$collection = yii::$app->mongodb->getCollection('customer');
$rows = [];
for ($i = 1; $i <= 10; $i++) {
$rows[] = [
Expand Down Expand Up @@ -282,66 +281,64 @@ public function testExists()

public function testScalar()
{
$connection = $this->getConnection();

$result = Customer::find()
->select(['name' => true, '_id' => false])
->orderBy(['name' => SORT_ASC])
->limit(1)
->scalar($connection);
->scalar();
$this->assertSame('name1', $result);

$result = Customer::find()
->select(['name' => true, '_id' => false])
->andWhere(['status' => -1])
->scalar($connection);
->scalar();
$this->assertSame(false, $result);

$result = Customer::find()
->select(['name'])
->orderBy(['name' => SORT_ASC])
->limit(1)
->scalar($connection);
->scalar();
$this->assertSame('name1', $result);

$result = Customer::find()
->select(['_id'])
->limit(1)
->scalar($connection);
->scalar();
$this->assertTrue($result instanceof ObjectID);
}

public function testColumn()
{
$connection = $this->getConnection();

$result = Customer::find()
->select(['name' => true, '_id' => false])
->orderBy(['name' => SORT_ASC])
->limit(2)
->column($connection);
->column();
$this->assertEquals(['name1', 'name10'], $result);

$result = Customer::find()
->select(['name' => true, '_id' => false])
->andWhere(['status' => -1])
->orderBy(['name' => SORT_ASC])
->limit(2)
->column($connection);
->column();
$this->assertEquals([], $result);

$result = Customer::find()
->select(['name'])
->orderBy(['name' => SORT_ASC])
->limit(2)
->column($connection);
->column();
$this->assertEquals(['name1', 'name10'], $result);

$result = Customer::find()
->select(['_id'])
->orderBy(['name' => SORT_ASC])
->limit(2)
->column($connection);
->column();
$this->assertTrue($result[0] instanceof ObjectID);
$this->assertTrue($result[1] instanceof ObjectID);
}
Expand Down
9 changes: 4 additions & 5 deletions tests/ActiveRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace yiiunit\extensions\mongodb;

use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
use yii;
use yiiunit\extensions\mongodb\data\ar\Customer;
use yiiunit\extensions\mongodb\data\ar\CustomerOrder;
use yiiunit\extensions\mongodb\data\ar\Item;
Expand All @@ -12,7 +12,6 @@ class ActiveRelationTest extends TestCase
protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
$this->setUpTestRows();
}

Expand All @@ -38,7 +37,7 @@ protected function setUpTestRows()
'status' => $i,
];
}
$customerCollection = $this->getConnection()->getCollection('customer');
$customerCollection = yii::$app->mongodb->getCollection('customer');
$customers = $customerCollection->batchInsert($customers);

$items = [];
Expand All @@ -48,7 +47,7 @@ protected function setUpTestRows()
'price' => $i,
];
}
$itemCollection = $this->getConnection()->getCollection('item');
$itemCollection = yii::$app->mongodb->getCollection('item');
$items = $itemCollection->batchInsert($items);

$customerOrders = [];
Expand All @@ -70,7 +69,7 @@ protected function setUpTestRows()
],
];
}
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
$customerOrderCollection = yii::$app->mongodb->getCollection('customer_order');
$customerOrderCollection->batchInsert($customerOrders);
}

Expand Down
Loading

0 comments on commit 61e732c

Please sign in to comment.