ActiveRecordTestTrait.php 47.1 KB
Newer Older
1 2
<?php
/**
3 4
 *
 *
5 6 7 8 9
 * @author Carsten Brandt <mail@cebe.cc>
 */

namespace yiiunit\framework\ar;

10
use yii\base\Event;
11
use yii\db\ActiveQueryInterface;
12
use yii\db\BaseActiveRecord;
13 14 15 16 17
use yiiunit\TestCase;
use yiiunit\data\ar\Customer;
use yiiunit\data\ar\Order;

/**
Qiang Xue committed
18
 * This trait provides unit tests shared by the different AR implementations
19 20 21
 */
trait ActiveRecordTestTrait
{
22
    /* @var $this TestCase */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    /**
     * This method should return the classname of Customer class
     * @return string
     */
    abstract public function getCustomerClass();

    /**
     * This method should return the classname of Order class
     * @return string
     */
    abstract public function getOrderClass();

    /**
     * This method should return the classname of OrderItem class
     * @return string
     */
    abstract public function getOrderItemClass();

    /**
     * This method should return the classname of Item class
     * @return string
     */
    abstract public function getItemClass();

47 48 49 50
    abstract public function getOrderWithNullFKClass();

    abstract public function getOrderItemWithNullFKmClass();

51 52 53 54 55 56 57 58 59
    /**
     * can be overridden to do things after save()
     */
    public function afterSave()
    {
    }

    public function testFind()
    {
60
        /* @var $customerClass \yii\db\ActiveRecordInterface */
61
        $customerClass = $this->getCustomerClass();
62
        /* @var $this TestCase|ActiveRecordTestTrait */
63
        // find one
Qiang Xue committed
64
        $result = $customerClass::find();
65 66 67 68 69
        $this->assertTrue($result instanceof ActiveQueryInterface);
        $customer = $result->one();
        $this->assertTrue($customer instanceof $customerClass);

        // find all
Qiang Xue committed
70
        $customers = $customerClass::find()->all();
71 72 73 74 75 76
        $this->assertEquals(3, count($customers));
        $this->assertTrue($customers[0] instanceof $customerClass);
        $this->assertTrue($customers[1] instanceof $customerClass);
        $this->assertTrue($customers[2] instanceof $customerClass);

        // find by a single primary key
Alexander Makarov committed
77
        $customer = $customerClass::findOne(2);
78 79
        $this->assertTrue($customer instanceof $customerClass);
        $this->assertEquals('user2', $customer->name);
Alexander Makarov committed
80
        $customer = $customerClass::findOne(5);
81
        $this->assertNull($customer);
Alexander Makarov committed
82
        $customer = $customerClass::findOne(['id' => [5, 6, 1]]);
83
        $this->assertEquals(1, count($customer));
Qiang Xue committed
84
        $customer = $customerClass::find()->where(['id' => [5, 6, 1]])->one();
85 86 87
        $this->assertNotNull($customer);

        // find by column values
Alexander Makarov committed
88
        $customer = $customerClass::findOne(['id' => 2, 'name' => 'user2']);
89 90
        $this->assertTrue($customer instanceof $customerClass);
        $this->assertEquals('user2', $customer->name);
Alexander Makarov committed
91
        $customer = $customerClass::findOne(['id' => 2, 'name' => 'user1']);
92
        $this->assertNull($customer);
Alexander Makarov committed
93
        $customer = $customerClass::findOne(['id' => 5]);
94
        $this->assertNull($customer);
Alexander Makarov committed
95
        $customer = $customerClass::findOne(['name' => 'user5']);
96 97 98
        $this->assertNull($customer);

        // find by attributes
Qiang Xue committed
99
        $customer = $customerClass::find()->where(['name' => 'user2'])->one();
100 101 102 103
        $this->assertTrue($customer instanceof $customerClass);
        $this->assertEquals(2, $customer->id);

        // scope
Qiang Xue committed
104 105
        $this->assertEquals(2, count($customerClass::find()->active()->all()));
        $this->assertEquals(2, $customerClass::find()->active()->count());
106 107 108 109
    }

    public function testFindAsArray()
    {
110
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
111 112
        $customerClass = $this->getCustomerClass();

113
        // asArray
Qiang Xue committed
114
        $customer = $customerClass::find()->where(['id' => 2])->asArray()->one();
115 116 117 118 119 120 121 122
        $this->assertEquals([
            'id' => 2,
            'email' => 'user2@example.com',
            'name' => 'user2',
            'address' => 'address2',
            'status' => 1,
            'profile_id' => null,
        ], $customer);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

        // find all asArray
        $customers = $customerClass::find()->asArray()->all();
        $this->assertEquals(3, count($customers));
        $this->assertArrayHasKey('id', $customers[0]);
        $this->assertArrayHasKey('name', $customers[0]);
        $this->assertArrayHasKey('email', $customers[0]);
        $this->assertArrayHasKey('address', $customers[0]);
        $this->assertArrayHasKey('status', $customers[0]);
        $this->assertArrayHasKey('id', $customers[1]);
        $this->assertArrayHasKey('name', $customers[1]);
        $this->assertArrayHasKey('email', $customers[1]);
        $this->assertArrayHasKey('address', $customers[1]);
        $this->assertArrayHasKey('status', $customers[1]);
        $this->assertArrayHasKey('id', $customers[2]);
        $this->assertArrayHasKey('name', $customers[2]);
        $this->assertArrayHasKey('email', $customers[2]);
        $this->assertArrayHasKey('address', $customers[2]);
        $this->assertArrayHasKey('status', $customers[2]);
142 143 144 145
    }

    public function testFindScalar()
    {
146
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
147 148
        $customerClass = $this->getCustomerClass();

149
        /* @var $this TestCase|ActiveRecordTestTrait */
150
        // query scalar
Qiang Xue committed
151
        $customerName = $customerClass::find()->where(['id' => 2])->scalar('name');
152
        $this->assertEquals('user2', $customerName);
Qiang Xue committed
153
        $customerName = $customerClass::find()->where(['status' => 2])->scalar('name');
154
        $this->assertEquals('user3', $customerName);
Qiang Xue committed
155
        $customerName = $customerClass::find()->where(['status' => 2])->scalar('noname');
156
        $this->assertNull($customerName);
Qiang Xue committed
157
        $customerId = $customerClass::find()->where(['status' => 2])->scalar('id');
158 159 160 161 162
        $this->assertEquals(3, $customerId);
    }

