ActiveRecordTest.php 11.3 KB
Newer Older
Qiang Xue committed
1 2 3 4 5
<?php

namespace yiiunit\framework\db\ar;

use yii\db\dao\Query;
Qiang Xue committed
6
use yii\db\ar\ActiveQuery;
Qiang Xue committed
7 8
use yiiunit\data\ar\ActiveRecord;
use yiiunit\data\ar\Customer;
Qiang Xue committed
9
use yiiunit\data\ar\OrderItem;
Qiang Xue committed
10
use yiiunit\data\ar\Order;
Qiang Xue committed
11 12 13 14 15 16 17 18

class ActiveRecordTest extends \yiiunit\MysqlTestCase
{
	public function setUp()
	{
		ActiveRecord::$db = $this->getConnection();
	}

Qiang Xue committed
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
	public function testInsert()
	{
		$customer = new Customer;
		$customer->email = 'user4@example.com';
		$customer->name = 'user4';
		$customer->address = 'address4';

		$this->assertNull($customer->id);
		$this->assertTrue($customer->isNewRecord);

		$customer->save();

		$this->assertEquals(4, $customer->id);
		$this->assertFalse($customer->isNewRecord);
	}

	public function testUpdate()
	{
		// save
		$customer = Customer::find(2)->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
		$this->assertFalse($customer->isNewRecord);
		$customer->name = 'user2x';
		$customer->save();
		$this->assertEquals('user2x', $customer->name);
.  
Qiang Xue committed
45
		$this->assertFalse($customer->isNewRecord);
Qiang Xue committed
46 47 48
		$customer2 = Customer::find(2)->one();
		$this->assertEquals('user2x', $customer2->name);

Qiang Xue committed
49
		// updateCounters
Qiang Xue committed
50
		$pk = array('order_id' => 2, 'item_id' => 4);
Qiang Xue committed
51
		$orderItem = OrderItem::find()->where($pk)->one();
Qiang Xue committed
52
		$this->assertEquals(1, $orderItem->quantity);
Qiang Xue committed
53
		$ret = $orderItem->updateCounters(array('quantity' => -1));
Qiang Xue committed
54
		$this->assertTrue($ret);
Qiang Xue committed
55
		$this->assertEquals(0, $orderItem->quantity);
Qiang Xue committed
56
		$orderItem = OrderItem::find()->where($pk)->one();
Qiang Xue committed
57 58 59 60 61
		$this->assertEquals(0, $orderItem->quantity);

		// updateAll
		$customer = Customer::find(3)->one();
		$this->assertEquals('user3', $customer->name);
Qiang Xue committed
62
		$ret = Customer::updateAll(array(
Qiang Xue committed
63 64
			'name' => 'temp',
		), array('id' => 3));
Qiang Xue committed
65
		$this->assertEquals(1, $ret);
Qiang Xue committed
66 67 68 69 70
		$customer = Customer::find(3)->one();
		$this->assertEquals('temp', $customer->name);

		// updateCounters
		$pk = array('order_id' => 1, 'item_id' => 2);
Qiang Xue committed
71
		$orderItem = OrderItem::find()->where($pk)->one();
Qiang Xue committed
72
		$this->assertEquals(2, $orderItem->quantity);
Qiang Xue committed
73
		$ret = OrderItem::updateAllCounters(array(
Qiang Xue committed
74 75
			'quantity' => 3,
		), $pk);
Qiang Xue committed
76
		$this->assertEquals(1, $ret);
Qiang Xue committed
77
		$orderItem = OrderItem::find()->where($pk)->one();
Qiang Xue committed
78 79 80 81 82
		$this->assertEquals(5, $orderItem->quantity);
	}

	public function testDelete()
	{
.  
Qiang Xue committed
83 84 85 86 87 88 89 90 91 92 93
		// delete
		$customer = Customer::find(2)->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
		$customer->delete();
		$customer = Customer::find(2)->one();
		$this->assertNull($customer);

		// deleteAll
		$customers = Customer::find()->all();
		$this->assertEquals(2, count($customers));
Qiang Xue committed
94 95
		$ret = Customer::deleteAll();
		$this->assertEquals(2, $ret);
.  
Qiang Xue committed
96 97
		$customers = Customer::find()->all();
		$this->assertEquals(0, count($customers));
Qiang Xue committed
98 99
	}

Qiang Xue committed
100 101 102 103
	public function testFind()
	{
		// find one
		$result = Customer::find();
Qiang Xue committed
104
		$this->assertTrue($result instanceof ActiveQuery);
Qiang Xue committed
105 106 107 108 109 110 111 112 113 114 115 116 117
		$customer = $result->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals(1, $result->count);

		// find all
		$result = Customer::find();
		$customers = $result->all();
		$this->assertTrue(is_array($customers));
		$this->assertEquals(3, count($customers));
		$this->assertTrue($customers[0] instanceof Customer);
		$this->assertTrue($customers[1] instanceof Customer);
		$this->assertTrue($customers[2] instanceof Customer);
		$this->assertEquals(3, $result->count);
Qiang Xue committed
118
		$this->assertEquals(3, count($result));
Qiang Xue committed
119 120 121 122 123 124

		// check count first
		$result = Customer::find();
		$this->assertEquals(3, $result->count);
		$customer = $result->one();
		$this->assertTrue($customer instanceof Customer);
Qiang Xue committed
125
		$this->assertEquals(3, $result->count);
Qiang Xue committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

		// iterator
		$result = Customer::find();
		$count = 0;
		foreach ($result as $customer) {
			$this->assertTrue($customer instanceof Customer);
			$count++;
		}
		$this->assertEquals($count, $result->count);

		// array access
		$result = Customer::find();
		$this->assertTrue($result[0] instanceof Customer);
		$this->assertTrue($result[1] instanceof Customer);
		$this->assertTrue($result[2] instanceof Customer);

		// find by a single primary key
		$customer = Customer::find(2)->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);

