ActiveRecordTest.php 15.9 KB
Newer Older
Qiang Xue committed
1
<?php
Qiang Xue committed
2
namespace yiiunit\framework\db;
Qiang Xue committed
3 4 5

use yiiunit\data\ar\ActiveRecord;
use yiiunit\data\ar\Customer;
6
use yiiunit\data\ar\NullValues;
Qiang Xue committed
7
use yiiunit\data\ar\OrderItem;
Qiang Xue committed
8
use yiiunit\data\ar\Order;
Qiang Xue committed
9
use yiiunit\data\ar\Item;
10
use yiiunit\data\ar\Profile;
11
use yiiunit\framework\ar\ActiveRecordTestTrait;
Qiang Xue committed
12

13 14 15 16
/**
 * @group db
 * @group mysql
 */
Alexander Makarov committed
17
class ActiveRecordTest extends DatabaseTestCase
Qiang Xue committed
18
{
19 20
	use ActiveRecordTestTrait;

Carsten Brandt committed
21
	protected function setUp()
Qiang Xue committed
22
	{
Alexander Makarov committed
23
		parent::setUp();
Qiang Xue committed
24 25 26
		ActiveRecord::$db = $this->getConnection();
	}

27 28 29 30
	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); }
Qiang Xue committed
31

32 33 34 35
	public function getCustomerClass() { return Customer::className(); }
	public function getItemClass() { return Item::className(); }
	public function getOrderClass() { return Order::className(); }
	public function getOrderItemClass() { return OrderItem::className(); }
Qiang Xue committed
36

37 38
	public function testCustomColumns()
	{
Qiang Xue committed
39
		// find custom column
40
		$customer = $this->callCustomerFind()->select(['*', '(status*2) AS status2'])
Alexander Makarov committed
41
			->where(['name' => 'user3'])->one();
Qiang Xue committed
42 43
		$this->assertEquals(3, $customer->id);
		$this->assertEquals(4, $customer->status2);
44
	}
Qiang Xue committed
45

Qiang Xue committed
46
	public function testStatisticalFind()
47
	{
48
		// find count, sum, average, min, max, scalar
49 50 51 52 53 54 55
		$this->assertEquals(3, $this->callCustomerFind()->count());
		$this->assertEquals(2, $this->callCustomerFind()->where('id=1 OR id=2')->count());
		$this->assertEquals(6, $this->callCustomerFind()->sum('id'));
		$this->assertEquals(2, $this->callCustomerFind()->average('id'));
		$this->assertEquals(1, $this->callCustomerFind()->min('id'));
		$this->assertEquals(3, $this->callCustomerFind()->max('id'));
		$this->assertEquals(3, $this->callCustomerFind()->select('COUNT(*)')->scalar());
Qiang Xue committed
56 57
	}

58
	public function testFindScalar()
59
	{
60
		// query scalar
Luciano Baraglia committed
61
		$customerName = $this->callCustomerFind()->where(['id' => 2])->select('name')->scalar();
62
		$this->assertEquals('user2', $customerName);
63 64
	}

65
	public function testFindColumn()
66
	{
67 68 69
		/** @var TestCase|ActiveRecordTestTrait $this */
		$this->assertEquals(['user1', 'user2', 'user3'], Customer::find()->select('name')->column());
		$this->assertEquals(['user3', 'user2', 'user1'], Customer::find()->orderBy(['name' => SORT_DESC])->select('name')->column());
70 71
	}

Qiang Xue committed
72 73 74 75 76 77 78 79 80 81 82 83
	public function testFindBySql()
	{
		// find one
		$customer = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC')->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user3', $customer->name);

		// find all
		$customers = Customer::findBySql('SELECT * FROM tbl_customer')->all();
		$this->assertEquals(3, count($customers));

		// find with parameter binding
Alexander Makarov committed
84
		$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', [':id' => 2])->one();
Qiang Xue committed
85 86 87
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
	}
Qiang Xue committed
88