    public function testFindColumn()
    {
163
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
164 165
        $customerClass = $this->getCustomerClass();

166
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
167 168
        $this->assertEquals(['user1', 'user2', 'user3'], $customerClass::find()->orderBy(['name' => SORT_ASC])->column('name'));
        $this->assertEquals(['user3', 'user2', 'user1'], $customerClass::find()->orderBy(['name' => SORT_DESC])->column('name'));
169 170 171 172
    }

    public function testFindIndexBy()
    {
173
        /* @var $customerClass \yii\db\ActiveRecordInterface */
174
        $customerClass = $this->getCustomerClass();
175
        /* @var $this TestCase|ActiveRecordTestTrait */
176
        // indexBy
Qiang Xue committed
177
        $customers = $customerClass::find()->indexBy('name')->orderBy('id')->all();
178 179 180 181 182 183
        $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
Qiang Xue committed
184
        $customers = $customerClass::find()->indexBy(function ($customer) {
185 186 187 188 189 190 191 192 193 194
            return $customer->id . '-' . $customer->name;
        })->orderBy('id')->all();
        $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 testFindIndexByAsArray()
    {
195
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
196 197
        $customerClass = $this->getCustomerClass();

198
        /* @var $this TestCase|ActiveRecordTestTrait */
199
        // indexBy + asArray
Qiang Xue committed
200
        $customers = $customerClass::find()->asArray()->indexBy('name')->all();
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        $this->assertEquals(3, count($customers));
        $this->assertArrayHasKey('id', $customers['user1']);
        $this->assertArrayHasKey('name', $customers['user1']);
        $this->assertArrayHasKey('email', $customers['user1']);
        $this->assertArrayHasKey('address', $customers['user1']);
        $this->assertArrayHasKey('status', $customers['user1']);
        $this->assertArrayHasKey('id', $customers['user2']);
        $this->assertArrayHasKey('name', $customers['user2']);
        $this->assertArrayHasKey('email', $customers['user2']);
        $this->assertArrayHasKey('address', $customers['user2']);
        $this->assertArrayHasKey('status', $customers['user2']);
        $this->assertArrayHasKey('id', $customers['user3']);
        $this->assertArrayHasKey('name', $customers['user3']);
        $this->assertArrayHasKey('email', $customers['user3']);
        $this->assertArrayHasKey('address', $customers['user3']);
        $this->assertArrayHasKey('status', $customers['user3']);

        // indexBy callable + asArray
Qiang Xue committed
219
        $customers = $customerClass::find()->indexBy(function ($customer) {
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            return $customer['id'] . '-' . $customer['name'];
        })->asArray()->all();
        $this->assertEquals(3, count($customers));
        $this->assertArrayHasKey('id', $customers['1-user1']);
        $this->assertArrayHasKey('name', $customers['1-user1']);
        $this->assertArrayHasKey('email', $customers['1-user1']);
        $this->assertArrayHasKey('address', $customers['1-user1']);
        $this->assertArrayHasKey('status', $customers['1-user1']);
        $this->assertArrayHasKey('id', $customers['2-user2']);
        $this->assertArrayHasKey('name', $customers['2-user2']);
        $this->assertArrayHasKey('email', $customers['2-user2']);
        $this->assertArrayHasKey('address', $customers['2-user2']);
        $this->assertArrayHasKey('status', $customers['2-user2']);
        $this->assertArrayHasKey('id', $customers['3-user3']);
        $this->assertArrayHasKey('name', $customers['3-user3']);
        $this->assertArrayHasKey('email', $customers['3-user3']);
        $this->assertArrayHasKey('address', $customers['3-user3']);
        $this->assertArrayHasKey('status', $customers['3-user3']);
    }

    public function testRefresh()
    {
242
        /* @var $customerClass \yii\db\ActiveRecordInterface */
243
        $customerClass = $this->getCustomerClass();
244
        /* @var $this TestCase|ActiveRecordTestTrait */
245 246 247
        $customer = new $customerClass();
        $this->assertFalse($customer->refresh());

Alexander Makarov committed
248
        $customer = $customerClass::findOne(1);
249 250 251 252 253 254 255
        $customer->name = 'to be refreshed';
        $this->assertTrue($customer->refresh());
        $this->assertEquals('user1', $customer->name);
    }

    public function testEquals()
    {
256
        /* @var $customerClass \yii\db\ActiveRecordInterface */
257
        $customerClass = $this->getCustomerClass();
258
        /* @var $itemClass \yii\db\ActiveRecordInterface */
259 260
        $itemClass = $this->getItemClass();

261
        /* @var $this TestCase|ActiveRecordTestTrait */
262 263 264 265 266 267 268 269
        $customerA = new $customerClass();
        $customerB = new $customerClass();
        $this->assertFalse($customerA->equals($customerB));

        $customerA = new $customerClass();
        $customerB = new $itemClass();
        $this->assertFalse($customerA->equals($customerB));

Alexander Makarov committed
270 271
        $customerA = $customerClass::findOne(1);
        $customerB = $customerClass::findOne(2);
272 273
        $this->assertFalse($customerA->equals($customerB));

Alexander Makarov committed
274
        $customerB = $customerClass::findOne(1);
275 276
        $this->assertTrue($customerA->equals($customerB));

Alexander Makarov committed
277 278
        $customerA = $customerClass::findOne(1);
        $customerB = $itemClass::findOne(1);
279 280 281 282 283
        $this->assertFalse($customerA->equals($customerB));
    }

    public function testFindCount()
    {
284
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
285 286
        $customerClass = $this->getCustomerClass();

287
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
288
        $this->assertEquals(3, $customerClass::find()->count());
289

Qiang Xue committed
290 291 292 293
        $this->assertEquals(1, $customerClass::find()->where(['id' => 1])->count());
        $this->assertEquals(2, $customerClass::find()->where(['id' => [1, 2]])->count());
        $this->assertEquals(2, $customerClass::find()->where(['id' => [1, 2]])->offset(1)->count());
        $this->assertEquals(2, $customerClass::find()->where(['id' => [1, 2]])->offset(2)->count());
294 295

        // limit should have no effect on count()
Qiang Xue committed
296 297 298 299
        $this->assertEquals(3, $customerClass::find()->limit(1)->count());
        $this->assertEquals(3, $customerClass::find()->limit(2)->count());
        $this->assertEquals(3, $customerClass::find()->limit(10)->count());
        $this->assertEquals(3, $customerClass::find()->offset(2)->limit(2)->count());
300 301 302 303
    }

    public function testFindLimit()
    {
304
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
305 306
        $customerClass = $this->getCustomerClass();

307
        /* @var $this TestCase|ActiveRecordTestTrait */
308
        // all()
Qiang Xue committed
309
        $customers = $customerClass::find()->all();
310 311
        $this->assertEquals(3, count($customers));

Qiang Xue committed
312
        $customers = $customerClass::find()->orderBy('id')->limit(1)->all();
313 314 315
        $this->assertEquals(1, count($customers));
        $this->assertEquals('user1', $customers[0]->name);

Qiang Xue committed
316
        $customers = $customerClass::find()->orderBy('id')->limit(1)->offset(1)->all();
317 318 319
        $this->assertEquals(1, count($customers));
        $this->assertEquals('user2', $customers[0]->name);

Qiang Xue committed
320
        $customers = $customerClass::find()->orderBy('id')->limit(1)->offset(2)->all();
321 322 323
        $this->assertEquals(1, count($customers));
        $this->assertEquals('user3', $customers[0]->name);

Qiang Xue committed
324
        $customers = $customerClass::find()->orderBy('id')->limit(2)->offset(1)->all();
325 326 327 328
        $this->assertEquals(2, count($customers));
        $this->assertEquals('user2', $customers[0]->name);
        $this->assertEquals('user3', $customers[1]->name);

Qiang Xue committed
329
        $customers = $customerClass::find()->limit(2)->offset(3)->all();
330 331 332
        $this->assertEquals(0, count($customers));

        // one()
Qiang Xue committed
333
        $customer = $customerClass::find()->orderBy('id')->one();
334 335
        $this->assertEquals('user1', $customer->name);

Qiang Xue committed
336
        $customer = $customerClass::find()->orderBy('id')->offset(0)->one();
337 338
        $this->assertEquals('user1', $customer->name);

Qiang Xue committed
339
        $customer = $customerClass::find()->orderBy('id')->offset(1)->one();
340 341
        $this->assertEquals('user2', $customer->name);

Qiang Xue committed
342
        $customer = $customerClass::find()->orderBy('id')->offset(2)->one();
343 344
        $this->assertEquals('user3', $customer->name);

Qiang Xue committed
345
        $customer = $customerClass::find()->offset(3)->one();
346 347 348 349 350 351
        $this->assertNull($customer);

    }

    public function testFindComplexCondition()
    {
352
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
353 354
        $customerClass = $this->getCustomerClass();

355
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
356 357
        $this->assertEquals(2, $customerClass::find()->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->count());
        $this->assertEquals(2, count($customerClass::find()->where(['OR', ['name' => 'user1'], ['name' => 'user2']])->all()));
358

Qiang Xue committed
359 360
        $this->assertEquals(2, $customerClass::find()->where(['name' => ['user1', 'user2']])->count());
        $this->assertEquals(2, count($customerClass::find()->where(['name' => ['user1', 'user2']])->all()));
361

Qiang Xue committed
362 363
        $this->assertEquals(1, $customerClass::find()->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->count());
        $this->assertEquals(1, count($customerClass::find()->where(['AND', ['name' => ['user2', 'user3']], ['BETWEEN', 'status', 2, 4]])->all()));
364 365 366 367
    }

    public function testFindNullValues()
    {
368
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
369 370
        $customerClass = $this->getCustomerClass();

371
        /* @var $this TestCase|ActiveRecordTestTrait */
Alexander Makarov committed
372
        $customer = $customerClass::findOne(2);
373 374 375 376
        $customer->name = null;
        $customer->save(false);
        $this->afterSave();

Qiang Xue committed
377
        $result = $customerClass::find()->where(['name' => null])->all();
378 379 380 381 382 383
        $this->assertEquals(1, count($result));
        $this->assertEquals(2, reset($result)->primaryKey);
    }

    public function testExists()
    {
384
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
385 386
        $customerClass = $this->getCustomerClass();

387
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
388 389 390 391 392 393 394 395
        $this->assertTrue($customerClass::find()->where(['id' => 2])->exists());
        $this->assertFalse($customerClass::find()->where(['id' => 5])->exists());
        $this->assertTrue($customerClass::find()->where(['name' => 'user1'])->exists());
        $this->assertFalse($customerClass::find()->where(['name' => 'user5'])->exists());

        $this->assertTrue($customerClass::find()->where(['id' => [2, 3]])->exists());
        $this->assertTrue($customerClass::find()->where(['id' => [2, 3]])->offset(1)->exists());
        $this->assertFalse($customerClass::find()->where(['id' => [2, 3]])->offset(2)->exists());
396 397 398 399
    }

    public function testFindLazy()
    {
400
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
401 402
        $customerClass = $this->getCustomerClass();

403
        /* @var $this TestCase|ActiveRecordTestTrait */
Alexander Makarov committed
404
        $customer = $customerClass::findOne(2);
405 406 407 408 409 410 411 412 413 414
        $this->assertFalse($customer->isRelationPopulated('orders'));
        $orders = $customer->orders;
        $this->assertTrue($customer->isRelationPopulated('orders'));
        $this->assertEquals(2, count($orders));
        $this->assertEquals(1, count($customer->relatedRecords));

        // unset
        unset($customer['orders']);
        $this->assertFalse($customer->isRelationPopulated('orders'));

415
        /* @var $customer Customer */
Alexander Makarov committed
416
        $customer = $customerClass::findOne(2);
417 418 419 420 421 422 423 424 425 426 427
        $this->assertFalse($customer->isRelationPopulated('orders'));
        $orders = $customer->getOrders()->where(['id' => 3])->all();
        $this->assertFalse($customer->isRelationPopulated('orders'));
        $this->assertEquals(0, count($customer->relatedRecords));

        $this->assertEquals(1, count($orders));
        $this->assertEquals(3, $orders[0]->id);
    }

    public function testFindEager()
    {
428
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
429
        $customerClass = $this->getCustomerClass();
430
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
431 432
        $orderClass = $this->getOrderClass();

433
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
434
        $customers = $customerClass::find()->with('orders')->indexBy('id')->all();
435 436 437 438 439 440 441 442 443 444 445 446
        ksort($customers);
        $this->assertEquals(3, count($customers));
        $this->assertTrue($customers[1]->isRelationPopulated('orders'));
        $this->assertTrue($customers[2]->isRelationPopulated('orders'));
        $this->assertTrue($customers[3]->isRelationPopulated('orders'));
        $this->assertEquals(1, count($customers[1]->orders));
        $this->assertEquals(2, count($customers[2]->orders));
        $this->assertEquals(0, count($customers[3]->orders));
        // unset
        unset($customers[1]->orders);
        $this->assertFalse($customers[1]->isRelationPopulated('orders'));

Qiang Xue committed
447
        $customer = $customerClass::find()->where(['id' => 1])->with('orders')->one();
448 449 450 451 452
        $this->assertTrue($customer->isRelationPopulated('orders'));
        $this->assertEquals(1, count($customer->orders));
        $this->assertEquals(1, count($customer->relatedRecords));

        // multiple with() calls
Qiang Xue committed
453
        $orders = $orderClass::find()->with('customer', 'items')->all();
454 455 456
        $this->assertEquals(3, count($orders));
        $this->assertTrue($orders[0]->isRelationPopulated('customer'));
        $this->assertTrue($orders[0]->isRelationPopulated('items'));
Qiang Xue committed
457
        $orders = $orderClass::find()->with('customer')->with('items')->all();
458 459 460 461 462 463 464
        $this->assertEquals(3, count($orders));
        $this->assertTrue($orders[0]->isRelationPopulated('customer'));
        $this->assertTrue($orders[0]->isRelationPopulated('items'));
    }

    public function testFindLazyVia()
    {
465
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
466 467
        $orderClass = $this->getOrderClass();

468 469
        /* @var $this TestCase|ActiveRecordTestTrait */
        /* @var $order Order */
Alexander Makarov committed
470
        $order = $orderClass::findOne(1);
471 472 473 474 475 476 477 478
        $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 testFindLazyVia2()
    {
479
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
480 481
        $orderClass = $this->getOrderClass();

482 483
        /* @var $this TestCase|ActiveRecordTestTrait */
        /* @var $order Order */
Alexander Makarov committed
484
        $order = $orderClass::findOne(1);
485 486 487 488 489 490
        $order->id = 100;
        $this->assertEquals([], $order->items);
    }

    public function testFindEagerViaRelation()
    {
491
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
492 493
        $orderClass = $this->getOrderClass();

494
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
495
        $orders = $orderClass::find()->with('items')->orderBy('id')->all();
496 497 498 499 500 501 502 503 504 505 506
        $this->assertEquals(3, count($orders));
        $order = $orders[0];
        $this->assertEquals(1, $order->id);
        $this->assertTrue($order->isRelationPopulated('items'));
        $this->assertEquals(2, count($order->items));
        $this->assertEquals(1, $order->items[0]->id);
        $this->assertEquals(2, $order->items[1]->id);
    }

    public function testFindNestedRelation()
    {
507
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
508 509
        $customerClass = $this->getCustomerClass();

510
        /* @var $this TestCase|ActiveRecordTestTrait */
Qiang Xue committed
511
        $customers = $customerClass::find()->with('orders', 'orders.items')->indexBy('id')->all();
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        ksort($customers);
        $this->assertEquals(3, count($customers));
        $this->assertTrue($customers[1]->isRelationPopulated('orders'));
        $this->assertTrue($customers[2]->isRelationPopulated('orders'));
        $this->assertTrue($customers[3]->isRelationPopulated('orders'));
        $this->assertEquals(1, count($customers[1]->orders));
        $this->assertEquals(2, count($customers[2]->orders));
        $this->assertEquals(0, count($customers[3]->orders));
        $this->assertTrue($customers[1]->orders[0]->isRelationPopulated('items'));
        $this->assertTrue($customers[2]->orders[0]->isRelationPopulated('items'));
        $this->assertTrue($customers[2]->orders[1]->isRelationPopulated('items'));
        $this->assertEquals(2, count($customers[1]->orders[0]->items));
        $this->assertEquals(3, count($customers[2]->orders[0]->items));
        $this->assertEquals(1, count($customers[2]->orders[1]->items));
    }

    /**
     * Ensure ActiveRelationTrait does preserve order of items on find via()
     * https://github.com/yiisoft/yii2/issues/1310
     */
    public function testFindEagerViaRelationPreserveOrder()
    {
534
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
535 536
        $orderClass = $this->getOrderClass();

537
        /* @var $this TestCase|ActiveRecordTestTrait */
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

        /*
        Item (name, category_id)
        Order (customer_id, created_at, total)
        OrderItem (order_id, item_id, quantity, subtotal)

        Result should be the following:

        Order 1: 1, 1325282384, 110.0
        - orderItems:
            OrderItem: 1, 1, 1, 30.0
            OrderItem: 1, 2, 2, 40.0
        - itemsInOrder:
            Item 1: 'Agile Web Application Development with Yii1.1 and PHP5', 1
            Item 2: 'Yii 1.1 Application Development Cookbook', 1

        Order 2: 2, 1325334482, 33.0
        - orderItems:
            OrderItem: 2, 3, 1, 8.0
            OrderItem: 2, 4, 1, 10.0
            OrderItem: 2, 5, 1, 15.0
        - itemsInOrder:
            Item 5: 'Cars', 2
            Item 3: 'Ice Age', 2
            Item 4: 'Toy Story', 2
        Order 3: 2, 1325502201, 40.0
        - orderItems:
            OrderItem: 3, 2, 1, 40.0
        - itemsInOrder:
            Item 3: 'Ice Age', 2
         */
Qiang Xue committed
569
        $orders = $orderClass::find()->with('itemsInOrder1')->orderBy('created_at')->all();
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
        $this->assertEquals(3, count($orders));

        $order = $orders[0];
        $this->assertEquals(1, $order->id);
        $this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
        $this->assertEquals(2, count($order->itemsInOrder1));
        $this->assertEquals(1, $order->itemsInOrder1[0]->id);
        $this->assertEquals(2, $order->itemsInOrder1[1]->id);

        $order = $orders[1];
        $this->assertEquals(2, $order->id);
        $this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
        $this->assertEquals(3, count($order->itemsInOrder1));
        $this->assertEquals(5, $order->itemsInOrder1[0]->id);
        $this->assertEquals(3, $order->itemsInOrder1[1]->id);
        $this->assertEquals(4, $order->itemsInOrder1[2]->id);

        $order = $orders[2];
        $this->assertEquals(3, $order->id);
        $this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
        $this->assertEquals(1, count($order->itemsInOrder1));
        $this->assertEquals(2, $order->itemsInOrder1[0]->id);
    }

    // different order in via table
    public function testFindEagerViaRelationPreserveOrderB()
    {
597
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
598 599 600
        $orderClass = $this->getOrderClass();

        $orders = $orderClass::find()->with('itemsInOrder2')->orderBy('created_at')->all();
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
        $this->assertEquals(3, count($orders));

        $order = $orders[0];
        $this->assertEquals(1, $order->id);
        $this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
        $this->assertEquals(2, count($order->itemsInOrder2));
        $this->assertEquals(1, $order->itemsInOrder2[0]->id);
        $this->assertEquals(2, $order->itemsInOrder2[1]->id);

        $order = $orders[1];
        $this->assertEquals(2, $order->id);
        $this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
        $this->assertEquals(3, count($order->itemsInOrder2));
        $this->assertEquals(5, $order->itemsInOrder2[0]->id);
        $this->assertEquals(3, $order->itemsInOrder2[1]->id);
        $this->assertEquals(4, $order->itemsInOrder2[2]->id);

        $order = $orders[2];
        $this->assertEquals(3, $order->id);
        $this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
        $this->assertEquals(1, count($order->itemsInOrder2));
        $this->assertEquals(2, $order->itemsInOrder2[0]->id);
    }

    public function testLink()
    {
627 628 629 630
        /* @var $orderClass \yii\db\ActiveRecordInterface */
        /* @var $itemClass \yii\db\ActiveRecordInterface */
        /* @var $orderItemClass \yii\db\ActiveRecordInterface */
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
631
        $customerClass = $this->getCustomerClass();
632 633
        $orderClass = $this->getOrderClass();
        $orderItemClass = $this->getOrderItemClass();
Qiang Xue committed
634
        $itemClass = $this->getItemClass();
635
        /* @var $this TestCase|ActiveRecordTestTrait */
Alexander Makarov committed
636
        $customer = $customerClass::findOne(2);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        $this->assertEquals(2, count($customer->orders));

        // has many
        $order = new $orderClass;
        $order->total = 100;
        $this->assertTrue($order->isNewRecord);
        $customer->link('orders', $order);
        $this->afterSave();
        $this->assertEquals(3, count($customer->orders));
        $this->assertFalse($order->isNewRecord);
        $this->assertEquals(3, count($customer->getOrders()->all()));
        $this->assertEquals(2, $order->customer_id);

        // belongs to
        $order = new $orderClass;
        $order->total = 100;
        $this->assertTrue($order->isNewRecord);
Alexander Makarov committed
654
        $customer = $customerClass::findOne(1);
655 656 657 658 659 660 661
        $this->assertNull($order->customer);
        $order->link('customer', $customer);
        $this->assertFalse($order->isNewRecord);
        $this->assertEquals(1, $order->customer_id);
        $this->assertEquals(1, $order->customer->primaryKey);

        // via model
Alexander Makarov committed
662
        $order = $orderClass::findOne(1);
663 664
        $this->assertEquals(2, count($order->items));
        $this->assertEquals(2, count($order->orderItems));
Alexander Makarov committed
665
        $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]);
666
        $this->assertNull($orderItem);
Alexander Makarov committed
667
        $item = $itemClass::findOne(3);
668 669 670 671
        $order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]);
        $this->afterSave();
        $this->assertEquals(3, count($order->items));
        $this->assertEquals(3, count($order->orderItems));
