diff --git a/tests/unit/data/ar/Customer.php b/tests/unit/data/ar/Customer.php index e7cab01..fd986b7 100644 --- a/tests/unit/data/ar/Customer.php +++ b/tests/unit/data/ar/Customer.php @@ -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'); diff --git a/tests/unit/data/ar/elasticsearch/Customer.php b/tests/unit/data/ar/elasticsearch/Customer.php index f05b168..73ad378 100644 --- a/tests/unit/data/ar/elasticsearch/Customer.php +++ b/tests/unit/data/ar/elasticsearch/Customer.php @@ -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'); diff --git a/tests/unit/data/ar/redis/Customer.php b/tests/unit/data/ar/redis/Customer.php index da04102..6c850ad 100644 --- a/tests/unit/data/ar/redis/Customer.php +++ b/tests/unit/data/ar/redis/Customer.php @@ -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']); diff --git a/tests/unit/framework/ar/ActiveRecordTestTrait.php b/tests/unit/framework/ar/ActiveRecordTestTrait.php index 5029a18..d04af1c 100644 --- a/tests/unit/framework/ar/ActiveRecordTestTrait.php +++ b/tests/unit/framework/ar/ActiveRecordTestTrait.php @@ -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;