Browse Source

added tests for #9047

1. verify it only affects composite primary key situation
2. add failing test for composite primary key situation with join
tags/2.0.8
Carsten Brandt 9 years ago
parent
commit
37d2373911
  1. 12
      tests/data/ar/Order.php
  2. 11
      tests/data/ar/OrderItem.php
  3. 33
      tests/framework/db/ActiveRecordTest.php

12
tests/data/ar/Order.php

@ -24,6 +24,18 @@ class Order extends ActiveRecord
return $this->hasOne(Customer::className(), ['id' => 'customer_id']); return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
} }
public function getCustomerJoinedWithProfile()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id'])
->joinWith('profile');
}
public function getCustomerJoinedWithProfileIndexOrdered()
{
return $this->hasMany(Customer::className(), ['id' => 'customer_id'])
->joinWith('profile')->orderBy(['profile.description' => SORT_ASC])->indexBy('name');
}
public function getCustomer2() public function getCustomer2()
{ {
return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->inverseOf('orders2'); return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->inverseOf('orders2');

11
tests/data/ar/OrderItem.php

@ -28,4 +28,15 @@ class OrderItem extends ActiveRecord
{ {
return $this->hasOne(Item::className(), ['id' => 'item_id']); return $this->hasOne(Item::className(), ['id' => 'item_id']);
} }
// relations used by ::testFindCompositeWithJoin()
public function getOrderItemCompositeWithJoin()
{
return $this->hasOne(OrderItem::className(), ['item_id' => 'item_id', 'order_id' => 'order_id' ])
->joinWith('item');
}
public function getOrderItemCompositeNoJoin()
{
return $this->hasOne(OrderItem::className(), ['item_id' => 'item_id', 'order_id' => 'order_id' ]);
}
} }

33
tests/framework/db/ActiveRecordTest.php

@ -815,6 +815,39 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(0, count($orders[0]->itemsIndexed)); $this->assertEquals(0, count($orders[0]->itemsIndexed));
} }
/**
* https://github.com/yiisoft/yii2/issues/10201
* https://github.com/yiisoft/yii2/issues/9047
*/
public function testFindCompositeRelationWithJoin()
{
/* @var $orderItem OrderItem */
$orderItem = OrderItem::findOne([1, 1]);
$orderItemNoJoin = $orderItem->orderItemCompositeNoJoin;
$this->assertInstanceOf('yiiunit\data\ar\OrderItem', $orderItemNoJoin);
$orderItemWithJoin = $orderItem->orderItemCompositeWithJoin;
$this->assertInstanceOf('yiiunit\data\ar\OrderItem', $orderItemWithJoin);
}
public function testFindSimpleRelationWithJoin()
{
/* @var $order Order */
$order = Order::findOne(1);
$customerNoJoin = $order->customer;
$this->assertInstanceOf('yiiunit\data\ar\Customer', $customerNoJoin);
$customerWithJoin = $order->customerJoinedWithProfile;
$this->assertInstanceOf('yiiunit\data\ar\Customer', $customerWithJoin);
$customerWithJoinIndexOrdered = $order->customerJoinedWithProfileIndexOrdered;
$this->assertTrue(is_array($customerWithJoinIndexOrdered));
$this->assertArrayHasKey('user1', $customerWithJoinIndexOrdered);
$this->assertInstanceOf('yiiunit\data\ar\Customer', $customerWithJoinIndexOrdered['user1']);
}
public function tableNameProvider() public function tableNameProvider()
{ {
return [ return [

Loading…
Cancel
Save