Alexander Makarov committed
672
        $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]);
673 674 675 676 677 678 679
        $this->assertTrue($orderItem instanceof $orderItemClass);
        $this->assertEquals(10, $orderItem->quantity);
        $this->assertEquals(100, $orderItem->subtotal);
    }

    public function testUnlink()
    {
680
        /* @var $customerClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
681
        $customerClass = $this->getCustomerClass();
682
        /* @var $orderClass \yii\db\ActiveRecordInterface */
Qiang Xue committed
683
        $orderClass = $this->getOrderClass();
684
        /* @var $orderWithNullFKClass \yii\db\ActiveRecordInterface */
685
        $orderWithNullFKClass = $this->getOrderWithNullFKClass();
686
        /* @var $orderItemsWithNullFKClass \yii\db\ActiveRecordInterface */
687
        $orderItemsWithNullFKClass = $this->getOrderItemWithNullFKmClass();
688 689


Qiang Xue committed
690

691
        /* @var $this TestCase|ActiveRecordTestTrait */
692 693 694 695 696 697 698 699 700 701 702 703
        // has many without delete
        $customer = $customerClass::findOne(2);
        $this->assertEquals(2, count($customer->ordersWithNullFK));
        $customer->unlink('ordersWithNullFK', $customer->ordersWithNullFK[1], false);

        $this->assertEquals(1, count($customer->ordersWithNullFK));
        $orderWithNullFK = $orderWithNullFKClass::findOne(3);

        $this->assertEquals(3,$orderWithNullFK->id);
        $this->assertNull($orderWithNullFK->customer_id);

        // has many with delete
Alexander Makarov committed
704
        $customer = $customerClass::findOne(2);
705 706
        $this->assertEquals(2, count($customer->orders));
        $customer->unlink('orders', $customer->orders[1], true);
707
        $this->afterSave();
708

709
        $this->assertEquals(1, count($customer->orders));
Alexander Makarov committed
710
        $this->assertNull($orderClass::findOne(3));
711

712
        // via model with delete
Alexander Makarov committed
713
        $order = $orderClass::findOne(2);
714 715 716
        $this->assertEquals(3, count($order->items));
        $this->assertEquals(3, count($order->orderItems));
        $order->unlink('items', $order->items[2], true);
717
        $this->afterSave();
718

719 720
        $this->assertEquals(2, count($order->items));
        $this->assertEquals(2, count($order->orderItems));
721 722 723 724

        // via model without delete
        $this->assertEquals(3, count($order->itemsWithNullFK));
        $order->unlink('itemsWithNullFK', $order->itemsWithNullFK[2], false);
725
        $this->afterSave();
726 727 728 729 730 731 732

        $this->assertEquals(2, count($order->itemsWithNullFK));
        $this->assertEquals(2, count($order->orderItems));
    }

    public function testUnlinkAll()
    {
733
        /* @var $customerClass \yii\db\ActiveRecordInterface */
734
        $customerClass = $this->getCustomerClass();
735
        /* @var $orderClass \yii\db\ActiveRecordInterface */
736
        $orderClass = $this->getOrderClass();
737
        /* @var $orderItemClass \yii\db\ActiveRecordInterface */
738
        $orderItemClass = $this->getOrderItemClass();
739
        /* @var $itemClass \yii\db\ActiveRecordInterface */
740
        $itemClass = $this->getItemClass();
741
        /* @var $orderWithNullFKClass \yii\db\ActiveRecordInterface */
742
        $orderWithNullFKClass = $this->getOrderWithNullFKClass();
743
        /* @var $orderItemsWithNullFKClass \yii\db\ActiveRecordInterface */
744
        $orderItemsWithNullFKClass = $this->getOrderItemWithNullFKmClass();
745

746
        /* @var $this TestCase|ActiveRecordTestTrait */
747 748 749
        // has many with delete
        $customer = $customerClass::findOne(2);
        $this->assertEquals(2, count($customer->orders));
750
        $this->assertEquals(3, $orderClass::find()->count());
751
        $customer->unlinkAll('orders', true);
752 753
        $this->afterSave();
        $this->assertEquals(1, $orderClass::find()->count());
754 755 756 757 758 759 760 761 762
        $this->assertEquals(0, count($customer->orders));

        $this->assertNull($orderClass::findOne(2));
        $this->assertNull($orderClass::findOne(3));


        // has many without delete
        $customer = $customerClass::findOne(2);
        $this->assertEquals(2, count($customer->ordersWithNullFK));
763
        $this->assertEquals(3, $orderWithNullFKClass::find()->count());
764
        $customer->unlinkAll('ordersWithNullFK', false);
765
        $this->afterSave();
766
        $this->assertEquals(0, count($customer->ordersWithNullFK));
767 768
        $this->assertEquals(3, $orderWithNullFKClass::find()->count());
        $this->assertEquals(2, $orderWithNullFKClass::find()->where(['AND', ['id' => [2, 3]], ['customer_id' => null]])->count());
769 770 771


        // via model with delete
772
        /* @var $order Order */
773 774
        $order = $orderClass::findOne(1);
        $this->assertEquals(2, count($order->books));
Carsten Brandt committed
775
        $orderItemCount = $orderItemClass::find()->count();
776
        $this->assertEquals(5, $itemClass::find()->count());
777
        $order->unlinkAll('books', true);
778 779
        $this->afterSave();
        $this->assertEquals(5, $itemClass::find()->count());
Carsten Brandt committed
780
        $this->assertEquals($orderItemCount - 2, $orderItemClass::find()->count());
781 782 783
        $this->assertEquals(0, count($order->books));

        // via model without delete
784
        $this->assertEquals(2, count($order->booksWithNullFK));
Carsten Brandt committed
785
        $orderItemCount = $orderItemsWithNullFKClass::find()->count();
786
        $this->assertEquals(5, $itemClass::find()->count());
787
        $order->unlinkAll('booksWithNullFK',false);
788 789
        $this->afterSave();
        $this->assertEquals(0, count($order->booksWithNullFK));
Carsten Brandt committed
790 791
        $this->assertEquals(2, $orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [1, 2]], ['order_id' => null]])->count());
        $this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count());