		// find by attributes
Qiang Xue committed
148
		$customer = Customer::find()->where(array('name' => 'user2'))->one();
Qiang Xue committed
149 150 151 152
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals(2, $customer->id);

		// find by Query
Qiang Xue committed
153 154 155 156
		$query = array(
			'where' => 'id=:id',
			'params' => array(':id' => 2),
		);
Qiang Xue committed
157 158 159
		$customer = Customer::find($query)->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
Qiang Xue committed
160 161

		// find count
Qiang Xue committed
162 163 164 165 166 167
		$this->assertEquals(3, Customer::find()->count());
		$this->assertEquals(3, Customer::count());
		$this->assertEquals(1, Customer::count(2));
		$this->assertEquals(2, Customer::count(array(
			'where' => 'id=1 OR id=2',
		)));
Qiang Xue committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
	}

	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
Qiang Xue committed
182
		$customer = Customer::findBySql('SELECT * FROM tbl_customer WHERE id=:id', array(':id' => 2))->one();
Qiang Xue committed
183 184 185 186
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);

		// count
Qiang Xue committed
187 188
		$query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
		$query->one();
Qiang Xue committed
189
		$this->assertEquals(3, $query->count);
Qiang Xue committed
190 191
		$query = Customer::findBySql('SELECT * FROM tbl_customer ORDER BY id DESC');
		$this->assertEquals(3, $query->count);
Qiang Xue committed
192 193 194 195
	}

	public function testQueryMethods()
	{
Qiang Xue committed
196
		$customer = Customer::find()->where('id=:id', array(':id' => 2))->one();
Qiang Xue committed
197 198 199 200 201 202 203
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);

		$customer = Customer::find()->where(array('name' => 'user3'))->one();
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user3', $customer->name);

204
		$customer = Customer::find()->select('id')->order('id DESC')->one();
Qiang Xue committed
205 206 207
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals(3, $customer->id);
		$this->assertEquals(null, $customer->name);
Qiang Xue committed
208 209 210 211

		// scopes
		$customers = Customer::find()->active()->all();
		$this->assertEquals(2, count($customers));
Qiang Xue committed
212 213 214 215 216 217
		$customers = Customer::find(array(
			'scopes' => array('active'),
		))->all();
		$this->assertEquals(2, count($customers));

		// asArray
218
		$customers = Customer::find()->order('id')->asArray()->all();
Qiang Xue committed
219 220
		$this->assertEquals('user2', $customers[1]['name']);

Qiang Xue committed
221 222
		// index
		$customers = Customer::find()->order('id')->index('name')->all();
Qiang Xue committed
223
		$this->assertEquals(2, $customers['user2']['id']);
Qiang Xue committed
224
	}
