Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test case improving #316

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions tests/ActiveDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use MongoDB\BSON\ObjectID;
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
Expand All @@ -14,7 +13,6 @@ 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');
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
$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
23 changes: 10 additions & 13 deletions tests/ActiveFixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testLoadCollection()
$fixture = $this->getMockBuilder(ActiveFixture::className())
->setConstructorArgs([
[
'db' => $this->getConnection(),
'db' => yii::$app->mongodb,
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
'collectionName' => Customer::collectionName()
]
])
Expand All @@ -45,7 +45,7 @@ public function testLoadCollection()

$fixture->load();

$rows = $this->findAll($this->getConnection()->getCollection(Customer::collectionName()));
$rows = $this->findAll(yii::$app->mongodb->getCollection(Customer::collectionName()));
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
$this->assertCount(2, $rows);
}

Expand All @@ -55,7 +55,7 @@ public function testLoadClass()
$fixture = $this->getMockBuilder(ActiveFixture::className())
->setConstructorArgs([
[
'db' => $this->getConnection(),
'db' => yii::$app->mongodb,
'collectionName' => Customer::collectionName()
]
])
Expand All @@ -68,7 +68,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 +83,7 @@ public function testLoadEmptyData()
$fixture = $this->getMockBuilder(ActiveFixture::className())
->setConstructorArgs([
[
'db' => $this->getConnection(),
'db' => yii::$app->mongodb,
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
'collectionName' => Customer::collectionName()
]
])
Expand All @@ -95,7 +95,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()));
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEmpty($rows);
}

Expand All @@ -106,7 +106,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 +137,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';
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
$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()));
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
$this->assertCount(2, $rows);

$fixture = new $className([
'db' => $db,
'collectionName' => [
$db->getDefaultDatabaseName(),
yii::$app->mongodb->getDefaultDatabaseName(),
ziaratban marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
22 changes: 9 additions & 13 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
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 +21,6 @@ class ActiveRecordTest extends TestCase
protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
$this->setUpTestRows();
}

Expand All @@ -37,7 +35,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 +280,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
8 changes: 3 additions & 5 deletions tests/ActiveRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace yiiunit\extensions\mongodb;

use yiiunit\extensions\mongodb\data\ar\ActiveRecord;
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 +11,6 @@ class ActiveRelationTest extends TestCase
protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
$this->setUpTestRows();
}

Expand All @@ -38,7 +36,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 +46,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 +68,7 @@ protected function setUpTestRows()
],
];
}
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
$customerOrderCollection = yii::$app->mongodb->getCollection('customer_order');
$customerOrderCollection->batchInsert($customerOrders);
}

Expand Down
Loading