792 793 794
        $this->assertEquals(5, $itemClass::find()->count());

        // via table is covered in \yiiunit\framework\db\ActiveRecordTest::testUnlinkAllViaTable()
795 796
    }

797 798 799 800 801 802 803
    public function testUnlinkAllAndConditionSetNull()
    {
        /* @var $this TestCase|ActiveRecordTestTrait */

        /* @var $customerClass \yii\db\BaseActiveRecord */
        $customerClass = $this->getCustomerClass();
        /* @var $orderClass \yii\db\BaseActiveRecord */
804
        $orderClass = $this->getOrderWithNullFKClass();
805 806 807 808 809 810 811 812 813 814

        // 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');
815
        $this->assertEquals(3, count($customer->ordersWithNullFK));
816 817
        $this->assertEquals(0, count($customer->expensiveOrdersWithNullFK));
        $this->assertEquals(3, $orderClass::find()->count());
818 819 820
        $customer = $customerClass::findOne(1);
        $this->assertEquals(2, count($customer->ordersWithNullFK));
        $this->assertEquals(0, count($customer->expensiveOrdersWithNullFK));
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    }

    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);
841
        $this->assertEquals(3, count($customer->orders));
842 843
        $this->assertEquals(0, count($customer->expensiveOrders));
        $this->assertEquals(2, $orderClass::find()->count());
