Commit 9ee76322 by Carsten Brandt

Added failing test cases for #3725

parent 583c660c
......@@ -36,6 +36,17 @@ class Customer extends ActiveRecord
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');
}
public function getExpensiveOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('total > 50')->orderBy('id');
}
public function getExpensiveOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->andWhere('total > 50')->orderBy('id');
}
public function getOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('id');
......
......@@ -35,6 +35,16 @@ class Customer extends ActiveRecord
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('created_at');
}
public function getExpensiveOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->filter(['range' => ['total' => ['gte' => 50]]])->orderBy('id');
}
public function getExpensiveOrdersWithNullFK()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->filter(['range' => ['total' => ['gte' => 50]]])->orderBy('id');
}
public function getOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('created_at');
......
......@@ -30,6 +30,22 @@ class Customer extends ActiveRecord
/**
* @return \yii\redis\ActiveQuery
*/
public function getExpensiveOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere("tonumber(redis.call('HGET','order' .. ':a:' .. pk, 'total')) > 50");
}
/**
* @return \yii\redis\ActiveQuery
*/
public function getExpensiveOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->andWhere("tonumber(redis.call('HGET','order' .. ':a:' .. pk, 'total')) > 50");
}
/**
* @return \yii\redis\ActiveQuery
*/
public function getOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id']);
......
......@@ -728,6 +728,54 @@ trait ActiveRecordTestTrait
$this->assertEquals(2, count($order->orderItems));
}
public function testUnlinkAndConditionSetNull()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $customerClass \yii\db\ActiveRecordInterface */
$customerClass = $this->getCustomerClass();
/* @var $orderClass \yii\db\ActiveRecordInterface */
$orderClass = $this->getOrderClass();
// in this test all orders are owned by customer 1
$orderClass::updateAll(['customer_id' => 1]);
$this->afterSave();
// removing a model that is not related should not work
$customer = $customerClass::findOne(1);
$this->assertEquals(3, count($customer->ordersWithNullFK));
$this->assertEquals(1, count($customer->expensiveOrdersWithNullFK));
$customer->unlink('expensiveOrdersWithNullFK', $customer->orders[2], false);
$this->afterSave();
$customer = $customerClass::findOne(1);
$this->assertEquals(3, count($customer->ordersWithNullFK));
$this->assertEquals(1, count($customer->expensiveOrdersWithNullFK));
}
public function testUnlinkAndConditionDelete()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $customerClass \yii\db\ActiveRecordInterface */
$customerClass = $this->getCustomerClass();
/* @var $orderClass \yii\db\ActiveRecordInterface */
$orderClass = $this->getOrderClass();
// in this test all orders are owned by customer 1
$orderClass::updateAll(['customer_id' => 1]);
$this->afterSave();
// removing a model that is not related should not work
$customer = $customerClass::findOne(1);
$this->assertEquals(3, count($customer->orders));
$this->assertEquals(1, count($customer->expensiveOrders));
$customer->unlink('expensiveOrders', $customer->orders[2], true);
$this->afterSave();
$customer = $customerClass::findOne(1);
$this->assertEquals(3, count($customer->orders));
$this->assertEquals(1, count($customer->expensiveOrders));
}
public function testUnlinkAll()
{
/* @var $customerClass \yii\db\ActiveRecordInterface */
......@@ -794,6 +842,52 @@ trait ActiveRecordTestTrait
// via table is covered in \yiiunit\framework\db\ActiveRecordTest::testUnlinkAllViaTable()
}
public function testUnlinkAllAndConditionSetNull()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $customerClass \yii\db\BaseActiveRecord */
$customerClass = $this->getCustomerClass();
/* @var $orderClass \yii\db\BaseActiveRecord */
$orderClass = $this->getOrderClass();
// in this test all orders are owned by customer 1
$orderClass::updateAll(['customer_id' => 1]);
$this->afterSave();
$customer = $customerClass::findOne(1);
$this->assertEquals(3, count($customer->ordersWithNullFK));
$this->assertEquals(1, count($customer->expensiveOrdersWithNullFK));
$this->assertEquals(3, $orderClass::find()->count());
$customer->unlinkAll('expensiveOrdersWithNullFK');
$this->assertEquals(2, count($customer->ordersWithNullFK));
$this->assertEquals(0, count($customer->expensiveOrdersWithNullFK));
$this->assertEquals(3, $orderClass::find()->count());
}
public function testUnlinkAllAndConditionDelete()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $customerClass \yii\db\BaseActiveRecord */
$customerClass = $this->getCustomerClass();
/* @var $orderClass \yii\db\BaseActiveRecord */
$orderClass = $this->getOrderClass();
// in this test all orders are owned by customer 1
$orderClass::updateAll(['customer_id' => 1]);
$this->afterSave();
$customer = $customerClass::findOne(1);
$this->assertEquals(3, count($customer->orders));
$this->assertEquals(1, count($customer->expensiveOrders));
$this->assertEquals(3, $orderClass::find()->count());
$customer->unlinkAll('expensiveOrders', true);
$this->assertEquals(2, count($customer->orders));
$this->assertEquals(0, count($customer->expensiveOrders));
$this->assertEquals(2, $orderClass::find()->count());
}
public static $afterSaveNewRecord;
public static $afterSaveInsert;
......
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