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

3
namespace yiiunit\framework\redis;
4

5
use yii\db\Query;
6
use yii\redis\ActiveQuery;
7 8 9 10 11 12
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;

Carsten Brandt committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
Users:
1 - user1
2 - user2
3 - user3

Items: 1-5

Order: 1-3

OrderItem:
1 - order: 1
2 - order: 1
3 - order: 2
4 - order: 2
5 - order: 2
6 - order: 3

 */

33 34 35 36
class ActiveRecordTest extends RedisTestCase
{
	public function setUp()
	{
Carsten Brandt committed
37
		parent::setUp();
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
		ActiveRecord::$db = $this->getConnection();

		$customer = new Customer();
		$customer->setAttributes(array('email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1), false);
		$customer->save(false);
		$customer = new Customer();
		$customer->setAttributes(array('email' => 'user2@example.com', 'name' => 'user2', 'address' => 'address2', 'status' => 1), false);
		$customer->save(false);
		$customer = new Customer();
		$customer->setAttributes(array('email' => 'user3@example.com', 'name' => 'user3', 'address' => 'address3', 'status' => 2), false);
		$customer->save(false);

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

		$item = new Item();
		$item->setAttributes(array('name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1), false);
		$item->save(false);
		$item = new Item();
		$item->setAttributes(array('name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1), false);
		$item->save(false);
		$item = new Item();
		$item->setAttributes(array('name' => 'Ice Age', 'category_id' => 2), false);
		$item->save(false);
		$item = new Item();
		$item->setAttributes(array('name' => 'Toy Story', 'category_id' => 2), false);
		$item->save(false);
		$item = new Item();
		$item->setAttributes(array('name' => 'Cars', 'category_id' => 2), false);
		$item->save(false);

		$order = new Order();
		$order->setAttributes(array('customer_id' => 1, 'create_time' => 1325282384, 'total' => 110.0), false);
		$order->save(false);
		$order = new Order();
		$order->setAttributes(array('customer_id' => 2, 'create_time' => 1325334482, 'total' => 33.0), false);
		$order->save(false);
		$order = new Order();
		$order->setAttributes(array('customer_id' => 2, 'create_time' => 1325502201, 'total' => 40.0), false);
		$order->save(false);

		$orderItem = new OrderItem();
		$orderItem->setAttributes(array('order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0), false);
		$orderItem->save(false);
		$orderItem = new OrderItem();
		$orderItem->setAttributes(array('order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0), false);
		$orderItem->save(false);
		$orderItem = new OrderItem();
		$orderItem->setAttributes(array('order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0), false);
		$orderItem->save(false);
		$orderItem = new OrderItem();
		$orderItem->setAttributes(array('order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0), false);
		$orderItem->save(false);
		$orderItem = new OrderItem();
		$orderItem->setAttributes(array('order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0), false);
		$orderItem->save(false);
		$orderItem = new OrderItem();
		$orderItem->setAttributes(array('order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0), false);
		$orderItem->save(false);
	}

	public function testFind()
	{
		// find one
		$result = Customer::find();
		$this->assertTrue($result instanceof ActiveQuery);
		$customer = $result->one();
		$this->assertTrue($customer instanceof Customer);

		// find all
		$customers = Customer::find()->all();
		$this->assertEquals(3, count($customers));
		$this->assertTrue($customers[0] instanceof Customer);
		$this->assertTrue($customers[1] instanceof Customer);
		$this->assertTrue($customers[2] instanceof Customer);

		// find by a single primary key
		$customer = Customer::find(2);
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
118 119 120 121
		$customer = Customer::find(5);
		$this->assertNull($customer);

		// query scalar
122
		$customerName = Customer::find()->where(array('id' => 2))->scalar('name');
123
		$this->assertEquals('user2', $customerName);
124 125 126 127 128 129 130 131 132

		// find by column values
		$customer = Customer::find(array('id' => 2));
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
		$customer = Customer::find(array('id' => 5));
		$this->assertNull($customer);

		// find by attributes
133
		$customer = Customer::find()->where(array('name' => 'user2'))->one();
134
		$this->assertTrue($customer instanceof Customer);
135
		$this->assertEquals(2, $customer->id);
136 137

		// find count, sum, average, min, max, scalar
138
		$this->assertEquals(6, Customer::find()->sum('id'));
139 140 141 142 143 144 145 146
		$this->assertEquals(2, Customer::find()->average('id'));
		$this->assertEquals(1, Customer::find()->min('id'));
		$this->assertEquals(3, Customer::find()->max('id'));

		// scope
//		$this->assertEquals(2, Customer::find()->active()->count());

		// asArray
147
		$customer = Customer::find()->where(array('id' => 2))->asArray()->one();
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		$this->assertEquals(array(
			'id' => '2',
			'email' => 'user2@example.com',
			'name' => 'user2',
			'address' => 'address2',
			'status' => '1',
		), $customer);

		// indexBy
		$customers = Customer::find()->indexBy('name')->all();
		$this->assertEquals(3, count($customers));
		$this->assertTrue($customers['user1'] instanceof Customer);
		$this->assertTrue($customers['user2'] instanceof Customer);
		$this->assertTrue($customers['user3'] instanceof Customer);
	}

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
	public function testFindCount()
	{
		$this->assertEquals(3, Customer::find()->count());
		$this->assertEquals(1, Customer::find()->limit(1)->count());
		$this->assertEquals(2, Customer::find()->limit(2)->count());
		$this->assertEquals(1, Customer::find()->offset(2)->limit(2)->count());
	}

	public function testFindLimit()
	{
		// all()
		$customers = Customer::find()->all();
		$this->assertEquals(3, count($customers));

		$customers = Customer::find()->limit(1)->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals('user1', $customers[0]->name);

		$customers = Customer::find()->limit(1)->offset(1)->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals('user2', $customers[0]->name);

		$customers = Customer::find()->limit(1)->offset(2)->all();
		$this->assertEquals(1, count($customers));
		$this->assertEquals('user3', $customers[0]->name);

		$customers = Customer::find()->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()->one();
		$this->assertEquals('user1', $customer->name);

		$customer = Customer::find()->offset(0)->one();
		$this->assertEquals('user1', $customer->name);

		$customer = Customer::find()->offset(1)->one();
		$this->assertEquals('user2', $customer->name);

		$customer = Customer::find()->offset(2)->one();
		$this->assertEquals('user3', $customer->name);

		$customer = Customer::find()->offset(3)->one();
		$this->assertNull($customer);

	}

216 217 218 219 220 221 222 223 224 225 226 227 228 229
	public function testFindComplexCondition()
	{
		$this->assertEquals(2, Customer::find()->where(array('OR', array('id' => 1), array('id' => 2)))->count());
		$this->assertEquals(2, count(Customer::find()->where(array('OR', array('id' => 1), array('id' => 2)))->all()));

		// TODO more conditions
	}

	public function testSum()
	{
		$this->assertEquals(6, OrderItem::find()->count());
		$this->assertEquals(7, OrderItem::find()->sum('quantity'));
	}

230 231 232 233 234 235
	public function testFindColumn()
	{
		$this->assertEquals(array('user1', 'user2', 'user3'), Customer::find()->column('name'));
//		TODO $this->assertEquals(array('user3', 'user2', 'user1'), Customer::find()->orderBy(array('name' => Query::SORT_DESC))->column('name'));
	}

236 237
	public function testExists()
	{
238 239
		$this->assertTrue(Customer::find()->where(array('id' => 2))->exists());
		$this->assertFalse(Customer::find()->where(array('id' => 5))->exists());
240 241
	}

Carsten Brandt committed
242 243 244 245 246 247 248 249 250 251 252
	public function testFindLazy()
	{
		/** @var $customer Customer */
		$customer = Customer::find(2);
		$orders = $customer->orders;
		$this->assertEquals(2, count($orders));

		$orders = $customer->getOrders()->where(array('id' => 3))->all();
		$this->assertEquals(1, count($orders));
		$this->assertEquals(3, $orders[0]->id);
	}
Carsten Brandt committed
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

//	public function testFindEager()
//	{
//		$customers = Customer::find()->with('orders')->all();
//		$this->assertEquals(3, count($customers));
//		$this->assertEquals(1, count($customers[0]->orders));
//		$this->assertEquals(2, count($customers[1]->orders));
//	}

//	public function testFindLazyVia()
//	{
//		/** @var $order Order */
//		$order = Order::find(1);
//		$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);
//
//		$order = Order::find(1);
//		$order->id = 100;
//		$this->assertEquals(array(), $order->items);
//	}

//	public function testFindEagerViaRelation()
//	{
//		$orders = Order::find()->with('items')->all();
//		$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);
//	}
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

/*	public function testFindLazyViaTable()
	{
		/** @var $order Order * /
		$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')->all();
		$this->assertEquals(3, count($orders));

		$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);
	}*/

Carsten Brandt committed
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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 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
//	public function testFindNestedRelation()
//	{
//		$customers = Customer::find()->with('orders', 'orders.items')->all();
//		$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));
//		$this->assertEquals(2, count($customers[0]->orders[0]->items));
//		$this->assertEquals(3, count($customers[1]->orders[0]->items));
//		$this->assertEquals(1, count($customers[1]->orders[1]->items));
//	}

//	public function testLink()
//	{
//		$customer = Customer::find(2);
//		$this->assertEquals(2, count($customer->orders));
//
//		// has many
//		$order = new Order;
//		$order->total = 100;
//		$this->assertTrue($order->isNewRecord);
//		$customer->link('orders', $order);
//		$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 Order;
//		$order->total = 100;
//		$this->assertTrue($order->isNewRecord);
//		$customer = Customer::find(1);
//		$this->assertNull($order->customer);
//		$order->link('customer', $customer);
//		$this->assertFalse($order->isNewRecord);
//		$this->assertEquals(1, $order->customer_id);
//		$this->assertEquals(1, $order->customer->id);
//
//		// via table
//		$order = Order::find(2);
//		$this->assertEquals(0, count($order->books));
//		$orderItem = OrderItem::find(array('order_id' => 2, 'item_id' => 1));
//		$this->assertNull($orderItem);
//		$item = Item::find(1);
//		$order->link('books', $item, array('quantity' => 10, 'subtotal' => 100));
//		$this->assertEquals(1, count($order->books));
//		$orderItem = OrderItem::find(array('order_id' => 2, 'item_id' => 1));
//		$this->assertTrue($orderItem instanceof OrderItem);
//		$this->assertEquals(10, $orderItem->quantity);
//		$this->assertEquals(100, $orderItem->subtotal);
//
//		// via model
//		$order = Order::find(1);
//		$this->assertEquals(2, count($order->items));
//		$this->assertEquals(2, count($order->orderItems));
//		$orderItem = OrderItem::find(array('order_id' => 1, 'item_id' => 3));
//		$this->assertNull($orderItem);
//		$item = Item::find(3);
//		$order->link('items', $item, array('quantity' => 10, 'subtotal' => 100));
//		$this->assertEquals(3, count($order->items));
//		$this->assertEquals(3, count($order->orderItems));
//		$orderItem = OrderItem::find(array('order_id' => 1, 'item_id' => 3));
//		$this->assertTrue($orderItem instanceof OrderItem);
//		$this->assertEquals(10, $orderItem->quantity);
//		$this->assertEquals(100, $orderItem->subtotal);
//	}

//	public function testUnlink()
//	{
//		// has many
//		$customer = Customer::find(2);
//		$this->assertEquals(2, count($customer->orders));
//		$customer->unlink('orders', $customer->orders[1], true);
//		$this->assertEquals(1, count($customer->orders));
//		$this->assertNull(Order::find(3));
//
//		// via model
//		$order = Order::find(2);
//		$this->assertEquals(3, count($order->items));
//		$this->assertEquals(3, count($order->orderItems));
//		$order->unlink('items', $order->items[2], true);
//		$this->assertEquals(2, count($order->items));
//		$this->assertEquals(2, count($order->orderItems));
//
//		// via table
//		$order = Order::find(1);
//		$this->assertEquals(2, count($order->books));
//		$order->unlink('books', $order->books[1], true);
//		$this->assertEquals(1, count($order->books));
//		$this->assertEquals(1, count($order->orderItems));
//	}
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

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

Carsten Brandt committed
430 431
	// TODO test serial column incr

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	public function testUpdate()
	{
		// save
		$customer = Customer::find(2);
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
		$this->assertFalse($customer->isNewRecord);
		$customer->name = 'user2x';
		$customer->save();
		$this->assertEquals('user2x', $customer->name);
		$this->assertFalse($customer->isNewRecord);
		$customer2 = Customer::find(2);
		$this->assertEquals('user2x', $customer2->name);

		// updateCounters
		$pk = array('order_id' => 2, 'item_id' => 4);
		$orderItem = OrderItem::find($pk);
		$this->assertEquals(1, $orderItem->quantity);
		$ret = $orderItem->updateCounters(array('quantity' => -1));
		$this->assertTrue($ret);
		$this->assertEquals(0, $orderItem->quantity);
		$orderItem = OrderItem::find($pk);
		$this->assertEquals(0, $orderItem->quantity);

		// updateAll
		$customer = Customer::find(3);
		$this->assertEquals('user3', $customer->name);
		$ret = Customer::updateAll(array(
			'name' => 'temp',
		), array('id' => 3));
		$this->assertEquals(1, $ret);
		$customer = Customer::find(3);
		$this->assertEquals('temp', $customer->name);

		// updateCounters
		$pk = array('order_id' => 1, 'item_id' => 2);
		$orderItem = OrderItem::find($pk);
		$this->assertEquals(2, $orderItem->quantity);
		$ret = OrderItem::updateAllCounters(array(
			'quantity' => 3,
			'subtotal' => -10,
		), $pk);
		$this->assertEquals(1, $ret);
		$orderItem = OrderItem::find($pk);
		$this->assertEquals(5, $orderItem->quantity);
		$this->assertEquals(30, $orderItem->subtotal);
	}

	public function testDelete()
	{
		// delete
		$customer = Customer::find(2);
		$this->assertTrue($customer instanceof Customer);
		$this->assertEquals('user2', $customer->name);
		$customer->delete();
		$customer = Customer::find(2);
		$this->assertNull($customer);

		// deleteAll
		$customers = Customer::find()->all();
		$this->assertEquals(2, count($customers));
		$ret = Customer::deleteAll();
		$this->assertEquals(2, $ret);
		$customers = Customer::find()->all();
		$this->assertEquals(0, count($customers));
	}
}