844 845 846
        $customer = $customerClass::findOne(1);
        $this->assertEquals(2, count($customer->orders));
        $this->assertEquals(0, count($customer->expensiveOrders));
847 848
    }

849 850 851 852 853
    public static $afterSaveNewRecord;
    public static $afterSaveInsert;

    public function testInsert()
    {
854
        /* @var $customerClass \yii\db\ActiveRecordInterface */
855
        $customerClass = $this->getCustomerClass();
856
        /* @var $this TestCase|ActiveRecordTestTrait */
857 858 859 860 861 862 863 864 865 866 867 868 869 870
        $customer = new $customerClass;
        $customer->email = 'user4@example.com';
        $customer->name = 'user4';
        $customer->address = 'address4';

        $this->assertNull($customer->id);
        $this->assertTrue($customer->isNewRecord);
        static::$afterSaveNewRecord = null;
        static::$afterSaveInsert = null;

        $customer->save();
        $this->afterSave();

        $this->assertNotNull($customer->id);
871
        $this->assertFalse(static::$afterSaveNewRecord);
872 873 874 875 876 877
        $this->assertTrue(static::$afterSaveInsert);
        $this->assertFalse($customer->isNewRecord);
    }

    public function testUpdate()
    {
878
        /* @var $customerClass \yii\db\ActiveRecordInterface */
879
        $customerClass = $this->getCustomerClass();
880
        /* @var $this TestCase|ActiveRecordTestTrait */
881
        // save
Carsten Brandt committed
882
        /* @var $customer Customer */
Alexander Makarov committed
883
        $customer = $customerClass::findOne(2);
884 885 886 887 888
        $this->assertTrue($customer instanceof $customerClass);
        $this->assertEquals('user2', $customer->name);
        $this->assertFalse($customer->isNewRecord);
        static::$afterSaveNewRecord = null;
        static::$afterSaveInsert = null;
Carsten Brandt committed
889
        $this->assertEmpty($customer->dirtyAttributes);
890 891 892 893 894 895 896 897

        $customer->name = 'user2x';
        $customer->save();
        $this->afterSave();
        $this->assertEquals('user2x', $customer->name);
        $this->assertFalse($customer->isNewRecord);
        $this->assertFalse(static::$afterSaveNewRecord);
        $this->assertFalse(static::$afterSaveInsert);
Alexander Makarov committed
898
        $customer2 = $customerClass::findOne(2);
899 900 901
        $this->assertEquals('user2x', $customer2->name);

        // updateAll
Alexander Makarov committed
902
        $customer = $customerClass::findOne(3);
903 904 905 906
        $this->assertEquals('user3', $customer->name);
        $ret = $customerClass::updateAll(['name' => 'temp'], ['id' => 3]);
        $this->afterSave();
        $this->assertEquals(1, $ret);
Alexander Makarov committed
907
        $customer = $customerClass::findOne(3);
908 909 910 911 912 913 914 915 916 917 918 919 920
        $this->assertEquals('temp', $customer->name);

        $ret = $customerClass::updateAll(['name' => 'tempX']);
        $this->afterSave();
        $this->assertEquals(3, $ret);

        $ret = $customerClass::updateAll(['name' => 'temp'], ['name' => 'user6']);
        $this->afterSave();
        $this->assertEquals(0, $ret);
    }

    public function testUpdateAttributes()
    {
921
        /* @var $customerClass \yii\db\ActiveRecordInterface */
922
        $customerClass = $this->getCustomerClass();
923
        /* @var $this TestCase|ActiveRecordTestTrait */
924
        /* @var $customer Customer */
Alexander Makarov committed
925
        $customer = $customerClass::findOne(2);
926 927 928 929 930 931 932 933 934 935
        $this->assertTrue($customer instanceof $customerClass);
        $this->assertEquals('user2', $customer->name);
        $this->assertFalse($customer->isNewRecord);
        static::$afterSaveNewRecord = null;
        static::$afterSaveInsert = null;

        $customer->updateAttributes(['name' => 'user2x']);
        $this->afterSave();
        $this->assertEquals('user2x', $customer->name);
        $this->assertFalse($customer->isNewRecord);
936 937
        $this->assertNull(static::$afterSaveNewRecord);
        $this->assertNull(static::$afterSaveInsert);
Alexander Makarov committed
938
        $customer2 = $customerClass::findOne(2);
939 940
        $this->assertEquals('user2x', $customer2->name);

Alexander Makarov committed
941
        $customer = $customerClass::findOne(1);
942 943 944 945 946 947 948
        $this->assertEquals('user1', $customer->name);
        $this->assertEquals(1, $customer->status);
        $customer->name = 'user1x';
        $customer->status = 2;
        $customer->updateAttributes(['name']);
        $this->assertEquals('user1x', $customer->name);
        $this->assertEquals(2, $customer->status);
Alexander Makarov committed
949
        $customer = $customerClass::findOne(1);
950 951 952 953 954 955
        $this->assertEquals('user1x', $customer->name);
        $this->assertEquals(1, $customer->status);
    }

    public function testUpdateCounters()
    {
956
        /* @var $orderItemClass \yii\db\ActiveRecordInterface */
957
        $orderItemClass = $this->getOrderItemClass();
958
        /* @var $this TestCase|ActiveRecordTestTrait */
959 960
        // updateCounters
        $pk = ['order_id' => 2, 'item_id' => 4];
Alexander Makarov committed
961
        $orderItem = $orderItemClass::findOne($pk);
962 963 964 965 966
        $this->assertEquals(1, $orderItem->quantity);
        $ret = $orderItem->updateCounters(['quantity' => -1]);
        $this->afterSave();
        $this->assertEquals(1, $ret);
        $this->assertEquals(0, $orderItem->quantity);
Alexander Makarov committed
967
        $orderItem = $orderItemClass::findOne($pk);
968 969 970 971
        $this->assertEquals(0, $orderItem->quantity);

        // updateAllCounters
        $pk = ['order_id' => 1, 'item_id' => 2];
Alexander Makarov committed
972
        $orderItem = $orderItemClass::findOne($pk);
973 974 975 976 977 978 979
        $this->assertEquals(2, $orderItem->quantity);
        $ret = $orderItemClass::updateAllCounters([
            'quantity' => 3,
            'subtotal' => -10,
        ], $pk);
        $this->afterSave();
        $this->assertEquals(1, $ret);
Alexander Makarov committed
980
        $orderItem = $orderItemClass::findOne($pk);
981 982 983 984 985 986
        $this->assertEquals(5, $orderItem->quantity);
        $this->assertEquals(30, $orderItem->subtotal);
    }

    public function testDelete()
    {
987
        /* @var $customerClass \yii\db\ActiveRecordInterface */
988
        $customerClass = $this->getCustomerClass();
989
        /* @var $this TestCase|ActiveRecordTestTrait */
990
        // delete
Alexander Makarov committed
991
        $customer = $customerClass::findOne(2);
992 993 994 995
        $this->assertTrue($customer instanceof $customerClass);
        $this->assertEquals('user2', $customer->name);
        $customer->delete();
        $this->afterSave();
Alexander Makarov committed
996
        $customer = $customerClass::findOne(2);
997 998 999
        $this->assertNull($customer);

        // deleteAll
Qiang Xue committed
1000
        $customers = $customerClass::find()->all();
1001 1002 1003 1004
        $this->assertEquals(2, count($customers));
        $ret = $customerClass::deleteAll();
        $this->afterSave();
        $this->assertEquals(2, $ret);
Qiang Xue committed
1005
        $customers = $customerClass::find()->all();
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
        $this->assertEquals(0, count($customers));

        $ret = $customerClass::deleteAll();
        $this->afterSave();
        $this->assertEquals(0, $ret);
    }

    /**
     * Some PDO implementations(e.g. cubrid) do not support boolean values.
     * Make sure this does not affect AR layer.
     */
    public function testBooleanAttribute()
    {
1019
        /* @var $customerClass \yii\db\ActiveRecordInterface */
1020
        $customerClass = $this->getCustomerClass();
1021
        /* @var $this TestCase|ActiveRecordTestTrait */
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
        $customer = new $customerClass();
        $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);

