ActiveRecordTest.php 8.76 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\redis;
4 5 6 7 8 9

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;
10
use yiiunit\framework\ar\ActiveRecordTestTrait;
11

12 13 14
/**
 * @group redis
 */
15 16
class ActiveRecordTest extends RedisTestCase
{
17 18 19 20 21 22 23 24 25 26 27 28 29
	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(); }


30 31
	public function setUp()
	{
Carsten Brandt committed
32
		parent::setUp();
33 34 35
		ActiveRecord::$db = $this->getConnection();

		$customer = new Customer();
Carsten Brandt committed
36
		$customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1, 'profile_id' => 1], false);
37 38
		$customer->save(false);
		$customer = new Customer();
Carsten Brandt committed
39
		$customer->setAttributes(['email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1, 'profile_id' => null], false);
40 41
		$customer->save(false);
		$customer = new Customer();
Carsten Brandt committed
42
		$customer->setAttributes(['email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2, 'profile_id' => 2], false);
43 44 45 46 47 48
		$customer->save(false);

//		INSERT INTO tbl_category (name) VALUES ('Books');
//		INSERT INTO tbl_category (name) VALUES ('Movies');

		$item = new Item();
49
		$item->setAttributes(['name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1], false);
50 51
		$item->save(false);
		$item = new Item();
52
		$item->setAttributes(['name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1], false);
53 54
		$item->save(false);
		$item = new Item();
55
		$item->setAttributes(['name' => 'Ice Age', 'category_id' => 2], false);
56 57
		$item->save(false);
		$item = new Item();
58
		$item->setAttributes(['name' => 'Toy Story', 'category_id' => 2], false);
59 60
		$item->save(false);
		$item = new Item();
61
		$item->setAttributes(['name' => 'Cars', 'category_id' => 2], false);
62 63 64
		$item->save(false);

		$order = new Order();
Alexander Kochetov committed
65
		$order->setAttributes(['customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false);
66 67
		$order->save(false);
		$order = new Order();
Alexander Kochetov committed
68
		$order->setAttributes(['customer_id' => 2, 'created_at' => 1325334482, 'total' => 33.0], false);
69 70
		$order->save(false);
		$order = new Order();
Alexander Kochetov committed
71
		$order->setAttributes(['customer_id' => 2, 'created_at' => 1325502201, 'total' => 40.0], false);
72 73 74
		$order->save(false);

		$orderItem = new OrderItem();
75
		$orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false);
76 77
		$orderItem->save(false);
		$orderItem = new OrderItem();
78
		$orderItem->setAttributes(['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0], false);
79 80
		$orderItem->save(false);
		$orderItem = new OrderItem();
81
		$orderItem->setAttributes(['order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0], false);
82 83
		$orderItem->save(false);
		$orderItem = new OrderItem();
84
		$orderItem->setAttributes(['order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0], false);
85 86
		$orderItem->save(false);
		$orderItem = new OrderItem();
87
		$orderItem->setAttributes(['order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0], false);
88 89
		$orderItem->save(false);
		$orderItem = new OrderItem();
90
		$orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false);
91 92 93
		$orderItem->save(false);
	}

94
	public function testFindNullValues()
95
	{
96 97 98
		// https://github.com/yiisoft/yii2/issues/1311
		$this->markTestSkipped('Redis does not store/find null values correctly.');
	}
99

100 101 102 103 104
	public function testBooleanAttribute()
	{
		// https://github.com/yiisoft/yii2/issues/1311
		$this->markTestSkipped('Redis does not store/find boolean values correctly.');
	}
105

106 107 108 109 110 111 112 113 114
	public function testFindEagerViaRelationPreserveOrder()
	{
		$this->markTestSkipped('Redis does not support orderBy.');
	}

	public function testFindEagerViaRelationPreserveOrderB()
	{
		$this->markTestSkipped('Redis does not support orderBy.');
	}
115

116 117
	public function testSatisticalFind()
	{
118
		// find count, sum, average, min, max, scalar
119
		$this->assertEquals(3, Customer::find()->count());
120
		$this->assertEquals(6, Customer::find()->sum('id'));
121 122 123 124
		$this->assertEquals(2, Customer::find()->average('id'));
		$this->assertEquals(1, Customer::find()->min('id'));
		$this->assertEquals(3, Customer::find()->max('id'));

125 126 127
		$this->assertEquals(6, OrderItem::find()->count());
		$this->assertEquals(7, OrderItem::find()->sum('quantity'));
	}
128

129 130 131 132
	public function testfindIndexBy()
	{
		$customerClass = $this->getCustomerClass();
		/** @var TestCase|ActiveRecordTestTrait $this */
133
		// indexBy
134
		$customers = $this->callCustomerFind()->indexBy('name')/*->orderBy('id')*/->all();
135
		$this->assertEquals(3, count($customers));
136 137 138
		$this->assertTrue($customers['user1'] instanceof $customerClass);
		$this->assertTrue($customers['user2'] instanceof $customerClass);
		$this->assertTrue($customers['user3'] instanceof $customerClass);
139 140

		// indexBy callable
141
		$customers = $this->callCustomerFind()->indexBy(function ($customer) {
142
			return $customer->id . '-' . $customer->name;
143
		})/*->orderBy('id')*/->all(); // TODO this test is duplicated because of missing orderBy support in redis
144
		$this->assertEquals(3, count($customers));
145 146 147
		$this->assertTrue($customers['1-user1'] instanceof $customerClass);
		$this->assertTrue($customers['2-user2'] instanceof $customerClass);
		$this->assertTrue($customers['3-user3'] instanceof $customerClass);
148 149
	}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	public function testFindLimit()
	{
		// TODO this test is duplicated because of missing orderBy support in redis
		/** @var TestCase|ActiveRecordTestTrait $this */
		// all()
		$customers = $this->callCustomerFind()->all();
		$this->assertEquals(3, count($customers));

		$customers = $this->callCustomerFind()/*->orderBy('id')*/->limit(1)->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals('user1', $customers[0]->name);

		$customers = $this->callCustomerFind()/*->orderBy('id')*/->limit(1)->offset(1)->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals('user2', $customers[0]->name);

		$customers = $this->callCustomerFind()/*->orderBy('id')*/->limit(1)->offset(2)->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals('user3', $customers[0]->name);

		$customers = $this->callCustomerFind()/*->orderBy('id')*/->limit(2)->offset(1)->all();
		$this->assertEquals(2, count($customers));
		$this->assertEquals('user2', $customers[0]->name);
		$this->assertEquals('user3', $customers[1]->name);

		$customers = $this->callCustomerFind()->limit(2)->offset(3)->all();
		$this->assertEquals(0, count($customers));

		// one()
		$customer = $this->callCustomerFind()/*->orderBy('id')*/->one();
		$this->assertEquals('user1', $customer->name);

		$customer = $this->callCustomerFind()/*->orderBy('id')*/->offset(0)->one();
		$this->assertEquals('user1', $customer->name);

		$customer = $this->callCustomerFind()/*->orderBy('id')*/->offset(1)->one();
		$this->assertEquals('user2', $customer->name);

		$customer = $this->callCustomerFind()/*->orderBy('id')*/->offset(2)->one();
		$this->assertEquals('user3', $customer->name);

		$customer = $this->callCustomerFind()->offset(3)->one();
		$this->assertNull($customer);
	}

195
	public function testFindEagerViaRelation()
196
	{
197 198 199 200 201 202 203 204
		/** @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);
205 206
	}

207 208
	public function testFindColumn()
	{
209 210
		$this->assertEquals(['user1', 'user2', 'user3'], Customer::find()->column('name'));
//		TODO $this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->column('name'));
211 212
	}

Carsten Brandt committed
213 214
	// TODO test serial column incr

215 216 217
	public function testUpdatePk()
	{
		// updateCounters
218
		$pk = ['order_id' => 2, 'item_id' => 4];
219 220 221 222 223 224 225 226 227
		$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));
228
		$this->assertNotNull(OrderItem::find(['order_id' => 2, 'item_id' => 10]));
229
	}
Alexander Kochetov committed
230
}