1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
namespace yiiunit\extensions\redis;
use yii\redis\ActiveQuery;
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;
use yiiunit\data\ar\redis\OrderItemWithNullFK;
use yiiunit\data\ar\redis\OrderWithNullFK;
use yiiunit\framework\ar\ActiveRecordTestTrait;
/**
* @group redis
*/
class ActiveRecordTest extends RedisTestCase
{
use ActiveRecordTestTrait;
public function getCustomerClass()
{
return Customer::className();
}
public function getItemClass()
{
return Item::className();
}
public function getOrderClass()
{
return Order::className();
}
public function getOrderItemClass()
{
return OrderItem::className();
}
public function getOrderWithNullFKClass()
{
return OrderWithNullFK::className();
}
public function getOrderItemWithNullFKmClass()
{
return OrderItemWithNullFK::className();
}
public function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->getConnection();
$customer = new Customer();
$customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1, 'profile_id' => 1], false);
$customer->save(false);
$customer = new Customer();
$customer->setAttributes(['email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1, 'profile_id' => null], false);
$customer->save(false);
$customer = new Customer();
$customer->setAttributes(['email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2, 'profile_id' => 2], false);
$customer->save(false);
// INSERT INTO category (name) VALUES ('Books');
// INSERT INTO category (name) VALUES ('Movies');
$item = new Item();
$item->setAttributes(['name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1], false);
$item->save(false);
$item = new Item();
$item->setAttributes(['name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1], false);
$item->save(false);
$item = new Item();
$item->setAttributes(['name' => 'Ice Age', 'category_id' => 2], false);
$item->save(false);
$item = new Item();
$item->setAttributes(['name' => 'Toy Story', 'category_id' => 2], false);
$item->save(false);
$item = new Item();
$item->setAttributes(['name' => 'Cars', 'category_id' => 2], false);
$item->save(false);
$order = new Order();
$order->setAttributes(['customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false);
$order->save(false);
$order = new Order();
$order->setAttributes(['customer_id' => 2, 'created_at' => 1325334482, 'total' => 33.0], false);
$order->save(false);
$order = new Order();
$order->setAttributes(['customer_id' => 2, 'created_at' => 1325502201, 'total' => 40.0], false);
$order->save(false);
$orderItem = new OrderItem();
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false);
$orderItem->save(false);
$orderItem = new OrderItem();
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0], false);
$orderItem->save(false);
$orderItem = new OrderItem();
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0], false);
$orderItem->save(false);
$orderItem = new OrderItem();
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0], false);
$orderItem->save(false);
$orderItem = new OrderItem();
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0], false);
$orderItem->save(false);
$orderItem = new OrderItem();
$orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false);
$orderItem->save(false);
$order = new OrderWithNullFK();
$order->setAttributes(['customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false);
$order->save(false);
$order = new OrderWithNullFK();
$order->setAttributes(['customer_id' => 2, 'created_at' => 1325334482, 'total' => 33.0], false);
$order->save(false);
$order = new OrderWithNullFK();
$order->setAttributes(['customer_id' => 2, 'created_at' => 1325502201, 'total' => 40.0], false);
$order->save(false);
$orderItem = new OrderItemWithNullFK();
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false);
$orderItem->save(false);
$orderItem = new OrderItemWithNullFK();
$orderItem->setAttributes(['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0], false);
$orderItem->save(false);
$orderItem = new OrderItemWithNullFK();
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0], false);
$orderItem->save(false);
$orderItem = new OrderItemWithNullFK();
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0], false);
$orderItem->save(false);
$orderItem = new OrderItemWithNullFK();
$orderItem->setAttributes(['order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0], false);
$orderItem->save(false);
$orderItem = new OrderItemWithNullFK();
$orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false);
$orderItem->save(false);
}
public function testFindNullValues()
{
// https://github.com/yiisoft/yii2/issues/1311
$this->markTestSkipped('Redis does not store/find null values correctly.');
}
public function testUnlinkAll()
{
// https://github.com/yiisoft/yii2/issues/1311
$this->markTestSkipped('Redis does not store/find null values correctly.');
}
public function testUnlink()
{
// https://github.com/yiisoft/yii2/issues/1311
$this->markTestSkipped('Redis does not store/find null values correctly.');
}
public function testBooleanAttribute()
{
// https://github.com/yiisoft/yii2/issues/1311
$this->markTestSkipped('Redis does not store/find boolean values correctly.');
}
public function testFindEagerViaRelationPreserveOrder()
{
$this->markTestSkipped('Redis does not support orderBy.');
}
public function testFindEagerViaRelationPreserveOrderB()
{
$this->markTestSkipped('Redis does not support orderBy.');
}
public function testStatisticalFind()
{
// find count, sum, average, min, max, scalar
$this->assertEquals(3, Customer::find()->count());
$this->assertEquals(6, Customer::find()->sum('id'));
$this->assertEquals(2, Customer::find()->average('id'));
$this->assertEquals(1, Customer::find()->min('id'));
$this->assertEquals(3, Customer::find()->max('id'));
$this->assertEquals(6, OrderItem::find()->count());
$this->assertEquals(7, OrderItem::find()->sum('quantity'));
}
public function testFindIndexBy()
{
$customerClass = $this->getCustomerClass();
/* @var $this TestCase|ActiveRecordTestTrait */
// indexBy
$customers = Customer::find()->indexBy('name')/*->orderBy('id')*/->all();
$this->assertEquals(3, count($customers));
$this->assertTrue($customers['user1'] instanceof $customerClass);
$this->assertTrue($customers['user2'] instanceof $customerClass);
$this->assertTrue($customers['user3'] instanceof $customerClass);
// indexBy callable
$customers = Customer::find()->indexBy(function ($customer) {
return $customer->id . '-' . $customer->name;
})/*->orderBy('id')*/->all(); // TODO this test is duplicated because of missing orderBy support in redis
$this->assertEquals(3, count($customers));
$this->assertTrue($customers['1-user1'] instanceof $customerClass);
$this->assertTrue($customers['2-user2'] instanceof $customerClass);
$this->assertTrue($customers['3-user3'] instanceof $customerClass);
}
public function testFindLimit()
{
// TODO this test is duplicated because of missing orderBy support in redis
/* @var $this TestCase|ActiveRecordTestTrait */
// all()
$customers = Customer::find()->all();
$this->assertEquals(3, count($customers));
$customers = Customer::find()/*->orderBy('id')*/->limit(1)->all();
$this->assertEquals(1, count($customers));
$this->assertEquals('user1', $customers[0]->name);
$customers = Customer::find()/*->orderBy('id')*/->limit(1)->offset(1)->all();
$this->assertEquals(1, count($customers));
$this->assertEquals('user2', $customers[0]->name);
$customers = Customer::find()/*->orderBy('id')*/->limit(1)->offset(2)->all();
$this->assertEquals(1, count($customers));
$this->assertEquals('user3', $customers[0]->name);
$customers = Customer::find()/*->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 = Customer::find()->limit(2)->offset(3)->all();
$this->assertEquals(0, count($customers));
// one()
$customer = Customer::find()/*->orderBy('id')*/->one();
$this->assertEquals('user1', $customer->name);
$customer = Customer::find()/*->orderBy('id')*/->offset(0)->one();
$this->assertEquals('user1', $customer->name);
$customer = Customer::find()/*->orderBy('id')*/->offset(1)->one();
$this->assertEquals('user2', $customer->name);
$customer = Customer::find()/*->orderBy('id')*/->offset(2)->one();
$this->assertEquals('user3', $customer->name);
$customer = Customer::find()->offset(3)->one();
$this->assertNull($customer);
}
public function testFindEagerViaRelation()
{
/* @var $orderClass \yii\db\ActiveRecordInterface */
$orderClass = $this->getOrderClass();
/* @var $this TestCase|ActiveRecordTestTrait */
$orders = $orderClass::find()->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);
}
public function testFindColumn()
{
$this->assertEquals(['user1', 'user2', 'user3'], Customer::find()->column('name'));
// TODO $this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->column('name'));
}
// TODO test serial column incr
public function testUpdatePk()
{
// updateCounters
$pk = ['order_id' => 2, 'item_id' => 4];
$orderItem = OrderItem::findOne($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::findOne($pk));
$this->assertNotNull(OrderItem::findOne(['order_id' => 2, 'item_id' => 10]));
}
public function testFilterWhere()
{
// should work with hash format
$query = new ActiveQuery('dummy');
$query->filterWhere([
'id' => 0,
'title' => ' ',
'author_ids' => [],
]);
$this->assertEquals(['id' => 0], $query->where);
$query->andFilterWhere(['status' => null]);
$this->assertEquals(['id' => 0], $query->where);
$query->orFilterWhere(['name' => '']);
$this->assertEquals(['id' => 0], $query->where);
// should work with operator format
$query = new ActiveQuery('dummy');
$condition = ['like', 'name', 'Alex'];
$query->filterWhere($condition);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['between', 'id', null, null]);
$this->assertEquals($condition, $query->where);
$query->orFilterWhere(['not between', 'id', null, null]);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['in', 'id', []]);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['not in', 'id', []]);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['not in', 'id', []]);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['like', 'id', '']);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['or like', 'id', '']);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['not like', 'id', ' ']);
$this->assertEquals($condition, $query->where);
$query->andFilterWhere(['or not like', 'id', null]);
$this->assertEquals($condition, $query->where);
}
public function testFilterWhereRecursively()
{
$query = new ActiveQuery('dummy');
$query->filterWhere(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
$this->assertEquals(['and', ['id' => 1]], $query->where);
}
}