Qiang Xue committed
1037
        $customers = $customerClass::find()->where(['status' => true])->all();
1038 1039
        $this->assertEquals(2, count($customers));

Qiang Xue committed
1040
        $customers = $customerClass::find()->where(['status' => false])->all();
1041 1042 1043 1044 1045
        $this->assertEquals(1, count($customers));
    }

    public function testAfterFind()
    {
1046
        /* @var $customerClass \yii\db\ActiveRecordInterface */
1047
        $customerClass = $this->getCustomerClass();
1048
        /* @var $orderClass BaseActiveRecord */
1049
        $orderClass = $this->getOrderClass();
1050
        /* @var $this TestCase|ActiveRecordTestTrait */
1051 1052 1053

        $afterFindCalls = [];
        Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND, function ($event) use (&$afterFindCalls) {
1054
            /* @var $ar BaseActiveRecord */
1055 1056 1057 1058
            $ar = $event->sender;
            $afterFindCalls[] = [get_class($ar), $ar->getIsNewRecord(), $ar->getPrimaryKey(), $ar->isRelationPopulated('orders')];
        });

Alexander Makarov committed
1059
        $customer = $customerClass::findOne(1);
1060 1061 1062 1063
        $this->assertNotNull($customer);
        $this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
        $afterFindCalls = [];

