You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
7.3 KiB
194 lines
7.3 KiB
12 years ago
|
<?php
|
||
|
|
||
11 years ago
|
namespace yiiunit\extensions\redis;
|
||
12 years ago
|
|
||
11 years ago
|
use yii\redis\ActiveQuery;
|
||
12 years ago
|
use yiiunit\data\ar\redis\ActiveRecord;
|
||
|
use yiiunit\data\ar\redis\Customer;
|
||
|
use yiiunit\data\ar\redis\OrderItem;
|
||
|
use yiiunit\data\ar\redis\Order;
|
||
|
use yiiunit\data\ar\redis\Item;
|
||
11 years ago
|
use yiiunit\framework\ar\ActiveRecordTestTrait;
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* @group redis
|
||
|
*/
|
||
12 years ago
|
class ActiveRecordTest extends RedisTestCase
|
||
|
{
|
||
11 years ago
|
use ActiveRecordTestTrait;
|
||
|
|
||
|
public function callCustomerFind($q = null) { return Customer::find($q); }
|
||
|
public function callOrderFind($q = null) { return Order::find($q); }
|
||
|
public function callOrderItemFind($q = null) { return OrderItem::find($q); }
|
||
|
public function callItemFind($q = null) { return Item::find($q); }
|
||
|
|
||
|
public function getCustomerClass() { return Customer::className(); }
|
||
|
public function getItemClass() { return Item::className(); }
|
||
|
public function getOrderClass() { return Order::className(); }
|
||
|
public function getOrderItemClass() { return OrderItem::className(); }
|
||
|
|
||
|
|
||
12 years ago
|
public function setUp()
|
||
|
{
|
||
11 years ago
|
parent::setUp();
|
||
12 years ago
|
ActiveRecord::$db = $this->getConnection();
|
||
|
|
||
|
$customer = new Customer();
|
||
11 years ago
|
$customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1], false);
|
||
12 years ago
|
$customer->save(false);
|
||
|
$customer = new Customer();
|
||
11 years ago
|
$customer->setAttributes(['email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1], false);
|
||
12 years ago
|
$customer->save(false);
|
||
|
$customer = new Customer();
|
||
11 years ago
|
$customer->setAttributes(['email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2], false);
|
||
12 years ago
|
$customer->save(false);
|
||
|
|
||
|
// INSERT INTO tbl_category (name) VALUES ('Books');
|
||
|
// INSERT INTO tbl_category (name) VALUES ('Movies');
|
||
|
|
||
|
$item = new Item();
|
||
11 years ago
|
$item->setAttributes(['name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1], false);
|
||
12 years ago
|
$item->save(false);
|
||
|
$item = new Item();
|
||
11 years ago
|
$item->setAttributes(['name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1], false);
|
||
12 years ago
|
$item->save(false);
|
||
|
$item = new Item();
|
||
11 years ago
|
$item->setAttributes(['name' => 'Ice Age', 'category_id' => 2], false);
|
||
12 years ago
|
$item->save(false);
|
||
|
$item = new Item();
|
||
11 years ago
|
$item->setAttributes(['name' => 'Toy Story', 'category_id' => 2], false);
|
||
12 years ago
|
$item->save(false);
|
||
|
$item = new Item();
|
||
11 years ago
|
$item->setAttributes(['name' => 'Cars', 'category_id' => 2], false);
|
||
12 years ago
|
$item->save(false);
|
||
|
|
||
|
$order = new Order();
|
||
11 years ago
|
$order->setAttributes(['customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0], false);
|
||
12 years ago
|
$order->save(false);
|
||
|
$order = new Order();
|
||
11 years ago
|
$order->setAttributes(['customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0], false);
|
||
12 years ago
|
$order->save(false);
|
||
|
$order = new Order();
|
||
11 years ago
|
$order->setAttributes(['customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0], false);
|
||
12 years ago
|
$order->save(false);
|
||
|
|
||
|
$orderItem = new OrderItem();
|
||
11 years ago
|
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false);
|
||
12 years ago
|
$orderItem->save(false);
|
||
|
$orderItem = new OrderItem();
|
||
11 years ago
|
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0], false);
|
||
12 years ago
|
$orderItem->save(false);
|
||
|
$orderItem = new OrderItem();
|
||
11 years ago
|
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0], false);
|
||
12 years ago
|
$orderItem->save(false);
|
||
|
$orderItem = new OrderItem();
|
||
11 years ago
|
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0], false);
|
||
12 years ago
|
$orderItem->save(false);
|
||
|
$orderItem = new OrderItem();
|
||
11 years ago
|
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0], false);
|
||
12 years ago
|
$orderItem->save(false);
|
||
|
$orderItem = new OrderItem();
|
||
11 years ago
|
$orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false);
|
||
12 years ago
|
$orderItem->save(false);
|
||
|
}
|
||
|
|
||
11 years ago
|
public function testFindNullValues()
|
||
12 years ago
|
{
|
||
11 years ago
|
// https://github.com/yiisoft/yii2/issues/1311
|
||
|
$this->markTestSkipped('Redis does not store/find null values correctly.');
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public function testBooleanAttribute()
|
||
|
{
|
||
|
// https://github.com/yiisoft/yii2/issues/1311
|
||
|
$this->markTestSkipped('Redis does not store/find boolean values correctly.');
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public function testFindEagerViaRelationPreserveOrder()
|
||
|
{
|
||
|
$this->markTestSkipped('Redis does not support orderBy.');
|
||
|
}
|
||
|
|
||
|
public function testFindEagerViaRelationPreserveOrderB()
|
||
|
{
|
||
|
$this->markTestSkipped('Redis does not support orderBy.');
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public function testSatisticalFind()
|
||
|
{
|
||
12 years ago
|
// find count, sum, average, min, max, scalar
|
||
11 years ago
|
$this->assertEquals(3, Customer::find()->count());
|
||
11 years ago
|
$this->assertEquals(6, Customer::find()->sum('id'));
|
||
12 years ago
|
$this->assertEquals(2, Customer::find()->average('id'));
|
||
|
$this->assertEquals(1, Customer::find()->min('id'));
|
||
|
$this->assertEquals(3, Customer::find()->max('id'));
|
||
|
|
||
11 years ago
|
$this->assertEquals(6, OrderItem::find()->count());
|
||
|
$this->assertEquals(7, OrderItem::find()->sum('quantity'));
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public function testfindIndexBy()
|
||
|
{
|
||
|
$customerClass = $this->getCustomerClass();
|
||
|
/** @var TestCase|ActiveRecordTestTrait $this */
|
||
12 years ago
|
// indexBy
|
||
11 years ago
|
$customers = $this->callCustomerFind()->indexBy('name')/*->orderBy('id')*/->all();
|
||
12 years ago
|
$this->assertEquals(3, count($customers));
|
||
11 years ago
|
$this->assertTrue($customers['user1'] instanceof $customerClass);
|
||
|
$this->assertTrue($customers['user2'] instanceof $customerClass);
|
||
|
$this->assertTrue($customers['user3'] instanceof $customerClass);
|
||
11 years ago
|
|
||
|
// indexBy callable
|
||
11 years ago
|
$customers = $this->callCustomerFind()->indexBy(function ($customer) {
|
||
11 years ago
|
return $customer->id . '-' . $customer->name;
|
||
11 years ago
|
})/*->orderBy('id')*/->all(); // TODO this test is duplicated because of missing orderBy support in redis
|
||
11 years ago
|
$this->assertEquals(3, count($customers));
|
||
11 years ago
|
$this->assertTrue($customers['1-user1'] instanceof $customerClass);
|
||
|
$this->assertTrue($customers['2-user2'] instanceof $customerClass);
|
||
|
$this->assertTrue($customers['3-user3'] instanceof $customerClass);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public function testFindEagerViaRelation()
|
||
11 years ago
|
{
|
||
11 years ago
|
/** @var TestCase|ActiveRecordTestTrait $this */
|
||
|
$orders = $this->callOrderFind()->with('items')/*->orderBy('id')*/->all(); // TODO this test is duplicated because of missing orderBy support in redis
|
||
|
$this->assertEquals(3, count($orders));
|
||
|
$order = $orders[0];
|
||
|
$this->assertEquals(1, $order->id);
|
||
|
$this->assertEquals(2, count($order->items));
|
||
|
$this->assertEquals(1, $order->items[0]->id);
|
||
|
$this->assertEquals(2, $order->items[1]->id);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
public function testFindCount()
|
||
|
{
|
||
|
$this->assertEquals(3, Customer::find()->count());
|
||
|
$this->assertEquals(1, Customer::find()->limit(1)->count());
|
||
|
$this->assertEquals(2, Customer::find()->limit(2)->count());
|
||
|
$this->assertEquals(1, Customer::find()->offset(2)->limit(2)->count());
|
||
|
}
|
||
|
|
||
11 years ago
|
public function testFindColumn()
|
||
|
{
|
||
11 years ago
|
$this->assertEquals(['user1', 'user2', 'user3'], Customer::find()->column('name'));
|
||
|
// TODO $this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->column('name'));
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// TODO test serial column incr
|
||
|
|
||
11 years ago
|
public function testUpdatePk()
|
||
|
{
|
||
|
// updateCounters
|
||
11 years ago
|
$pk = ['order_id' => 2, 'item_id' => 4];
|
||
11 years ago
|
$orderItem = OrderItem::find($pk);
|
||
|
$this->assertEquals(2, $orderItem->order_id);
|
||
|
$this->assertEquals(4, $orderItem->item_id);
|
||
|
|
||
|
$orderItem->order_id = 2;
|
||
|
$orderItem->item_id = 10;
|
||
|
$orderItem->save();
|
||
|
|
||
|
$this->assertNull(OrderItem::find($pk));
|
||
11 years ago
|
$this->assertNotNull(OrderItem::find(['order_id' => 2, 'item_id' => 10]));
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|