Qiang Xue committed
225 226 227

	public function testEagerLoading()
	{
Qiang Xue committed
228
		// has many
229
		$customers = Customer::find()->with('orders')->order('@.id')->all();
Qiang Xue committed
230 231 232 233 234
		$this->assertEquals(3, count($customers));
		$this->assertEquals(1, count($customers[0]->orders));
		$this->assertEquals(2, count($customers[1]->orders));
		$this->assertEquals(0, count($customers[2]->orders));

Qiang Xue committed
235
		// nested
236 237 238 239 240
		$customers = Customer::find()->with('orders.customer')->order('@.id')->all();
		$this->assertEquals(3, count($customers));
		$this->assertEquals(1, $customers[0]->orders[0]->customer->id);
		$this->assertEquals(2, $customers[1]->orders[0]->customer->id);
		$this->assertEquals(2, $customers[1]->orders[1]->customer->id);
Qiang Xue committed
241

Qiang Xue committed
242
		// has many via relation
Qiang Xue committed
243 244 245 246 247 248 249 250
		$orders = Order::find()->with('items')->order('@.id')->all();
		$this->assertEquals(3, count($orders));
		$this->assertEquals(1, $orders[0]->items[0]->id);
		$this->assertEquals(2, $orders[0]->items[1]->id);
		$this->assertEquals(3, $orders[1]->items[0]->id);
		$this->assertEquals(4, $orders[1]->items[1]->id);
		$this->assertEquals(5, $orders[1]->items[2]->id);

Qiang Xue committed
251
		// has many via join table
Qiang Xue committed
252 253 254 255 256
		$orders = Order::find()->with('books')->order('@.id')->all();
		$this->assertEquals(2, count($orders));
		$this->assertEquals(1, $orders[0]->books[0]->id);
		$this->assertEquals(2, $orders[0]->books[1]->id);
		$this->assertEquals(2, $orders[1]->books[0]->id);
Qiang Xue committed
257

Qiang Xue committed
258
		// has many and base limited
Qiang Xue committed
259 260
		$orders = Order::find()->with('items')->order('@.id')->limit(2)->all();
		$this->assertEquals(2, count($orders));
261 262 263 264 265 266 267 268
		$this->assertEquals(1, $orders[0]->items[0]->id);

		/// customize "with" query
		$orders = Order::find()->with(array('items' => function($q) {
			$q->order('@.id DESC');
		}))->order('@.id')->limit(2)->all();
		$this->assertEquals(2, count($orders));
		$this->assertEquals(2, $orders[0]->items[0]->id);
269

Qiang Xue committed
270
		// findBySql with
271 272
		$orders = Order::findBySql('SELECT * FROM tbl_order WHERE customer_id=2')->with('items')->all();
		$this->assertEquals(2, count($orders));
Qiang Xue committed
273

Qiang Xue committed
274
		// index and array
Qiang Xue committed
275 276 277 278 279 280 281 282
		$customers = Customer::find()->with('orders.customer')->order('@.id')->index('id')->asArray()->all();
		$this->assertEquals(3, count($customers));
		$this->assertTrue(isset($customers[1], $customers[2], $customers[3]));
		$this->assertTrue(is_array($customers[1]));
		$this->assertEquals(1, count($customers[1]['orders']));
		$this->assertEquals(2, count($customers[2]['orders']));
		$this->assertEquals(0, count($customers[3]['orders']));
		$this->assertTrue(is_array($customers[1]['orders'][0]['customer']));
Qiang Xue committed
283 284 285 286 287 288 289 290 291

		// count with
		$this->assertEquals(3, Order::count());
		$value = Order::count(array(
			'select' => array('COUNT(DISTINCT @.id, @.customer_id)'),
			'with' => 'books',
		));
		$this->assertEquals(2, $value);

Qiang Xue committed
292 293
	}

Qiang Xue committed
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
	public function testLazyLoading()
	{
		// has one
		$order = Order::find(3)->one();
		$this->assertTrue($order->customer instanceof Customer);
		$this->assertEquals(2, $order->customer->id);

		// has many
		$customer = Customer::find(2)->one();
		$orders = $customer->orders;
		$this->assertEquals(2, count($orders));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);

		// has many via join table
		$orders = Order::find()->order('@.id')->all();
		$this->assertEquals(3, count($orders));
		$this->assertEquals(2, count($orders[0]->books));
		$this->assertEquals(1, $orders[0]->books[0]->id);
		$this->assertEquals(2, $orders[0]->books[1]->id);
		$this->assertEquals(array(), $orders[1]->books);
		$this->assertEquals(1, count($orders[2]->books));
		$this->assertEquals(2, $orders[2]->books[0]->id);

		// customized relation query
		$customer = Customer::find(2)->one();
		$orders = $customer->orders(array(
			'where' => '@.id = 3',
		));
		$this->assertEquals(1, count($orders));
		$this->assertEquals(3, $orders[0]->id);
325

Qiang Xue committed
326 327 328 329 330
		// original results are kept after customized query
		$orders = $customer->orders;
		$this->assertEquals(2, count($orders));
		$this->assertEquals(2, $orders[0]->id);
		$this->assertEquals(3, $orders[1]->id);
331

Qiang Xue committed
332 333 334 335 336 337 338 339
		// as array
		$orders = $customer->orders(array(
			'asArray' => true,
		));
		$this->assertEquals(2, count($orders));
		$this->assertTrue(is_array($orders[0]));
		$this->assertEquals(2, $orders[0]['id']);
		$this->assertEquals(3, $orders[1]['id']);
340 341 342 343 344 345 346 347 348

		// using anonymous function to customize query condition
		$orders = $customer->orders(function($q) {
			$q->order('@.id DESC')->asArray();
		});
		$this->assertEquals(2, count($orders));
		$this->assertTrue(is_array($orders[0]));
		$this->assertEquals(3, $orders[0]['id']);
		$this->assertEquals(2, $orders[1]['id']);
Qiang Xue committed
349
	}
Qiang Xue committed
350
}