Qiang Xue committed
1064
        $customer = $customerClass::find()->where(['id' => 1])->one();
1065 1066 1067 1068
        $this->assertNotNull($customer);
        $this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
        $afterFindCalls = [];

Qiang Xue committed
1069
        $customer = $customerClass::find()->where(['id' => 1])->all();
1070 1071 1072 1073
        $this->assertNotNull($customer);
        $this->assertEquals([[$customerClass, false, 1, false]], $afterFindCalls);
        $afterFindCalls = [];

Qiang Xue committed
1074
        $customer = $customerClass::find()->where(['id' => 1])->with('orders')->all();
1075 1076 1077 1078 1079 1080 1081 1082
        $this->assertNotNull($customer);
        $this->assertEquals([
            [$this->getOrderClass(), false, 1, false],
            [$customerClass, false, 1, true],
        ], $afterFindCalls);
        $afterFindCalls = [];

        if ($this instanceof \yiiunit\extensions\redis\ActiveRecordTest) { // TODO redis does not support orderBy() yet
Qiang Xue committed
1083
            $customer = $customerClass::find()->where(['id' => [1, 2]])->with('orders')->all();
1084 1085
        } else {
            // orderBy is needed to avoid random test failure
Qiang Xue committed
1086
            $customer = $customerClass::find()->where(['id' => [1, 2]])->with('orders')->orderBy('name')->all();
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
        }
        $this->assertNotNull($customer);
        $this->assertEquals([
            [$orderClass, false, 1, false],
            [$orderClass, false, 2, false],
            [$orderClass, false, 3, false],
            [$customerClass, false, 1, true],
            [$customerClass, false, 2, true],
        ], $afterFindCalls);
        $afterFindCalls = [];

        Event::off(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND);
    }
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    public function testFindEmptyInCondition()
    {
        /* @var $customerClass \yii\db\ActiveRecordInterface */
        $customerClass = $this->getCustomerClass();
        /* @var $this TestCase|ActiveRecordTestTrait */

        $customers = $customerClass::find()->where(['id' => [1]])->all();
        $this->assertEquals(1, count($customers));

        $customers = $customerClass::find()->where(['id' => []])->all();
        $this->assertEquals(0, count($customers));

        $customers = $customerClass::find()->where(['IN', 'id', [1]])->all();
        $this->assertEquals(1, count($customers));

        $customers = $customerClass::find()->where(['IN', 'id', []])->all();
        $this->assertEquals(0, count($customers));
    }
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

    public function testFindEagerIndexBy()
    {
        /* @var $this TestCase|ActiveRecordTestTrait */

        /* @var $orderClass \yii\db\ActiveRecordInterface */
        $orderClass = $this->getOrderClass();

        /* @var $order Order */
        $order = $orderClass::find()->with('itemsIndexed')->where(['id' => 1])->one();
        $this->assertTrue($order->isRelationPopulated('itemsIndexed'));
        $items = $order->itemsIndexed;
        $this->assertEquals(2, count($items));
        $this->assertTrue(isset($items[1]));
        $this->assertTrue(isset($items[2]));

        /* @var $order Order */
        $order = $orderClass::find()->with('itemsIndexed')->where(['id' => 2])->one();
        $this->assertTrue($order->isRelationPopulated('itemsIndexed'));
        $items = $order->itemsIndexed;
        $this->assertEquals(3, count($items));
        $this->assertTrue(isset($items[3]));
        $this->assertTrue(isset($items[4]));
        $this->assertTrue(isset($items[5]));
    }
1144
}