Commit 64359373 by Carsten Brandt

added test case for #1310

parent f017ba35
......@@ -35,6 +35,22 @@ class Order extends ActiveRecord
})->orderBy('id');
}
public function getItemsInOrder1()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) {
$q->orderBy(['subtotal' => SORT_ASC]);
})->orderBy('name');
}
public function getItemsInOrder2()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) {
$q->orderBy(['subtotal' => SORT_DESC]);
})->orderBy('name');
}
public function getBooks()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......
......@@ -33,6 +33,22 @@ class Order extends ActiveRecord
->via('orderItems')->orderBy('name');
}
public function getItemsInOrder1()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) {
$q->orderBy(['subtotal' => SORT_ASC]);
})->orderBy('name');
}
public function getItemsInOrder2()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) {
$q->orderBy(['subtotal' => SORT_DESC]);
})->orderBy('name');
}
// public function getBooks()
// {
// return $this->hasMany('Item', ['primaryKey' => 'item_id'])
......
......@@ -27,6 +27,22 @@ class Order extends ActiveRecord
});
}
public function getItemsInOrder1()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) {
$q->orderBy(['subtotal' => SORT_ASC]);
})->orderBy('name');
}
public function getItemsInOrder2()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function ($q) {
$q->orderBy(['subtotal' => SORT_DESC]);
})->orderBy('name');
}
public function getBooks()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......
......@@ -165,8 +165,8 @@ trait ActiveRecordTestTrait
public function testFindColumn()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$this->assertEquals(['user1', 'user2', 'user3'], Customer::find()->column('name'));
$this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->column('name'));
$this->assertEquals(['user1', 'user2', 'user3'], $this->callCustomerFind()->column('name'));
$this->assertEquals(['user3', 'user2', 'user1'], $this->callCustomerFind()->orderBy(['name' => SORT_DESC])->column('name'));
}
public function testfindIndexBy()
......@@ -378,6 +378,7 @@ trait ActiveRecordTestTrait
$this->assertEquals(3, count($orders));
$order = $orders[0];
$this->assertEquals(1, $order->id);
$this->assertTrue($order->isRelationPopulated('items'));
$this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id);
......@@ -388,14 +389,78 @@ trait ActiveRecordTestTrait
/** @var TestCase|ActiveRecordTestTrait $this */
$customers = $this->callCustomerFind()->with('orders', 'orders.items')->all();
$this->assertEquals(3, count($customers));
$this->assertTrue($customers[0]->isRelationPopulated('orders'));
$this->assertTrue($customers[1]->isRelationPopulated('orders'));
$this->assertTrue($customers[2]->isRelationPopulated('orders'));
$this->assertEquals(1, count($customers[0]->orders));
$this->assertEquals(2, count($customers[1]->orders)); // TODO check is populated
$this->assertEquals(2, count($customers[1]->orders));
$this->assertEquals(0, count($customers[2]->orders));
$this->assertTrue($customers[0]->orders[0]->isRelationPopulated('items'));
$this->assertTrue($customers[1]->orders[0]->isRelationPopulated('items'));
$this->assertTrue($customers[1]->orders[1]->isRelationPopulated('items'));
$this->assertEquals(2, count($customers[0]->orders[0]->items));
$this->assertEquals(3, count($customers[1]->orders[0]->items));
$this->assertEquals(1, count($customers[1]->orders[1]->items));
}
/**
* Ensure ActiveRelation does preserve order of items on find via()
* https://github.com/yiisoft/yii2/issues/1310
*/
public function testFindEagerViaRelationPreserveOrder()
{
/** @var TestCase|ActiveRecordTestTrait $this */
$orders = $this->callOrderFind()->with('itemsInOrder1')->orderBy('create_time')->all();
$this->assertEquals(3, count($orders));
$order = $orders[0];
$this->assertEquals(1, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
$this->assertEquals(2, count($order->itemsInOrder1));
$this->assertEquals(1, $order->itemsInOrder1[0]->id);
$this->assertEquals(2, $order->itemsInOrder1[1]->id);
$order = $orders[1];
$this->assertEquals(2, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
$this->assertEquals(3, count($order->itemsInOrder1));
$this->assertEquals(5, $order->itemsInOrder1[0]->id);
$this->assertEquals(3, $order->itemsInOrder1[1]->id);
$this->assertEquals(4, $order->itemsInOrder1[2]->id);
$order = $orders[3];
$this->assertEquals(3, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
$this->assertEquals(0, count($order->itemsInOrder1));
}
// different order in via table
public function testFindEagerViaRelationPreserveOrderB()
{
$orders = $this->callOrderFind()->with('itemsInOrder2')->orderBy('create_time')->all();
$this->assertEquals(3, count($orders));
$order = $orders[0];
$this->assertEquals(1, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
$this->assertEquals(2, count($order->itemsInOrder2));
$this->assertEquals(1, $order->itemsInOrder2[0]->id);
$this->assertEquals(2, $order->itemsInOrder2[1]->id);
$order = $orders[1];
$this->assertEquals(2, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
$this->assertEquals(3, count($order->itemsInOrder2));
$this->assertEquals(5, $order->itemsInOrder2[0]->id);
$this->assertEquals(3, $order->itemsInOrder2[1]->id);
$this->assertEquals(4, $order->itemsInOrder2[2]->id);
$order = $orders[3];
$this->assertEquals(3, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
$this->assertEquals(0, count($order->itemsInOrder2));
}
public function testLink()
{
$orderClass = $this->getOrderClass();
......
......@@ -121,7 +121,6 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(2, $order->books[0]->id);
}
public function testStoreNull()
{
$record = new NullValues();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment