PageRenderTime 65ms CodeModel.GetById 1ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/framework/db/ar/CActiveRecordTest.php

https://bitbucket.org/penkoh/yii
PHP | 1404 lines | 1128 code | 204 blank | 72 comment | 3 complexity | d6c662445ba3de67ebf100267ce81d8e MD5 | raw file
   1<?php
   2
   3Yii::import('system.db.CDbConnection');
   4Yii::import('system.db.ar.CActiveRecord');
   5
   6require_once(dirname(__FILE__).'/../data/models.php');
   7
   8class CActiveRecordTest extends CTestCase
   9{
  10	protected $backupStaticAttributes = true;
  11
  12	private $_connection;
  13
  14	protected function setUp()
  15	{
  16		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
  17			$this->markTestSkipped('PDO and SQLite extensions are required.');
  18
  19		$this->_connection=new CDbConnection('sqlite::memory:');
  20		$this->_connection->active=true;
  21		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/../data/sqlite.sql'));
  22		CActiveRecord::$db=$this->_connection;
  23	}
  24
  25	protected function tearDown()
  26	{
  27		$this->_connection->active=false;
  28	}
  29
  30	public function testModel()
  31	{
  32		$model=Post::model();
  33		$this->assertTrue($model instanceof Post);
  34		$this->assertTrue($model->dbConnection===$this->_connection);
  35		$this->assertTrue($model->dbConnection->active);
  36		$this->assertEquals('posts',$model->tableName());
  37		$this->assertEquals('id',$model->tableSchema->primaryKey);
  38		$this->assertTrue($model->tableSchema->sequenceName==='');
  39		$this->assertEquals(array(),$model->attributeLabels());
  40		$this->assertEquals('Id',$model->getAttributeLabel('id'));
  41		$this->assertEquals('Author Id',$model->getAttributeLabel('author_id'));
  42		$this->assertTrue($model->getActiveRelation('author') instanceof CBelongsToRelation);
  43		$this->assertTrue($model->tableSchema instanceof CDbTableSchema);
  44		$this->assertTrue($model->commandBuilder instanceof CDbCommandBuilder);
  45		$this->assertTrue($model->hasAttribute('id'));
  46		$this->assertFalse($model->hasAttribute('comments'));
  47		$this->assertFalse($model->hasAttribute('foo'));
  48		$this->assertEquals(array('id'=>null,'title'=>null,'create_time'=>null,'author_id'=>null,'content'=>null),$model->attributes);
  49
  50		$post=new Post;
  51		$this->assertNull($post->id);
  52		$this->assertNull($post->title);
  53		$post->setAttributes(array('id'=>3,'title'=>'test title'));
  54		$this->assertNull($post->id);
  55		$this->assertEquals('test title',$post->title);
  56	}
  57
  58	public function testFind()
  59	{
  60		// test find() with various parameters
  61		$post=Post::model()->find();
  62		$this->assertTrue($post instanceof Post);
  63		$this->assertEquals(1,$post->id);
  64
  65		$post=Post::model()->find('id=5');
  66		$this->assertTrue($post instanceof Post);
  67		$this->assertEquals(5,$post->id);
  68
  69		$post=Post::model()->find('id=:id',array(':id'=>2));
  70		$this->assertTrue($post instanceof Post);
  71		$this->assertEquals(2,$post->id);
  72
  73		$post=Post::model()->find(array('condition'=>'id=:id','params'=>array(':id'=>3)));
  74		$this->assertTrue($post instanceof Post);
  75		$this->assertEquals(3,$post->id);
  76
  77		// test find() without result
  78		$post=Post::model()->find('id=6');
  79		$this->assertNull($post);
  80
  81		// test findAll() with various parameters
  82		$posts=Post::model()->findAll();
  83		$this->assertEquals(5,count($posts));
  84		$this->assertTrue($posts[3] instanceof Post);
  85		$this->assertEquals(4,$posts[3]->id);
  86
  87		$posts=Post::model()->findAll(new CDbCriteria(array('limit'=>3,'offset'=>1)));
  88		$this->assertEquals(3,count($posts));
  89		$this->assertTrue($posts[2] instanceof Post);
  90		$this->assertEquals(4,$posts[2]->id);
  91
  92		// test findAll() without result
  93		$posts=Post::model()->findAll('id=6');
  94		$this->assertTrue($posts===array());
  95
  96		// test findByPk
  97		$post=Post::model()->findByPk(2);
  98		$this->assertEquals(2,$post->id);
  99
 100		$post=Post::model()->findByPk(array(3,2));
 101		$this->assertEquals(2,$post->id);
 102
 103		$post=Post::model()->findByPk(array());
 104		$this->assertNull($post);
 105
 106		$post=Post::model()->findByPk(null);
 107		$this->assertNull($post);
 108
 109		$post=Post::model()->findByPk(6);
 110		$this->assertNull($post);
 111
 112		// test findAllByPk
 113		$posts=Post::model()->findAllByPk(2);
 114		$this->assertEquals(1,count($posts));
 115		$this->assertEquals(2,$posts[0]->id);
 116
 117		$posts=Post::model()->findAllByPk(array(4,3,2),'id<4');
 118		$this->assertEquals(2,count($posts));
 119		$this->assertEquals(2,$posts[0]->id);
 120		$this->assertEquals(3,$posts[1]->id);
 121
 122		$posts=Post::model()->findAllByPk(array());
 123		$this->assertTrue($posts===array());
 124
 125		// test findByAttributes
 126		$post=Post::model()->findByAttributes(array('author_id'=>2),array('order'=>'id DESC'));
 127		$this->assertEquals(4,$post->id);
 128
 129		// test findAllByAttributes
 130		$posts=Post::model()->findAllByAttributes(array('author_id'=>2));
 131		$this->assertEquals(3,count($posts));
 132
 133		// test findBySql
 134		$post=Post::model()->findBySql('select * from posts where id=:id',array(':id'=>2));
 135		$this->assertEquals(2,$post->id);
 136
 137		// test findAllBySql
 138		$posts=Post::model()->findAllBySql('select * from posts where id>:id',array(':id'=>2));
 139		$this->assertEquals(3,count($posts));
 140
 141		// test count
 142		$this->assertEquals(5,Post::model()->count());
 143		$this->assertEquals(3,Post::model()->count(array('condition'=>'id>2')));
 144
 145		// test countBySql
 146		$this->assertEquals(1,Post::model()->countBySql('select id from posts limit 1'));
 147
 148		// test exists
 149		$this->assertTrue(Post::model()->exists('id=:id',array(':id'=>1)));
 150		$this->assertFalse(Post::model()->exists('id=:id',array(':id'=>6)));
 151	}
 152
 153	public function testInsert()
 154	{
 155		$post=new Post;
 156		$this->assertEquals(array('id'=>null,'title'=>null,'create_time'=>null,'author_id'=>null,'content'=>null),$post->attributes);
 157		$post->title='test post 1';
 158		$post->create_time=time();
 159		$post->author_id=1;
 160		$post->content='test post content 1';
 161		$this->assertTrue($post->isNewRecord);
 162		$this->assertNull($post->id);
 163		$this->assertTrue($post->save());
 164		$this->assertEquals(array(
 165			'id'=>6,
 166			'title'=>'test post 1',
 167			'create_time'=>$post->create_time,
 168			'author_id'=>1,
 169			'content'=>'test post content 1'),$post->attributes);
 170		$this->assertFalse($post->isNewRecord);
 171		$this->assertEquals($post->attributes,Post::model()->findByPk($post->id)->attributes);
 172	}
 173
 174	public function testUpdate()
 175	{
 176		// test save
 177		$post=Post::model()->findByPk(1);
 178		$this->assertFalse($post->isNewRecord);
 179		$this->assertEquals('post 1',$post->title);
 180		$post->title='test post 1';
 181		$this->assertTrue($post->save());
 182		$this->assertFalse($post->isNewRecord);
 183		$this->assertEquals('test post 1',$post->title);
 184		$this->assertEquals('test post 1',Post::model()->findByPk(1)->title);
 185
 186		// test updateByPk
 187		$this->assertEquals(2,Post::model()->updateByPk(array(4,5),array('title'=>'test post')));
 188		$this->assertEquals('post 2',Post::model()->findByPk(2)->title);
 189		$this->assertEquals('test post',Post::model()->findByPk(4)->title);
 190		$this->assertEquals('test post',Post::model()->findByPk(5)->title);
 191
 192		// test updateAll
 193		$this->assertEquals(1,Post::model()->updateAll(array('title'=>'test post'),'id=1'));
 194		$this->assertEquals('test post',Post::model()->findByPk(1)->title);
 195
 196		// test updateCounters
 197		$this->assertEquals(2,Post::model()->findByPk(2)->author_id);
 198		$this->assertEquals(2,Post::model()->findByPk(3)->author_id);
 199		$this->assertEquals(2,Post::model()->findByPk(4)->author_id);
 200		$this->assertEquals(3,Post::model()->updateCounters(array('author_id'=>-1),'id>2'));
 201		$this->assertEquals(2,Post::model()->findByPk(2)->author_id);
 202		$this->assertEquals(1,Post::model()->findByPk(3)->author_id);
 203	}
 204
 205	public function testSaveCounters()
 206	{
 207		$post=Post::model()->findByPk(2);
 208		$this->assertEquals(2, $post->author_id);
 209		$result=$post->saveCounters(array('author_id'=>-1));
 210		$this->assertTrue($result);
 211		$this->assertEquals(1, $post->author_id);
 212		$this->assertEquals(1, Post::model()->findByPk(2)->author_id);
 213		$this->assertEquals(2, Post::model()->findByPk(3)->author_id);
 214	}
 215
 216	public function testDelete()
 217	{
 218		$post=Post::model()->findByPk(1);
 219		$this->assertTrue($post->delete());
 220		$this->assertNull(Post::model()->findByPk(1));
 221
 222		$this->assertTrue(Post::model()->findByPk(2) instanceof Post);
 223		$this->assertTrue(Post::model()->findByPk(3) instanceof Post);
 224		$this->assertEquals(2,Post::model()->deleteByPk(array(2,3)));
 225		$this->assertNull(Post::model()->findByPk(2));
 226		$this->assertNull(Post::model()->findByPk(3));
 227
 228		$this->assertTrue(Post::model()->findByPk(5) instanceof Post);
 229		$this->assertEquals(1,Post::model()->deleteAll('id=5'));
 230		$this->assertNull(Post::model()->findByPk(5));
 231	}
 232
 233	public function testRefresh()
 234	{
 235		$post=Post::model()->findByPk(1);
 236		$post2=Post::model()->findByPk(1);
 237		$post2->title='new post';
 238		$post2->save();
 239		$this->assertEquals('post 1',$post->title);
 240		$this->assertTrue($post->refresh());
 241		$this->assertEquals('new post',$post->title);
 242
 243		$post = new Post();
 244		$this->assertFalse($post->refresh());
 245		$post->id = 1;
 246		$this->assertTrue($post->refresh());
 247		$this->assertEquals('new post',$post->title);
 248	}
 249
 250	public function testEquals()
 251	{
 252		$post=Post::model()->findByPk(1);
 253		$post2=Post::model()->findByPk(1);
 254		$post3=Post::model()->findByPk(3);
 255		$this->assertEquals(1,$post->primaryKey);
 256		$this->assertTrue($post->equals($post2));
 257		$this->assertTrue($post2->equals($post));
 258		$this->assertFalse($post->equals($post3));
 259		$this->assertFalse($post3->equals($post));
 260	}
 261
 262	public function testValidation()
 263	{
 264		$user=new User;
 265		$user->password='passtest';
 266		$this->assertFalse($user->hasErrors());
 267		$this->assertEquals(array(),$user->errors);
 268		$this->assertEquals(array(),$user->getErrors('username'));
 269		$this->assertFalse($user->save());
 270		$this->assertNull($user->id);
 271		$this->assertTrue($user->isNewRecord);
 272		$this->assertTrue($user->hasErrors());
 273		$this->assertTrue($user->hasErrors('username'));
 274		$this->assertTrue($user->hasErrors('email'));
 275		$this->assertFalse($user->hasErrors('password'));
 276		$this->assertEquals(1,count($user->getErrors('username')));
 277		$this->assertEquals(1,count($user->getErrors('email')));
 278		$this->assertEquals(2,count($user->errors));
 279
 280		$user->clearErrors();
 281		$this->assertFalse($user->hasErrors());
 282		$this->assertEquals(array(),$user->errors);
 283	}
 284
 285	public function testCompositeKey()
 286	{
 287		$order=new Order;
 288		$this->assertEquals(array('key1','key2'),$order->tableSchema->primaryKey);
 289		$order=Order::model()->findByPk(array('key1'=>2,'key2'=>1));
 290		$this->assertEquals('order 21',$order->name);
 291		$orders=Order::model()->findAllByPk(array(array('key1'=>2,'key2'=>1),array('key1'=>1,'key2'=>3)));
 292		$this->assertEquals('order 13',$orders[0]->name);
 293		$this->assertEquals('order 21',$orders[1]->name);
 294	}
 295
 296	public function testDefault()
 297	{
 298		$type=new ComplexType;
 299		$this->assertEquals(1,$type->int_col2);
 300		$this->assertEquals('something',$type->char_col2);
 301		$this->assertEquals(1.23,$type->float_col2);
 302		$this->assertEquals(33.22,$type->numeric_col);
 303		$this->assertEquals(123,$type->time);
 304		$this->assertNull($type->bool_col);
 305		$this->assertTrue($type->bool_col2);
 306	}
 307
 308	public function testPublicAttribute()
 309	{
 310		$post=new PostExt;
 311		$this->assertEquals(array('id'=>null,'title'=>'default title','create_time'=>null,'author_id'=>null,'content'=>null),$post->attributes);
 312		$post=Post::model()->findByPk(1);
 313		$this->assertEquals(array(
 314			'id'=>1,
 315			'title'=>'post 1',
 316			'create_time'=>100000,
 317			'author_id'=>1,
 318			'content'=>'content 1'),$post->attributes);
 319
 320		$post=new PostExt;
 321		$post->title='test post';
 322		$post->create_time=1000000;
 323		$post->author_id=1;
 324		$post->content='test';
 325		$post->save();
 326		$this->assertEquals(array(
 327			'id'=>6,
 328			'title'=>'test post',
 329			'create_time'=>1000000,
 330			'author_id'=>1,
 331			'content'=>'test'),$post->attributes);
 332	}
 333
 334	public function testLazyRelation()
 335	{
 336		// test belongsTo
 337		$post=Post::model()->findByPk(2);
 338		$this->assertTrue($post->author instanceof User);
 339		$this->assertEquals(array(
 340			'id'=>2,
 341			'username'=>'user2',
 342			'password'=>'pass2',
 343			'email'=>'email2'),$post->author->attributes);
 344
 345		// test hasOne
 346		$post=Post::model()->findByPk(2);
 347		$this->assertTrue($post->firstComment instanceof Comment);
 348		$this->assertEquals(array(
 349			'id'=>4,
 350			'content'=>'comment 4',
 351			'post_id'=>2,
 352			'author_id'=>2),$post->firstComment->attributes);
 353		$post=Post::model()->findByPk(4);
 354		$this->assertNull($post->firstComment);
 355
 356		// test hasMany
 357		$post=Post::model()->findByPk(2);
 358		$this->assertEquals(2,count($post->comments));
 359		$this->assertEquals(array(
 360			'id'=>5,
 361			'content'=>'comment 5',
 362			'post_id'=>2,
 363			'author_id'=>2),$post->comments[0]->attributes);
 364		$this->assertEquals(array(
 365			'id'=>4,
 366			'content'=>'comment 4',
 367			'post_id'=>2,
 368			'author_id'=>2),$post->comments[1]->attributes);
 369		$post=Post::model()->findByPk(4);
 370		$this->assertEquals(array(),$post->comments);
 371
 372		// test manyMany
 373		$post=Post::model()->findByPk(2);
 374		$this->assertEquals(2,count($post->categories));
 375		$this->assertEquals(array(
 376			'id'=>4,
 377			'name'=>'cat 4',
 378			'parent_id'=>1),$post->categories[0]->attributes);
 379		$this->assertEquals(array(
 380			'id'=>1,
 381			'name'=>'cat 1',
 382			'parent_id'=>null),$post->categories[1]->attributes);
 383		$post=Post::model()->findByPk(4);
 384		$this->assertEquals(array(),$post->categories);
 385
 386		// test self join
 387		$category=Category::model()->findByPk(5);
 388		$this->assertEquals(array(),$category->posts);
 389		$this->assertEquals(2,count($category->children));
 390		$this->assertEquals(array(
 391			'id'=>6,
 392			'name'=>'cat 6',
 393			'parent_id'=>5),$category->children[0]->attributes);
 394		$this->assertEquals(array(
 395			'id'=>7,
 396			'name'=>'cat 7',
 397			'parent_id'=>5),$category->children[1]->attributes);
 398		$this->assertTrue($category->parent instanceof Category);
 399		$this->assertEquals(array(
 400			'id'=>1,
 401			'name'=>'cat 1',
 402			'parent_id'=>null),$category->parent->attributes);
 403
 404		$category=Category::model()->findByPk(2);
 405		$this->assertEquals(1,count($category->posts));
 406		$this->assertEquals(array(),$category->children);
 407		$this->assertNull($category->parent);
 408
 409		// test composite key
 410		$order=Order::model()->findByPk(array('key1'=>1,'key2'=>2));
 411		$this->assertEquals(2,count($order->items));
 412		$order=Order::model()->findByPk(array('key1'=>2,'key2'=>1));
 413		$this->assertEquals(0,count($order->items));
 414		$item=Item::model()->findByPk(4);
 415		$this->assertTrue($item->order instanceof Order);
 416		$this->assertEquals(array(
 417			'key1'=>2,
 418			'key2'=>2,
 419			'name'=>'order 22'),$item->order->attributes);
 420	}
 421
 422	public function testEagerRelation2()
 423	{
 424		$post=Post::model()->with('author','firstComment','comments','categories')->findByPk(2);
 425	}
 426
 427	private function checkEagerLoadedModel($post)
 428	{
 429		$this->assertEquals(array(
 430			'id'=>2,
 431			'username'=>'user2',
 432			'password'=>'pass2',
 433			'email'=>'email2'),$post->author->attributes);
 434		$this->assertTrue($post->firstComment instanceof Comment);
 435		$this->assertEquals(array(
 436			'id'=>4,
 437			'content'=>'comment 4',
 438			'post_id'=>2,
 439			'author_id'=>2),$post->firstComment->attributes);
 440		$this->assertEquals(2,count($post->comments));
 441		$this->assertEquals(array(
 442			'id'=>5,
 443			'content'=>'comment 5',
 444			'post_id'=>2,
 445			'author_id'=>2),$post->comments[0]->attributes);
 446		$this->assertEquals(array(
 447			'id'=>4,
 448			'content'=>'comment 4',
 449			'post_id'=>2,
 450			'author_id'=>2),$post->comments[1]->attributes);
 451		$this->assertEquals(2,count($post->categories));
 452		$this->assertEquals(array(
 453			'id'=>4,
 454			'name'=>'cat 4',
 455			'parent_id'=>1),$post->categories[0]->attributes);
 456		$this->assertEquals(array(
 457			'id'=>1,
 458			'name'=>'cat 1',
 459			'parent_id'=>null),$post->categories[1]->attributes);
 460	}
 461
 462	public function testEagerRelation()
 463	{
 464		$post=Post::model()->with('author','firstComment','comments','categories')->findByPk(2);
 465		$this->checkEagerLoadedModel($post);
 466		$post=Post::model()->findByPk(2,array(
 467			'with'=>array('author','firstComment','comments','categories'),
 468		));
 469		$this->checkEagerLoadedModel($post);
 470
 471		$post=Post::model()->with('author','firstComment','comments','categories')->findByPk(4);
 472		$this->assertEquals(array(
 473			'id'=>2,
 474			'username'=>'user2',
 475			'password'=>'pass2',
 476			'email'=>'email2'),$post->author->attributes);
 477		$this->assertNull($post->firstComment);
 478		$this->assertEquals(array(),$post->comments);
 479		$this->assertEquals(array(),$post->categories);
 480	}
 481
 482	public function testLazyRecursiveRelation()
 483	{
 484		$post=PostExt::model()->findByPk(2);
 485		$this->assertEquals(2,count($post->comments));
 486		$this->assertTrue($post->comments[0]->post instanceof Post);
 487		$this->assertTrue($post->comments[1]->post instanceof Post);
 488		$this->assertTrue($post->comments[0]->author instanceof User);
 489		$this->assertTrue($post->comments[1]->author instanceof User);
 490		$this->assertEquals(3,count($post->comments[0]->author->posts));
 491		$this->assertEquals(3,count($post->comments[1]->author->posts));
 492		$this->assertTrue($post->comments[0]->author->posts[1]->author instanceof User);
 493
 494		// test self join
 495		$category=Category::model()->findByPk(1);
 496		$this->assertEquals(2,count($category->nodes));
 497		$this->assertTrue($category->nodes[0]->parent instanceof Category);
 498		$this->assertTrue($category->nodes[1]->parent instanceof Category);
 499		$this->assertEquals(0,count($category->nodes[0]->children));
 500		$this->assertEquals(2,count($category->nodes[1]->children));
 501	}
 502
 503	public function testEagerRecursiveRelation()
 504	{
 505		//$post=Post::model()->with(array('comments'=>'author','categories'))->findByPk(2);
 506		$post=Post::model()->with('comments.author','categories')->findByPk(2);
 507		$this->assertEquals(2,count($post->comments));
 508		$this->assertEquals(2,count($post->categories));
 509
 510		$posts=PostExt::model()->with('comments')->findAll();
 511		$this->assertEquals(5,count($posts));
 512	}
 513
 514	public function testRelationWithCondition()
 515	{
 516		$posts=Post::model()->with('comments')->findAllByPk(array(2,3,4),array('order'=>'t.id'));
 517		$this->assertEquals(3,count($posts));
 518		$this->assertEquals(2,count($posts[0]->comments));
 519		$this->assertEquals(4,count($posts[1]->comments));
 520		$this->assertEquals(0,count($posts[2]->comments));
 521
 522		$post=Post::model()->with('comments')->findByAttributes(array('id'=>2));
 523		$this->assertTrue($post instanceof Post);
 524		$this->assertEquals(2,count($post->comments));
 525		$posts=Post::model()->with('comments')->findAllByAttributes(array('id'=>2));
 526		$this->assertEquals(1,count($posts));
 527
 528		$post=Post::model()->with('comments')->findBySql('select * from posts where id=:id',array(':id'=>2));
 529		$this->assertTrue($post instanceof Post);
 530		$posts=Post::model()->with('comments')->findAllBySql('select * from posts where id=:id1 OR id=:id2',array(':id1'=>2,':id2'=>3));
 531		$this->assertEquals(2,count($posts));
 532
 533		$post=Post::model()->with('comments','author')->find('t.id=:id',array(':id'=>2));
 534		$this->assertTrue($post instanceof Post);
 535
 536		$posts=Post::model()->with('comments','author')->findAll(array(
 537			'select'=>'title',
 538			'condition'=>'t.id=:id',
 539			'limit'=>1,
 540			'offset'=>0,
 541			'order'=>'t.title',
 542			'group'=>'t.id',
 543			'params'=>array(':id'=>2)));
 544		$this->assertTrue($posts[0] instanceof Post);
 545
 546		$posts=Post::model()->with('comments','author')->findAll(array(
 547			'select'=>'title',
 548			'condition'=>'t.id=:id',
 549			'limit'=>1,
 550			'offset'=>2,
 551			'order'=>'t.title',
 552			'params'=>array(':id'=>2)));
 553		$this->assertTrue($posts===array());
 554	}
 555
 556	public function testRelationWithColumnAlias()
 557	{
 558		$users=User::model()->with('posts')->findAll(array(
 559			'select'=>'id, username AS username2',
 560			'order'=>'username2',
 561		));
 562
 563		$this->assertEquals(4,count($users));
 564		$this->assertEquals($users[1]->username,null);
 565		$this->assertEquals($users[1]->username2,'user2');
 566	}
 567
 568	public function testRelationalWithoutFK()
 569	{
 570		$users=UserNoFk::model()->with('posts')->findAll();
 571		$this->assertEquals(4,count($users));
 572		$this->assertEquals(3,count($users[1]->posts));
 573
 574		$posts=PostNoFk::model()->with('author')->findAll();
 575		$this->assertEquals(5,count($posts));
 576		$this->assertTrue($posts[2]->author instanceof UserNoFk);
 577	}
 578
 579	public function testRelationWithNewRecord()
 580	{
 581		$user=new User;
 582		$posts=$user->posts;
 583		$this->assertTrue(is_array($posts) && empty($posts));
 584
 585		$post=new Post;
 586		$author=$post->author;
 587		$this->assertNull($author);
 588	}
 589
 590	public function testRelationWithDynamicCondition()
 591	{
 592		$user=User::model()->with('posts')->findByPk(2);
 593		$this->assertEquals($user->posts[0]->id,2);
 594		$this->assertEquals($user->posts[1]->id,3);
 595		$this->assertEquals($user->posts[2]->id,4);
 596		$user=User::model()->with(array('posts'=>array('order'=>'posts.id DESC')))->findByPk(2);
 597		$this->assertEquals($user->posts[0]->id,4);
 598		$this->assertEquals($user->posts[1]->id,3);
 599		$this->assertEquals($user->posts[2]->id,2);
 600	}
 601
 602	public function testEagerTogetherRelation()
 603	{
 604		$post=Post::model()->with('author','firstComment','comments','categories')->findByPk(2);
 605		$comments=$post->comments;
 606		$this->assertEquals(array(
 607			'id'=>2,
 608			'username'=>'user2',
 609			'password'=>'pass2',
 610			'email'=>'email2'),$post->author->attributes);
 611		$this->assertTrue($post->firstComment instanceof Comment);
 612		$this->assertEquals(array(
 613			'id'=>4,
 614			'content'=>'comment 4',
 615			'post_id'=>2,
 616			'author_id'=>2),$post->firstComment->attributes);
 617		$this->assertEquals(2,count($post->comments));
 618		$this->assertEquals(array(
 619			'id'=>5,
 620			'content'=>'comment 5',
 621			'post_id'=>2,
 622			'author_id'=>2),$post->comments[0]->attributes);
 623		$this->assertEquals(array(
 624			'id'=>4,
 625			'content'=>'comment 4',
 626			'post_id'=>2,
 627			'author_id'=>2),$post->comments[1]->attributes);
 628		$this->assertEquals(2,count($post->categories));
 629		$this->assertEquals(array(
 630			'id'=>4,
 631			'name'=>'cat 4',
 632			'parent_id'=>1),$post->categories[0]->attributes);
 633		$this->assertEquals(array(
 634			'id'=>1,
 635			'name'=>'cat 1',
 636			'parent_id'=>null),$post->categories[1]->attributes);
 637
 638		$post=Post::model()->with('author','firstComment','comments','categories')->findByPk(4);
 639		$this->assertEquals(array(
 640			'id'=>2,
 641			'username'=>'user2',
 642			'password'=>'pass2',
 643			'email'=>'email2'),$post->author->attributes);
 644		$this->assertNull($post->firstComment);
 645		$this->assertEquals(array(),$post->comments);
 646		$this->assertEquals(array(),$post->categories);
 647	}
 648
 649	public function testRelationalCount()
 650	{
 651		$count=Post::model()->with('author','firstComment','comments','categories')->count();
 652		$this->assertEquals(5,$count);
 653
 654		$count=Post::model()->count(array('with'=>array('author','firstComment','comments','categories')));
 655		$this->assertEquals(5,$count);
 656
 657		$count=Post::model()->with('author','firstComment','comments','categories')->count('t.id=4');
 658		$this->assertEquals(1,$count);
 659
 660		$count=Post::model()->with('author','firstComment','comments','categories')->count('t.id=14');
 661		$this->assertEquals(0,$count);
 662	}
 663
 664	public function testRelationalStat()
 665	{
 666		$users=User::model()->with('postCount')->findAll();
 667		$this->assertEquals(4,count($users));
 668		$this->assertEquals(1,$users[0]->postCount);
 669		$this->assertEquals(3,$users[1]->postCount);
 670		$this->assertEquals(1,$users[2]->postCount);
 671
 672		$users=User::model()->findAll();
 673		$this->assertEquals(4,count($users));
 674		$this->assertEquals(1,$users[0]->postCount);
 675		$this->assertEquals(3,$users[1]->postCount);
 676		$this->assertEquals(1,$users[2]->postCount);
 677
 678		$orders=Order::model()->with('itemCount')->findAll();
 679		$this->assertEquals(4,count($orders));
 680		$this->assertEquals(2,$orders[0]->itemCount);
 681		$this->assertEquals(1,$orders[1]->itemCount);
 682		$this->assertEquals(0,$orders[2]->itemCount);
 683		$this->assertEquals(2,$orders[3]->itemCount);
 684
 685		$orders=Order::model()->findAll();
 686		$this->assertEquals(4,count($orders));
 687		$this->assertEquals(2,$orders[0]->itemCount);
 688		$this->assertEquals(1,$orders[1]->itemCount);
 689		$this->assertEquals(0,$orders[2]->itemCount);
 690		$this->assertEquals(2,$orders[3]->itemCount);
 691
 692		$categories=Category::model()->with('postCount')->findAll();
 693		$this->assertEquals(7,count($categories));
 694		$this->assertEquals(3,$categories[0]->postCount);
 695		$this->assertEquals(1,$categories[1]->postCount);
 696		$this->assertEquals(1,$categories[2]->postCount);
 697		$this->assertEquals(1,$categories[3]->postCount);
 698		$this->assertEquals(0,$categories[4]->postCount);
 699		$this->assertEquals(0,$categories[5]->postCount);
 700		$this->assertEquals(0,$categories[6]->postCount);
 701
 702		$categories=Category::model()->findAll();
 703		$this->assertEquals(7,count($categories));
 704		$this->assertEquals(3,$categories[0]->postCount);
 705		$this->assertEquals(1,$categories[1]->postCount);
 706		$this->assertEquals(1,$categories[2]->postCount);
 707		$this->assertEquals(1,$categories[3]->postCount);
 708		$this->assertEquals(0,$categories[4]->postCount);
 709		$this->assertEquals(0,$categories[5]->postCount);
 710		$this->assertEquals(0,$categories[6]->postCount);
 711
 712		$users=User::model()->with('postCount','posts.commentCount')->findAll();
 713		$this->assertEquals(4,count($users));
 714	}
 715
 716	public function testLazyLoadingWithConditions()
 717	{
 718		$user=User::model()->findByPk(2);
 719		$posts=$user->posts;
 720		$this->assertEquals(3,count($posts));
 721		$posts=$user->posts(array('condition'=>'posts.id>=3', 'alias'=>'posts'));
 722		$this->assertEquals(2,count($posts));
 723	}
 724
 725	public function testDuplicateLazyLoadingBug()
 726	{
 727		$user=User::model()->with(array(
 728			'posts'=>array('on'=>'posts.id=-1')
 729		))->findByPk(1);
 730		// with the bug, an eager loading for 'posts' would be trigger in the following
 731		// and result with non-empty posts
 732		$this->assertTrue($user->posts===array());
 733	}
 734
 735	public function testTogether()
 736	{
 737		// test without together
 738		$users=UserNoTogether::model()->with('posts.comments')->findAll();
 739		$postCount=0;
 740		$commentCount=0;
 741		foreach($users as $user)
 742		{
 743			$postCount+=count($user->posts);
 744			foreach($posts=$user->posts as $post)
 745				$commentCount+=count($post->comments);
 746		}
 747		$this->assertEquals(4,count($users));
 748		$this->assertEquals(5,$postCount);
 749		$this->assertEquals(10,$commentCount);
 750
 751		// test with together
 752		$users=UserNoTogether::model()->with('posts.comments')->together()->findAll();
 753		$postCount=0;
 754		$commentCount=0;
 755		foreach($users as $user)
 756		{
 757			$postCount+=count($user->posts);
 758			foreach($posts=$user->posts as $post)
 759				$commentCount+=count($post->comments);
 760		}
 761		$this->assertEquals(3,count($users));
 762		$this->assertEquals(4,$postCount);
 763		$this->assertEquals(10,$commentCount);
 764	}
 765
 766	public function testTogetherWithOption()
 767	{
 768		// test with together off option
 769		$users=User::model()->with(array(
 770			'posts'=>array(
 771				'with'=>array(
 772					'comments'=>array(
 773						'joinType'=>'INNER JOIN',
 774						'together'=>false,
 775					),
 776				),
 777				'joinType'=>'INNER JOIN',
 778				'together'=>false,
 779			),
 780		))->findAll();
 781
 782		$postCount=0;
 783		$commentCount=0;
 784		foreach($users as $user)
 785		{
 786			$postCount+=count($user->posts);
 787			foreach($posts=$user->posts as $post)
 788				$commentCount+=count($post->comments);
 789		}
 790		$this->assertEquals(4,count($users));
 791		$this->assertEquals(5,$postCount);
 792		$this->assertEquals(10,$commentCount);
 793
 794		// test with together on option
 795		$users=User::model()->with(array(
 796			'posts'=>array(
 797				'with'=>array(
 798					'comments'=>array(
 799						'joinType'=>'INNER JOIN',
 800						'together'=>true,
 801					),
 802				),
 803				'joinType'=>'INNER JOIN',
 804				'together'=>true,
 805			),
 806		))->findAll();
 807
 808		$postCount=0;
 809		$commentCount=0;
 810		foreach($users as $user)
 811		{
 812			$postCount+=count($user->posts);
 813			foreach($posts=$user->posts as $post)
 814				$commentCount+=count($post->comments);
 815		}
 816		$this->assertEquals(3,count($users));
 817		$this->assertEquals(4,$postCount);
 818		$this->assertEquals(10,$commentCount);
 819	}
 820
 821	public function testCountByAttributes()
 822	{
 823		$n=Post::model()->countByAttributes(array('author_id'=>2));
 824		$this->assertEquals(3,$n);
 825
 826	}
 827
 828	public function testScopes()
 829	{
 830		$models1=Post::model()->post23()->findAll();
 831		$models2=Post::model()->findAll(array('scopes'=>'post23'));
 832
 833		foreach(array($models1,$models2) as $models)
 834		{
 835			$this->assertEquals(2,count($models));
 836			$this->assertEquals(2,$models[0]->id);
 837			$this->assertEquals(3,$models[1]->id);
 838		}
 839
 840		$model1=Post::model()->post23()->find();
 841		$model2=Post::model()->find(array('scopes'=>'post23'));
 842
 843		foreach(array($model1,$model2) as $model)
 844			$this->assertEquals(2,$model->id);
 845
 846		$models1=Post::model()->post23()->post3()->findAll();
 847		$models2=Post::model()->findAll(array('scopes'=>array('post23','post3')));
 848
 849		foreach(array($models1,$models2) as $models)
 850		{
 851			$this->assertEquals(1,count($models));
 852			$this->assertEquals(3,$models[0]->id);
 853		}
 854
 855		$models1=Post::model()->post23()->findAll('id=3');
 856		$models2=Post::model()->post23()->findAll(array('condition'=>'id=3','scopes'=>'post23'));
 857
 858		foreach(array($models1,$models2) as $models)
 859		{
 860			$this->assertEquals(1,count($models));
 861			$this->assertEquals(3,$models[0]->id);
 862		}
 863
 864		$models1=Post::model()->recent()->with('author')->findAll();
 865		$models2=Post::model()->with('author')->findAll(array('scopes'=>'recent'));
 866		$models3=Post::model()->with('author')->findAll(array('scopes'=>array('recent')));
 867		$models4=Post::model()->with('author')->findAll(array('scopes'=>array(array('recent'=>array()))));
 868
 869		foreach(array($models1,$models2,$models3,$models4) as $models)
 870		{
 871			$this->assertEquals(5,count($models));
 872			$this->assertEquals(5,$models[0]->id);
 873			$this->assertEquals(4,$models[1]->id);
 874		}
 875
 876		$models1=Post::model()->recent(3)->findAll();
 877		$models2=Post::model()->findAll(array('scopes'=>array('recent'=>3)));
 878		$models3=Post::model()->findAll(array('scopes'=>array(array('recent'=>3))));
 879
 880		foreach(array($models1,$models2,$models3) as $models)
 881		{
 882			$this->assertEquals(3,count($models));
 883			$this->assertEquals(5,$models[0]->id);
 884			$this->assertEquals(4,$models[1]->id);
 885		}
 886
 887		//default scope
 888		$models=PostSpecial::model()->findAll();
 889		$this->assertEquals(2,count($models));
 890		$this->assertEquals(2,$models[0]->id);
 891		$this->assertEquals(3,$models[1]->id);
 892
 893		//default scope + scope
 894		$models1=PostSpecial::model()->desc()->findAll();
 895		$models2=PostSpecial::model()->findAll(array('scopes'=>'desc'));
 896
 897		foreach(array($models1,$models2) as $models)
 898		{
 899			$this->assertEquals(2,count($models));
 900			$this->assertEquals(3,$models[0]->id);
 901			$this->assertEquals(2,$models[1]->id);
 902		}
 903
 904		//behavior scope
 905		$models=Post::model()->findAll(array('scopes'=>'behaviorPost23'));
 906		$this->assertEquals(2,count($models));
 907		$this->assertEquals(2,$models[0]->id);
 908		$this->assertEquals(3,$models[1]->id);
 909
 910		//behavior parametrized scope
 911		$models=Post::model()->findAll(array('scopes'=>array('behaviorRecent'=>3)));
 912		$this->assertEquals(3,count($models));
 913		$this->assertEquals(5,$models[0]->id);
 914		$this->assertEquals(4,$models[1]->id);
 915	}
 916
 917	public function testScopeWithRelations()
 918	{
 919		$user1=User::model()->with('posts:post23')->findByPk(2);
 920		$user2=User::model()->with(array('posts'=>array('scopes'=>'post23')))->findByPk(2);
 921		$user3=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>'post23'))));
 922		//ensure alais overloading work correctly
 923		$user4=User::model()->with(array('posts:post23A'=>array('alias'=>'alias')))->findByPk(2);
 924		$user5=User::model()->with(array('posts'=>array('scopes'=>'post23A','alias'=>'alias')))->findByPk(2);
 925		$user6=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>'post23A','alias'=>'alias'))));
 926
 927		foreach(array($user1,$user2,$user3,$user4,$user5,$user6) as $user)
 928		{
 929			$this->assertEquals(2,count($user->posts));
 930			$this->assertEquals(2,$user->posts[0]->id);
 931			$this->assertEquals(3,$user->posts[1]->id);
 932		}
 933
 934		$user1=User::model()->with(array('posts'=>array('scopes'=>array('p'=>4))))->findByPk(2);
 935		$user2=User::model()->with(array('posts'=>array('scopes'=>array('p'=>array(4)))))->findByPk(2);
 936		$user3=User::model()->with(array('posts'=>array('scopes'=>array(array('p'=>4)))))->findByPk(2);
 937		$user4=User::model()->with(array('posts'=>array('scopes'=>array(array('p'=>array(4))))))->findByPk(2);
 938		$user5=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('p'=>4)))));
 939		$user6=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('p'=>array(4))))));
 940		$user7=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('p'=>4))))));
 941		$user8=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('p'=>array(4)))))));
 942
 943		foreach(array($user1,$user2,$user3,$user4,$user5,$user6,$user7,$user8) as $user)
 944		{
 945			$this->assertEquals(1,count($user->posts));
 946			$this->assertEquals(4,$user->posts[0]->id);
 947		}
 948
 949		$user=UserSpecial::model()->findByPk(2);
 950		$posts=$user->posts;
 951		$this->assertEquals(2,count($posts));
 952		$this->assertEquals(2,$posts[0]->id);
 953		$this->assertEquals(3,$posts[1]->id);
 954
 955		$user=UserSpecial::model()->findByPk(2);
 956		$posts=$user->posts(array('params'=>array(':id1'=>4),'order'=>'posts.id DESC'));
 957		$this->assertEquals(2,count($posts));
 958		$this->assertEquals(4,$posts[0]->id);
 959		$this->assertEquals(3,$posts[1]->id);
 960
 961		$user=User::model()->with('posts:post23')->findByPk(2);
 962		$posts=$user->posts(array('scopes'=>'post23'));
 963		$this->assertEquals(2,count($posts));
 964		$this->assertEquals(2,$posts[0]->id);
 965		$this->assertEquals(3,$posts[1]->id);
 966
 967		//related model behavior scope
 968		$user1=User::model()->with('posts:behaviorPost23')->findByPk(2);
 969		$user2=User::model()->with(array('posts'=>array('scopes'=>'behaviorPost23')))->findByPk(2);
 970		$user3=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>'behaviorPost23'))));
 971
 972		foreach(array($user1,$user2,$user3) as $user)
 973		{
 974			$this->assertEquals(2,count($user->posts));
 975			$this->assertEquals(2,$user->posts[0]->id);
 976			$this->assertEquals(3,$user->posts[1]->id);
 977		}
 978
 979		//related model with behavior parametrized scope
 980		$user1=User::model()->with(array('posts'=>array('scopes'=>array('behaviorP'=>4))))->findByPk(2);
 981		$user2=User::model()->with(array('posts'=>array('scopes'=>array('behaviorP'=>array(4)))))->findByPk(2);
 982		$user3=User::model()->with(array('posts'=>array('scopes'=>array(array('behaviorP'=>4)))))->findByPk(2);
 983		$user4=User::model()->with(array('posts'=>array('scopes'=>array(array('behaviorP'=>array(4))))))->findByPk(2);
 984		$user5=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('behaviorP'=>4)))));
 985		$user6=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('behaviorP'=>array(4))))));
 986		$user7=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('behaviorP'=>4))))));
 987		$user8=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('behaviorP'=>array(4)))))));
 988
 989		foreach(array($user1,$user2,$user3,$user4,$user5,$user6,$user7,$user8) as $user)
 990		{
 991			$this->assertEquals(1,count($user->posts));
 992			$this->assertEquals(4,$user->posts[0]->id);
 993		}
 994
 995		//related model with 'scopes' as relation option
 996		$user=User::model()->with('postsOrderDescFormat1')->findByPk(2);
 997		$this->assertEquals(3,count($user->postsOrderDescFormat1));
 998		$this->assertEquals(array(4,3,2),array(
 999			$user->postsOrderDescFormat1[0]->id,
1000			$user->postsOrderDescFormat1[1]->id,
1001			$user->postsOrderDescFormat1[2]->id,
1002		));
1003		$user=User::model()->with('postsOrderDescFormat2')->findByPk(2);
1004		$this->assertEquals(3,count($user->postsOrderDescFormat2));
1005		$this->assertEquals(array(4,3,2),array(
1006			$user->postsOrderDescFormat2[0]->id,
1007			$user->postsOrderDescFormat2[1]->id,
1008			$user->postsOrderDescFormat2[2]->id,
1009		));
1010	}
1011
1012	public function testResetScope()
1013	{
1014		// resetting named scope
1015		$posts=Post::model()->post23()->resetScope()->findAll();
1016		$this->assertEquals(5,count($posts));
1017
1018		// resetting default scope
1019		$posts=PostSpecial::model()->resetScope()->findAll();
1020		$this->assertEquals(5,count($posts));
1021	}
1022
1023	public function testJoinWithoutSelect()
1024	{
1025        // 1:1 test
1026		$groups=Group::model()->findAll(array(
1027			'with'=>array(
1028				'description'=>array(
1029					'select'=>false,
1030					'joinType'=>'INNER JOIN',
1031				),
1032			),
1033		));
1034
1035		$result=array();
1036		foreach($groups as $group)
1037		{
1038			// there should be nothing in relation
1039			$this->assertFalse($group->hasRelated('description'));
1040			$result[]=array($group->id,$group->name);
1041		}
1042
1043		$this->assertEquals(array(
1044			array(1,'group1'),
1045			array(2,'group2'),
1046			array(3,'group3'),
1047			array(4,'group4'),
1048		),$result);
1049
1050		// 1:M test
1051		$users=User::model()->findAll(array(
1052			'with'=>array(
1053				'roles'=>array(
1054					'select'=>false,
1055					'joinType'=>'INNER JOIN',
1056				),
1057			),
1058		));
1059
1060		$result=array();
1061		foreach($users as $user)
1062		{
1063			// there should be nothing in relation
1064			$this->assertFalse($user->hasRelated('roles'));
1065			$result[]=array($user->id,$user->username,$user->email);
1066		}
1067
1068		$this->assertEquals(array(
1069			array(1,'user1','email1'),
1070			array(2,'user2','email2'),
1071		),$result);
1072	}
1073
1074	public function testHasManyThroughEager()
1075	{
1076		// just bridge
1077		$user=User::model()->with('groups')->findByPk(1);
1078
1079		$result=array();
1080		foreach($user->groups as $group)
1081			$result[]=array($user->username,$group->name);
1082
1083		$this->assertEquals(array(
1084			array('user1','group1'),
1085			array('user1','group2'),
1086		),$result);
1087
1088		// just bridge, base limited
1089		$users=User::model()->with('groups')->findAll(array('limit'=>1));
1090
1091		$result=array();
1092		foreach($users as $user)
1093		{
1094			foreach($user->groups as $group)
1095				$result[]=array($user->username,$group->name);
1096		}
1097
1098		$this->assertEquals(array(
1099			array('user1','group1'),
1100			array('user1','group2'),
1101		),$result);
1102
1103		// 'through' should not clear existing relations defined via short syntax
1104		$user=User::model()->with('groups.description')->findByPk(1);
1105
1106		$result=array();
1107		foreach($user->groups as $group)
1108			$result[]=array($user->username,$group->name,$group->description->name);
1109
1110		$this->assertEquals(array(
1111			array('user1','group1','room1'),
1112			array('user1','group2','room2'),
1113		),$result);
1114
1115		// 'through' should not clear existing with
1116		$user=User::model()->with(array('groups'=>array('with'=>'description')))->findByPk(1);
1117
1118		$result=array();
1119		foreach($user->groups as $group)
1120			$result[]=array($user->username,$group->name,$group->description->name);
1121
1122		$this->assertEquals(array(
1123			array('user1','group1','room1'),
1124			array('user1','group2','room2'),
1125		),$result);
1126
1127		// bridge fields handling
1128		$user=User::model()->with('roles','groups')->findByPk(1);
1129
1130		$result=array();
1131		foreach($user->groups as $group)
1132			$result[]=array($user->username,$group->name);
1133
1134		$this->assertEquals(array(
1135			array('user1','group1'),
1136			array('user1','group2'),
1137		),$result);
1138
1139		$result=array();
1140		foreach($user->roles as $role)
1141			$result[]=array($user->username,$role->name);
1142
1143		$this->assertEquals(array(
1144			array('user1','dev'),
1145			array('user1','user'),
1146		),$result);
1147
1148		// bridge fields handling, another relations order
1149		$user=User::model()->with('groups','roles')->findByPk(1);
1150
1151		$result=array();
1152		foreach($user->groups as $group)
1153			$result[]=array($user->username,$group->name);
1154
1155		$this->assertEquals(array(
1156			array('user1','group1'),
1157			array('user1','group2'),
1158		),$result);
1159
1160		$result=array();
1161		foreach($user->roles as $role)
1162			$result[]=array($user->username,$role->name);
1163
1164		$this->assertEquals(array(
1165			array('user1','dev'),
1166			array('user1','user'),
1167		),$result);
1168
1169		// bridge fields handling, base limited
1170		$users=User::model()->with('roles','groups')->findAll(array('limit'=>1));
1171
1172		$result=array();
1173		foreach($users as $user)
1174		{
1175			foreach($user->groups as $group)
1176				$result[]=array($user->username,$group->name);
1177		}
1178
1179		$this->assertEquals(array(
1180			array('user1','group1'),
1181			array('user1','group2'),
1182		),$result);
1183
1184		$result=array();
1185		foreach($users as $user)
1186		{
1187			foreach($user->roles as $role)
1188				$result[]=array($user->username,$role->name);
1189		}
1190
1191		$this->assertEquals(array(
1192			array('user1','dev'),
1193			array('user1','user'),
1194		),$result);
1195
1196		// nested through
1197		$group=Group::model()->with('comments')->findByPk(1);
1198
1199		$result=array();
1200		foreach($group->comments as $comment)
1201			$result[]=array($group->name,$comment->content);
1202
1203		$this->assertEquals(array(
1204			array('group1','comment 1'),
1205			array('group1','comment 2'),
1206			array('group1','comment 3'),
1207			array('group1','comment 4'),
1208			array('group1','comment 5'),
1209			array('group1','comment 6'),
1210			array('group1','comment 7'),
1211			array('group1','comment 8'),
1212			array('group1','comment 9'),
1213		),$result);
1214
1215		// nested through, base limited
1216		$groups=Group::model()->with('comments')->findAll(array('limit'=>1));
1217
1218		$result=array();
1219		foreach($groups as $group)
1220		{
1221			foreach($group->comments as $comment)
1222				$result[]=array($group->name,$comment->content);
1223		}
1224
1225		$this->assertEquals(array(
1226			array('group1','comment 1'),
1227			array('group1','comment 2'),
1228			array('group1','comment 3'),
1229			array('group1','comment 4'),
1230			array('group1','comment 5'),
1231			array('group1','comment 6'),
1232			array('group1','comment 7'),
1233			array('group1','comment 8'),
1234			array('group1','comment 9'),
1235		),$result);
1236
1237		// self through
1238		$teachers=User::model()->with('students')->findAll();
1239
1240		$result=array();
1241		foreach($teachers as $teacher)
1242		{
1243			foreach($teacher->students as $student)
1244				$result[]=array($teacher->username,$student->username);
1245		}
1246
1247		$this->assertEquals(array(
1248			array('user1','user3'),
1249			array('user2','user4'),
1250		),$result);
1251
1252		// self through, bridge fields handling for right part
1253		$teachers=User::model()->with('mentorships','students')->findAll();
1254
1255		$result=array();
1256		foreach($teachers as $teacher)
1257		{
1258			foreach($teacher->students as $student)
1259				$result[$student->primaryKey]=array('teacher'=>$teacher->username,'student'=>$student->username);
1260
1261			foreach($teacher->mentorships as $mentorship)
1262				$result[$mentorship->student_id]['progress']=$mentorship->progress;
1263		}
1264
1265		$this->assertEquals(array(
1266			3=>array('teacher'=>'user1','student'=>'user3','progress'=>'good'),
1267			4=>array('teacher'=>'user2','student'=>'user4','progress'=>'average'),
1268		),$result);
1269
1270		// self through, base limited
1271		$teachers=User::model()->with('students')->findAll(array('limit'=>1));
1272
1273		$result=array();
1274		foreach($teachers as $teacher)
1275		{
1276			foreach($teacher->students as $student)
1277				$result[]=array($teacher->username,$student->username);
1278		}
1279
1280		$this->assertEquals(array(
1281			array('user1','user3'),
1282		),$result);
1283	}
1284
1285	public function testHasManyThroughLazy()
1286	{
1287		$user=User::model()->findByPk(1);
1288
1289		$result=array();
1290		foreach($user->groups as $group)
1291			$result[]=array($user->username,$group->name);
1292
1293		$this->assertEquals(array(
1294			array('user1','group1'),
1295			array('user1','group2'),
1296		),$result);
1297
1298
1299		$user=User::model()->findByPk(1);
1300
1301		$result=array();
1302		foreach($user->groups(array('with'=>'description')) as $group)
1303			$result[]=array($user->username,$group->name,$group->description->name);
1304
1305		$this->assertEquals(array(
1306			array('user1','group1','room1'),
1307			array('user1','group2','room2'),
1308		),$result);
1309
1310		// nested through
1311		$group=Group::model()->findByPk(1);
1312
1313		$result=array();
1314		foreach($group->comments as $comment)
1315			$result[]=array($group->name,$comment->content);
1316
1317		$this->assertEquals(array(
1318			array('group1','comment 1'),
1319			array('group1','comment 2'),
1320			array('group1','comment 3'),
1321			array('group1','comment 4'),
1322			array('group1','comment 5'),
1323			array('group1','comment 6'),
1324			array('group1','comment 7'),
1325			array('group1','comment 8'),
1326			array('group1','comment 9'),
1327		),$result);
1328
1329		// self through
1330		$teacher=User::model()->findByPk(1);
1331
1332		$result=array();
1333		foreach($teacher->students as $student)
1334			$result[]=array($teacher->username,$student->username);
1335
1336		$this->assertEquals(array(
1337			array('user1','user3'),
1338		),$result);
1339	}
1340
1341	/**
1342	 * @see issue2274
1343	 */
1344	function testMergingWith()
1345	{
1346		User::model()->nonEmptyPosts()->findAll(array(
1347			'with'=>array(
1348        		'posts'=>array(
1349            		'joinType'=>'INNER JOIN',
1350        		),
1351    		)
1352		));
1353	}
1354
1355	/**
1356	 * @see github issue 206
1357	 * Unable to pass CDbCriteria to relation while array works fine.
1358	 */
1359	public function testIssue206()
1360	{
1361		$user = User::model()->findByPk(2);
1362		$result1 = $user->posts(array('condition' => 'id IN (2,3)'));
1363
1364		$criteria = new CDbCriteria();
1365		$criteria->addInCondition('id', array(2,3));
1366		$user = User::model()->findByPk(2);
1367		$result2 = $user->posts($criteria);
1368
1369		$this->assertEquals($result1, $result2);
1370	}
1371
1372	/**
1373	 * https://github.com/yiisoft/yii/issues/1070
1374	 */
1375	public function testIssue1070()
1376	{
1377		$dataProvider=new CActiveDataProvider('UserWithDefaultScope');
1378
1379		foreach($dataProvider->getData() as $item)
1380		{
1381			try
1382			{
1383				$item->links[0]->from_user;
1384				$result=true;
1385			}
1386			catch ( CDbException $e )
1387			{
1388				$result=false;
1389			}
1390
1391			$this->assertTrue($result);
1392		}
1393	}
1394
1395	/**
1396	 * https://github.com/yiisoft/yii/issues/507
1397	 */
1398	public function testIssue507()
1399	{
1400		$this->assertEquals(2, count(UserWithDefaultScope::model()->findAll()));
1401
1402	}
1403
1404}