Qiang Xue committed
89 90
	public function testFindLazyViaTable()
	{
slavcodev committed
91
		/** @var Order $order */
Qiang Xue committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105
		$order = Order::find(1);
		$this->assertEquals(1, $order->id);
		$this->assertEquals(2, count($order->books));
		$this->assertEquals(1, $order->items[0]->id);
		$this->assertEquals(2, $order->items[1]->id);

		$order = Order::find(2);
		$this->assertEquals(2, $order->id);
		$this->assertEquals(0, count($order->books));
	}

	public function testFindEagerViaTable()
	{
		$orders = Order::find()->with('books')->orderBy('id')->all();
Qiang Xue committed
106 107
		$this->assertEquals(3, count($orders));

Qiang Xue committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121
		$order = $orders[0];
		$this->assertEquals(1, $order->id);
		$this->assertEquals(2, count($order->books));
		$this->assertEquals(1, $order->books[0]->id);
		$this->assertEquals(2, $order->books[1]->id);

		$order = $orders[1];
		$this->assertEquals(2, $order->id);
		$this->assertEquals(0, count($order->books));

		$order = $orders[2];
		$this->assertEquals(3, $order->id);
		$this->assertEquals(1, count($order->books));
		$this->assertEquals(2, $order->books[0]->id);
Qiang Xue committed
122 123 124 125 126 127 128 129 130 131 132

		// https://github.com/yiisoft/yii2/issues/1402
		$orders = Order::find()->with('books')->orderBy('id')->asArray()->all();
		$this->assertEquals(3, count($orders));

		$order = $orders[0];
		$this->assertTrue(is_array($order));
		$this->assertEquals(1, $order['id']);
		$this->assertEquals(2, count($order['books']));
		$this->assertEquals(1, $order['books'][0]['id']);
		$this->assertEquals(2, $order['books'][1]['id']);
Qiang Xue committed
133 134
	}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	// deeply nested table relation
	public function testDeeplyNestedTableRelation()
	{
		/** @var Customer $customer */
		$customer = $this->callCustomerFind(1);
		$this->assertNotNull($customer);

		$items = $customer->orderItems;

		$this->assertEquals(2, count($items));
		$this->assertInstanceOf(Item::className(), $items[0]);
		$this->assertInstanceOf(Item::className(), $items[1]);
		$this->assertEquals(1, $items[0]->id);
		$this->assertEquals(2, $items[1]->id);
	}

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
	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);
	}
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	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);
	}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	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']));
	}
236 237 238

	public function testJoinWith()
	{
239 240 241 242 243 244 245 246 247 248
		// left join and eager loading
		$orders = Order::find()->joinWith('customer')->orderBy('tbl_customer.id DESC, tbl_order.id')->all();
		$this->assertEquals(3, count($orders));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);
		$this->assertEquals(1, $orders[2]->id);
		$this->assertTrue($orders[0]->isRelationPopulated('customer'));
		$this->assertTrue($orders[1]->isRelationPopulated('customer'));
		$this->assertTrue($orders[2]->isRelationPopulated('customer'));

249
		// inner join filtering and eager loading
250
		$orders = Order::find()->innerJoinWith([
251 252 253 254 255 256 257 258 259 260 261
			'customer' => function ($query) {
				$query->where('tbl_customer.id=2');
			},
		])->orderBy('tbl_order.id')->all();
		$this->assertEquals(2, count($orders));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);
		$this->assertTrue($orders[0]->isRelationPopulated('customer'));
		$this->assertTrue($orders[1]->isRelationPopulated('customer'));

		// inner join filtering without eager loading
262
		$orders = Order::find()->innerJoinWith([
263 264 265 266 267 268 269 270 271
			'customer' => function ($query) {
				$query->where('tbl_customer.id=2');
			},
		], false)->orderBy('tbl_order.id')->all();
		$this->assertEquals(2, count($orders));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);
		$this->assertFalse($orders[0]->isRelationPopulated('customer'));
		$this->assertFalse($orders[1]->isRelationPopulated('customer'));
272 273

		// join with via-relation
274
		$orders = Order::find()->innerJoinWith('books')->orderBy('tbl_order.id')->all();
275 276 277 278 279 280 281 282 283
		$this->assertEquals(2, count($orders));
		$this->assertEquals(1, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);
		$this->assertTrue($orders[0]->isRelationPopulated('books'));
		$this->assertTrue($orders[1]->isRelationPopulated('books'));
		$this->assertEquals(2, count($orders[0]->books));
		$this->assertEquals(1, count($orders[1]->books));

		// join with sub-relation
284
		$orders = Order::find()->innerJoinWith([
285 286 287 288 289 290 291 292 293 294
			'items.category' => function ($q) {
				$q->where('tbl_category.id = 2');
			},
		])->orderBy('tbl_order.id')->all();
		$this->assertEquals(1, count($orders));
		$this->assertTrue($orders[0]->isRelationPopulated('items'));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, count($orders[0]->items));
		$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
		$this->assertEquals(2, $orders[0]->items[0]->category->id);
