mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Merge branch 'master' into redis
* master: (613 commits) fixed typo Fixes #1222: refactored jui/Widget, intorduced jui/Slider and jui/SliderInput Correct Nav.php comments/PHP doc to reflect BS3 dropdown support. Correct Nav.php comments/PHP doc to reflect BS3 dropdown support. Correct Nav.php comments/PHP doc to reflect BS3 dropdown support. Added example for dividers to bootstrap Nav Checkboxlist documentation fix Added "Using controller action to render errors" Fix doc Renamed DetailView attribute type to format encode email in Formatter Added default status code setting. "yii\swiftmailer\Mailer" transport setup has been advanced to support constructor arguments and plugins. fix rbac select statement Comments cleanup. Reverted closeButton Nomenclature and code realignment. better nginx config guide about using bootstrap less files Include Schema in new migrations by default ... Conflicts: framework/yii/db/ActiveRecord.php framework/yii/db/ActiveRelation.php tests/unit/data/config.php
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
namespace yiiunit\framework\db;
|
||||
|
||||
use yii\db\Query;
|
||||
use yii\db\ActiveQuery;
|
||||
use yiiunit\data\ar\ActiveRecord;
|
||||
use yiiunit\data\ar\Customer;
|
||||
use yiiunit\data\ar\NullValues;
|
||||
use yiiunit\data\ar\OrderItem;
|
||||
use yiiunit\data\ar\Order;
|
||||
use yiiunit\data\ar\Item;
|
||||
@ -48,20 +48,20 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
$this->assertEquals('user2', $customerName);
|
||||
|
||||
// find by column values
|
||||
$customer = Customer::find(array('id' => 2, 'name' => 'user2'));
|
||||
$customer = Customer::find(['id' => 2, 'name' => 'user2']);
|
||||
$this->assertTrue($customer instanceof Customer);
|
||||
$this->assertEquals('user2', $customer->name);
|
||||
$customer = Customer::find(array('id' => 2, 'name' => 'user1'));
|
||||
$customer = Customer::find(['id' => 2, 'name' => 'user1']);
|
||||
$this->assertNull($customer);
|
||||
|
||||
// find by attributes
|
||||
$customer = Customer::find()->where(array('name' => 'user2'))->one();
|
||||
$customer = Customer::find()->where(['name' => 'user2'])->one();
|
||||
$this->assertTrue($customer instanceof Customer);
|
||||
$this->assertEquals(2, $customer->id);
|
||||
|
||||
// find custom column
|
||||
$customer = Customer::find()->select(array('*', '(status*2) AS status2'))
|
||||
->where(array('name' => 'user3'))->one();
|
||||
$customer = Customer::find()->select(['*', '(status*2) AS status2'])
|
||||
->where(['name' => 'user3'])->one();
|
||||
$this->assertEquals(3, $customer->id);
|
||||
$this->assertEquals(4, $customer->status2);
|
||||
|
||||
@ -79,13 +79,13 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
|
||||
// asArray
|
||||
$customer = Customer::find()->where('id=2')->asArray()->one();
|
||||
$this->assertEquals(array(
|
||||
$this->assertEquals([
|
||||
'id' => '2',
|
||||
'email' => 'user2@example.com',
|
||||
'name' => 'user2',
|
||||
'address' => 'address2',
|
||||
'status' => '1',
|
||||
), $customer);
|
||||
], $customer);
|
||||
|
||||
// indexBy
|
||||
$customers = Customer::find()->indexBy('name')->orderBy('id')->all();
|
||||
@ -116,19 +116,28 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
$this->assertEquals(3, count($customers));
|
||||
|
||||
// find with parameter binding
|
||||
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
|
||||
$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', [':id' => 2])->one();
|
||||
$this->assertTrue($customer instanceof Customer);
|
||||
$this->assertEquals('user2', $customer->name);
|
||||
}
|
||||
|
||||
public function testFindLazy()
|
||||
{
|
||||
/** @var $customer Customer */
|
||||
/** @var Customer $customer */
|
||||
$customer = Customer::find(2);
|
||||
$this->assertFalse($customer->isRelationPopulated('orders'));
|
||||
$orders = $customer->orders;
|
||||
$this->assertTrue($customer->isRelationPopulated('orders'));
|
||||
$this->assertEquals(2, count($orders));
|
||||
$this->assertEquals(1, count($customer->populatedRelations));
|
||||
|
||||
/** @var Customer $customer */
|
||||
$customer = Customer::find(2);
|
||||
$this->assertFalse($customer->isRelationPopulated('orders'));
|
||||
$orders = $customer->getOrders()->where('id=3')->all();
|
||||
$this->assertFalse($customer->isRelationPopulated('orders'));
|
||||
$this->assertEquals(0, count($customer->populatedRelations));
|
||||
|
||||
$this->assertEquals(1, count($orders));
|
||||
$this->assertEquals(3, $orders[0]->id);
|
||||
}
|
||||
@ -137,13 +146,20 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
{
|
||||
$customers = Customer::find()->with('orders')->all();
|
||||
$this->assertEquals(3, count($customers));
|
||||
$this->assertTrue($customers[0]->isRelationPopulated('orders'));
|
||||
$this->assertTrue($customers[1]->isRelationPopulated('orders'));
|
||||
$this->assertEquals(1, count($customers[0]->orders));
|
||||
$this->assertEquals(2, count($customers[1]->orders));
|
||||
|
||||
$customer = Customer::find()->with('orders')->one();
|
||||
$this->assertTrue($customer->isRelationPopulated('orders'));
|
||||
$this->assertEquals(1, count($customer->orders));
|
||||
$this->assertEquals(1, count($customer->populatedRelations));
|
||||
}
|
||||
|
||||
public function testFindLazyVia()
|
||||
{
|
||||
/** @var $order Order */
|
||||
/** @var Order $order */
|
||||
$order = Order::find(1);
|
||||
$this->assertEquals(1, $order->id);
|
||||
$this->assertEquals(2, count($order->items));
|
||||
@ -152,7 +168,7 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
|
||||
$order = Order::find(1);
|
||||
$order->id = 100;
|
||||
$this->assertEquals(array(), $order->items);
|
||||
$this->assertEquals([], $order->items);
|
||||
}
|
||||
|
||||
public function testFindEagerViaRelation()
|
||||
@ -168,7 +184,7 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
|
||||
public function testFindLazyViaTable()
|
||||
{
|
||||
/** @var $order Order */
|
||||
/** @var Order $order */
|
||||
$order = Order::find(1);
|
||||
$this->assertEquals(1, $order->id);
|
||||
$this->assertEquals(2, count($order->books));
|
||||
@ -242,12 +258,12 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
// via table
|
||||
$order = Order::find(2);
|
||||
$this->assertEquals(0, count($order->books));
|
||||
$orderItem = OrderItem::find(array('order_id' => 2, 'item_id' => 1));
|
||||
$orderItem = OrderItem::find(['order_id' => 2, 'item_id' => 1]);
|
||||
$this->assertNull($orderItem);
|
||||
$item = Item::find(1);
|
||||
$order->link('books', $item, array('quantity' => 10, 'subtotal' => 100));
|
||||
$order->link('books', $item, ['quantity' => 10, 'subtotal' => 100]);
|
||||
$this->assertEquals(1, count($order->books));
|
||||
$orderItem = OrderItem::find(array('order_id' => 2, 'item_id' => 1));
|
||||
$orderItem = OrderItem::find(['order_id' => 2, 'item_id' => 1]);
|
||||
$this->assertTrue($orderItem instanceof OrderItem);
|
||||
$this->assertEquals(10, $orderItem->quantity);
|
||||
$this->assertEquals(100, $orderItem->subtotal);
|
||||
@ -256,13 +272,13 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
$order = Order::find(1);
|
||||
$this->assertEquals(2, count($order->items));
|
||||
$this->assertEquals(2, count($order->orderItems));
|
||||
$orderItem = OrderItem::find(array('order_id' => 1, 'item_id' => 3));
|
||||
$orderItem = OrderItem::find(['order_id' => 1, 'item_id' => 3]);
|
||||
$this->assertNull($orderItem);
|
||||
$item = Item::find(3);
|
||||
$order->link('items', $item, array('quantity' => 10, 'subtotal' => 100));
|
||||
$order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]);
|
||||
$this->assertEquals(3, count($order->items));
|
||||
$this->assertEquals(3, count($order->orderItems));
|
||||
$orderItem = OrderItem::find(array('order_id' => 1, 'item_id' => 3));
|
||||
$orderItem = OrderItem::find(['order_id' => 1, 'item_id' => 3]);
|
||||
$this->assertTrue($orderItem instanceof OrderItem);
|
||||
$this->assertEquals(10, $orderItem->quantity);
|
||||
$this->assertEquals(100, $orderItem->subtotal);
|
||||
@ -302,10 +318,14 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
|
||||
$this->assertNull($customer->id);
|
||||
$this->assertTrue($customer->isNewRecord);
|
||||
Customer::$afterSaveNewRecord = null;
|
||||
Customer::$afterSaveInsert = null;
|
||||
|
||||
$customer->save();
|
||||
|
||||
$this->assertEquals(4, $customer->id);
|
||||
$this->assertFalse(Customer::$afterSaveNewRecord);
|
||||
$this->assertTrue(Customer::$afterSaveInsert);
|
||||
$this->assertFalse($customer->isNewRecord);
|
||||
}
|
||||
|
||||
@ -316,18 +336,23 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
$this->assertTrue($customer instanceof Customer);
|
||||
$this->assertEquals('user2', $customer->name);
|
||||
$this->assertFalse($customer->isNewRecord);
|
||||
Customer::$afterSaveNewRecord = null;
|
||||
Customer::$afterSaveInsert = null;
|
||||
|
||||
$customer->name = 'user2x';
|
||||
$customer->save();
|
||||
$this->assertEquals('user2x', $customer->name);
|
||||
$this->assertFalse($customer->isNewRecord);
|
||||
$this->assertFalse(Customer::$afterSaveNewRecord);
|
||||
$this->assertFalse(Customer::$afterSaveInsert);
|
||||
$customer2 = Customer::find(2);
|
||||
$this->assertEquals('user2x', $customer2->name);
|
||||
|
||||
// updateCounters
|
||||
$pk = array('order_id' => 2, 'item_id' => 4);
|
||||
$pk = ['order_id' => 2, 'item_id' => 4];
|
||||
$orderItem = OrderItem::find($pk);
|
||||
$this->assertEquals(1, $orderItem->quantity);
|
||||
$ret = $orderItem->updateCounters(array('quantity' => -1));
|
||||
$ret = $orderItem->updateCounters(['quantity' => -1]);
|
||||
$this->assertTrue($ret);
|
||||
$this->assertEquals(0, $orderItem->quantity);
|
||||
$orderItem = OrderItem::find($pk);
|
||||
@ -336,21 +361,19 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
// updateAll
|
||||
$customer = Customer::find(3);
|
||||
$this->assertEquals('user3', $customer->name);
|
||||
$ret = Customer::updateAll(array(
|
||||
'name' => 'temp',
|
||||
), array('id' => 3));
|
||||
$ret = Customer::updateAll(['name' => 'temp'], ['id' => 3]);
|
||||
$this->assertEquals(1, $ret);
|
||||
$customer = Customer::find(3);
|
||||
$this->assertEquals('temp', $customer->name);
|
||||
|
||||
// updateCounters
|
||||
$pk = array('order_id' => 1, 'item_id' => 2);
|
||||
$pk = ['order_id' => 1, 'item_id' => 2];
|
||||
$orderItem = OrderItem::find($pk);
|
||||
$this->assertEquals(2, $orderItem->quantity);
|
||||
$ret = OrderItem::updateAllCounters(array(
|
||||
$ret = OrderItem::updateAllCounters([
|
||||
'quantity' => 3,
|
||||
'subtotal' => -10,
|
||||
), $pk);
|
||||
], $pk);
|
||||
$this->assertEquals(1, $ret);
|
||||
$orderItem = OrderItem::find($pk);
|
||||
$this->assertEquals(5, $orderItem->quantity);
|
||||
@ -375,4 +398,118 @@ class ActiveRecordTest extends DatabaseTestCase
|
||||
$customers = Customer::find()->all();
|
||||
$this->assertEquals(0, count($customers));
|
||||
}
|
||||
|
||||
public function testStoreNull()
|
||||
{
|
||||
$record = new NullValues();
|
||||
$this->assertNull($record->var1);
|
||||
$this->assertNull($record->var2);
|
||||
$this->assertNull($record->var3);
|
||||
$this->assertNull($record->stringcol);
|
||||
|
||||
$record->id = 1;
|
||||
|
||||
$record->var1 = 123;
|
||||
$record->var2 = 456;
|
||||
$record->var3 = 789;
|
||||
$record->stringcol = 'hello!';
|
||||
|
||||
$record->save(false);
|
||||
$this->assertTrue($record->refresh());
|
||||
|
||||
$this->assertEquals(123, $record->var1);
|
||||
$this->assertEquals(456, $record->var2);
|
||||
$this->assertEquals(789, $record->var3);
|
||||
$this->assertEquals('hello!', $record->stringcol);
|
||||
|
||||
$record->var1 = null;
|
||||
$record->var2 = null;
|
||||
$record->var3 = null;
|
||||
$record->stringcol = null;
|
||||
|
||||
$record->save(false);
|
||||
$this->assertTrue($record->refresh());
|
||||
|
||||
$this->assertNull($record->var1);
|
||||
$this->assertNull($record->var2);
|
||||
$this->assertNull($record->var3);
|
||||
$this->assertNull($record->stringcol);
|
||||
|
||||
$record->var1 = 0;
|
||||
$record->var2 = 0;
|
||||
$record->var3 = 0;
|
||||
$record->stringcol = '';
|
||||
|
||||
$record->save(false);
|
||||
$this->assertTrue($record->refresh());
|
||||
|
||||
$this->assertEquals(0, $record->var1);
|
||||
$this->assertEquals(0, $record->var2);
|
||||
$this->assertEquals(0, $record->var3);
|
||||
$this->assertEquals('', $record->stringcol);
|
||||
}
|
||||
|
||||
public function testStoreEmpty()
|
||||
{
|
||||
$record = new NullValues();
|
||||
$record->id = 1;
|
||||
|
||||
// this is to simulate empty html form submission
|
||||
$record->var1 = '';
|
||||
$record->var2 = '';
|
||||
$record->var3 = '';
|
||||
$record->stringcol = '';
|
||||
|
||||
$record->save(false);
|
||||
$this->assertTrue($record->refresh());
|
||||
|
||||
// https://github.com/yiisoft/yii2/commit/34945b0b69011bc7cab684c7f7095d837892a0d4#commitcomment-4458225
|
||||
$this->assertTrue($record->var1 === $record->var2);
|
||||
$this->assertTrue($record->var2 === $record->var3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Some PDO implementations(e.g. cubrid) do not support boolean values.
|
||||
* Make sure this does not affect AR layer.
|
||||
*/
|
||||
public function testBooleanAttribute()
|
||||
{
|
||||
$customer = new Customer();
|
||||
$customer->name = 'boolean customer';
|
||||
$customer->email = 'mail@example.com';
|
||||
$customer->status = true;
|
||||
$customer->save(false);
|
||||
|
||||
$customer->refresh();
|
||||
$this->assertEquals(1, $customer->status);
|
||||
|
||||
$customer->status = false;
|
||||
$customer->save(false);
|
||||
|
||||
$customer->refresh();
|
||||
$this->assertEquals(0, $customer->status);
|
||||
|
||||
$customers = Customer::find()->where(['status' => true])->all();
|
||||
$this->assertEquals(2, count($customers));
|
||||
|
||||
$customers = Customer::find()->where(['status' => false])->all();
|
||||
$this->assertEquals(1, count($customers));
|
||||
}
|
||||
|
||||
public function testIsPrimaryKey()
|
||||
{
|
||||
$this->assertFalse(Customer::isPrimaryKey([]));
|
||||
$this->assertTrue(Customer::isPrimaryKey(['id']));
|
||||
$this->assertFalse(Customer::isPrimaryKey(['id', 'name']));
|
||||
$this->assertFalse(Customer::isPrimaryKey(['name']));
|
||||
$this->assertFalse(Customer::isPrimaryKey(['name', 'email']));
|
||||
|
||||
$this->assertFalse(OrderItem::isPrimaryKey([]));
|
||||
$this->assertFalse(OrderItem::isPrimaryKey(['order_id']));
|
||||
$this->assertFalse(OrderItem::isPrimaryKey(['item_id']));
|
||||
$this->assertFalse(OrderItem::isPrimaryKey(['quantity']));
|
||||
$this->assertFalse(OrderItem::isPrimaryKey(['quantity', 'subtotal']));
|
||||
$this->assertTrue(OrderItem::isPrimaryKey(['order_id', 'item_id']));
|
||||
$this->assertFalse(OrderItem::isPrimaryKey(['order_id', 'item_id', 'quantity']));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user