mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-09 01:27:20 +08:00
Unit test for Mongo Active Relation added.
This commit is contained in:
@ -24,4 +24,9 @@ class Customer extends ActiveRecord
|
|||||||
{
|
{
|
||||||
$query->andWhere(['status' => 2]);
|
$query->andWhere(['status' => 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getOrders()
|
||||||
|
{
|
||||||
|
return $this->hasMany(CustomerOrder::className(), ['customer_id' => 'id']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
27
tests/unit/data/ar/mongo/CustomerOrder.php
Normal file
27
tests/unit/data/ar/mongo/CustomerOrder.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace yiiunit\data\ar\mongo;
|
||||||
|
|
||||||
|
|
||||||
|
class CustomerOrder extends ActiveRecord
|
||||||
|
{
|
||||||
|
public static function collectionName()
|
||||||
|
{
|
||||||
|
return 'customer_order';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'_id',
|
||||||
|
'number',
|
||||||
|
'customer_id',
|
||||||
|
'items',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCustomer()
|
||||||
|
{
|
||||||
|
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
|
||||||
|
}
|
||||||
|
}
|
||||||
83
tests/unit/extensions/mongo/ActiveRelationTest.php
Normal file
83
tests/unit/extensions/mongo/ActiveRelationTest.php
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace yiiunit\extensions\mongo;
|
||||||
|
|
||||||
|
use yiiunit\data\ar\mongo\ActiveRecord;
|
||||||
|
use yiiunit\data\ar\mongo\Customer;
|
||||||
|
use yiiunit\data\ar\mongo\CustomerOrder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group mongo
|
||||||
|
*/
|
||||||
|
class ActiveRelationTest extends MongoTestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
ActiveRecord::$db = $this->getConnection();
|
||||||
|
$this->setUpTestRows();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
$this->dropCollection(Customer::collectionName());
|
||||||
|
$this->dropCollection(CustomerOrder::collectionName());
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up test rows.
|
||||||
|
*/
|
||||||
|
protected function setUpTestRows()
|
||||||
|
{
|
||||||
|
$customerCollection = $this->getConnection()->getCollection('customer');
|
||||||
|
|
||||||
|
$customers = [];
|
||||||
|
for ($i = 1; $i <= 5; $i++) {
|
||||||
|
$customers[] = [
|
||||||
|
'name' => 'name' . $i,
|
||||||
|
'email' => 'email' . $i,
|
||||||
|
'address' => 'address' . $i,
|
||||||
|
'status' => $i,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$customerCollection->batchInsert($customers);
|
||||||
|
|
||||||
|
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
|
||||||
|
$customerOrders = [];
|
||||||
|
foreach ($customers as $customer) {
|
||||||
|
$customerOrders[] = [
|
||||||
|
'customer_id' => $customer['_id'],
|
||||||
|
'number' => $customer['status'],
|
||||||
|
];
|
||||||
|
$customerOrders[] = [
|
||||||
|
'customer_id' => $customer['_id'],
|
||||||
|
'number' => $customer['status'] + 1,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$customerOrderCollection->batchInsert($customerOrders);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests :
|
||||||
|
|
||||||
|
public function testFindLazy()
|
||||||
|
{
|
||||||
|
/** @var CustomerOrder $order */
|
||||||
|
$order = CustomerOrder::find(['number' => 2]);
|
||||||
|
$this->assertFalse($order->isRelationPopulated('customer'));
|
||||||
|
$index = $order->customer;
|
||||||
|
$this->assertTrue($order->isRelationPopulated('customer'));
|
||||||
|
$this->assertTrue($index instanceof Customer);
|
||||||
|
$this->assertEquals(1, count($order->populatedRelations));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindEager()
|
||||||
|
{
|
||||||
|
$orders = CustomerOrder::find()->with('customer')->all();
|
||||||
|
$this->assertEquals(10, count($orders));
|
||||||
|
$this->assertTrue($orders[0]->isRelationPopulated('customer'));
|
||||||
|
$this->assertTrue($orders[1]->isRelationPopulated('customer'));
|
||||||
|
$this->assertTrue($orders[0]->index instanceof ArticleIndex);
|
||||||
|
$this->assertTrue($orders[1]->index instanceof ArticleIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user