295 296 297 298 299 300 301 302 303 304 305 306 307 308

		// join with table alias
		$orders = Order::find()->joinWith([
			'customer' => function ($q) {
				$q->from('tbl_customer c');
			}
		])->orderBy('c.id DESC, tbl_order.id')->all();
		$this->assertEquals(3, count($orders));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);
		$this->assertEquals(1, $orders[2]->id);
		$this->assertTrue($orders[0]->isRelationPopulated('customer'));
		$this->assertTrue($orders[1]->isRelationPopulated('customer'));
		$this->assertTrue($orders[2]->isRelationPopulated('customer'));
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

		// join with ON condition
		$orders = Order::find()->joinWith('books2')->orderBy('tbl_order.id')->all();
		$this->assertEquals(3, count($orders));
		$this->assertEquals(1, $orders[0]->id);
		$this->assertEquals(2, $orders[1]->id);
		$this->assertEquals(3, $orders[2]->id);
		$this->assertTrue($orders[0]->isRelationPopulated('books2'));
		$this->assertTrue($orders[1]->isRelationPopulated('books2'));
		$this->assertTrue($orders[2]->isRelationPopulated('books2'));
		$this->assertEquals(2, count($orders[0]->books2));
		$this->assertEquals(0, count($orders[1]->books2));
		$this->assertEquals(1, count($orders[2]->books2));

		// lazy loading with ON condition
		$order = Order::find(1);
		$this->assertEquals(2, count($order->books2));
		$order = Order::find(2);
		$this->assertEquals(0, count($order->books2));
		$order = Order::find(3);
		$this->assertEquals(1, count($order->books2));

		// eager loading with ON condition
		$orders = Order::find()->with('books2')->all();
		$this->assertEquals(3, count($orders));
		$this->assertEquals(1, $orders[0]->id);
		$this->assertEquals(2, $orders[1]->id);
		$this->assertEquals(3, $orders[2]->id);
		$this->assertTrue($orders[0]->isRelationPopulated('books2'));
		$this->assertTrue($orders[1]->isRelationPopulated('books2'));
		$this->assertTrue($orders[2]->isRelationPopulated('books2'));
		$this->assertEquals(2, count($orders[0]->books2));
		$this->assertEquals(0, count($orders[1]->books2));
		$this->assertEquals(1, count($orders[2]->books2));
343
	}
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	public function testJoinWithAndScope()
	{
		// hasOne inner join
		$customers = Customer::find()->active()->innerJoinWith('profile')->orderBy('tbl_customer.id')->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals(1, $customers[0]->id);
		$this->assertTrue($customers[0]->isRelationPopulated('profile'));

		// hasOne outer join
		$customers = Customer::find()->active()->joinWith('profile')->orderBy('tbl_customer.id')->all();
		$this->assertEquals(2, count($customers));
		$this->assertEquals(1, $customers[0]->id);
		$this->assertEquals(2, $customers[1]->id);
		$this->assertTrue($customers[0]->isRelationPopulated('profile'));
		$this->assertTrue($customers[1]->isRelationPopulated('profile'));
		$this->assertInstanceOf(Profile::className(), $customers[0]->profile);
		$this->assertNull($customers[1]->profile);

		// hasMany
		$customers = Customer::find()->active()->joinWith('orders')->orderBy('tbl_customer.id DESC, tbl_order.id')->all();
		$this->assertEquals(2, count($customers));
		$this->assertEquals(2, $customers[0]->id);
		$this->assertEquals(1, $customers[1]->id);
		$this->assertTrue($customers[0]->isRelationPopulated('orders'));
		$this->assertTrue($customers[1]->isRelationPopulated('orders'));

	}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	public function testInverseOf()
	{
		// eager loading: find one and all
		$customer = Customer::find()->with('orders2')->where(['id' => 1])->one();
		$this->assertTrue($customer->orders2[0]->customer2 === $customer);
		$customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->all();
		$this->assertTrue($customers[0]->orders2[0]->customer2 === $customers[0]);
		$this->assertTrue(empty($customers[1]->orders2));
		// lazy loading
		$customer = Customer::find(2);
		$orders = $customer->orders2;
		$this->assertTrue(count($orders) === 2);
		$this->assertTrue($customer->orders2[0]->customer2 === $customer);
		$this->assertTrue($customer->orders2[1]->customer2 === $customer);
		// ad-hoc lazy loading
		$customer = Customer::find(2);
		$orders = $customer->getOrders2()->all();
		$this->assertTrue(count($orders) === 2);
		$this->assertTrue($customer->orders2[0]->customer2 === $customer);
		$this->assertTrue($customer->orders2[1]->customer2 === $customer);

		// the other way around
		$customer = Customer::find()->with('orders2')->where(['id' => 1])->asArray()->one();
		$this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customer['id']);
		$customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->asArray()->all();
		$this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customers[0]['id']);
		$this->assertTrue(empty($customers[1]['orders2']));

		$orders = Order::find()->with('customer2')->where(['id' => 1])->all();
		$this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
		$order = Order::find()->with('customer2')->where(['id' => 1])->one();
		$this->assertTrue($order->customer2->orders2 === [$order]);

		$orders = Order::find()->with('customer2')->where(['id' => 1])->asArray()->all();
		$this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
		$order = Order::find()->with('customer2')->where(['id' => 1])->asArray()->one();
		$this->assertTrue($order['customer2']['orders2'][0]['id'] === $orders[0]['id']);

		$orders = Order::find()->with('customer2')->where(['id' => [1, 3]])->all();
		$this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
		$this->assertTrue($orders[1]->customer2->orders2 === [$orders[1]]);

		$orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->all();
		$this->assertTrue($orders[0]->customer2->orders2 === $orders);
		$this->assertTrue($orders[1]->customer2->orders2 === $orders);

		$orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->asArray()->all();
		$this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
		$this->assertTrue($orders[0]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
		$this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
		$this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
	}
Zander Baldwin committed
425
}