code:
<?php

Yii::import('system.collections.CConfiguration');

class MyClass extends CComponent
{
	public $param1;
	private $_param2;
	public $param3;
	private $_object;
	public $backquote;

	public function getParam2()
	{
		return $this->_param2;
	}

	public function setParam2($value)
	{
		$this->_param2=$value;
	}

	public function getObject()
	{
		if($this->_object===null)
			$this->_object=new MyClass;
		return $this->_object;
	}
}

class CConfigurationTest extends CTestCase
{
	public $configFile;

	public function setUp()
	{
		$this->configFile=dirname(__FILE__).'/data/config.php';
	}

	public function tearDown()
	{
	}

	public function testLoadFromFile()
	{
		$config=new CConfiguration;
		$this->assertTrue($config->toArray()===array());
		$config->loadFromFile($this->configFile);
		$data=include($this->configFile);
		$this->assertTrue($config->toArray()===$data);
	}

	public function testSaveAsString()
	{
		$config=new CConfiguration($this->configFile);
		$str=$config->saveAsString();
		eval("\$data=$str;");
		$this->assertTrue($config->toArray()===$data);
	}

	public function testApplyTo()
	{
		$config=new CConfiguration($this->configFile);
		$object=new MyClass;
		$config->applyTo($object);
		$this->assertTrue($object->param1==='value1');
		$this->assertTrue($object->param2===false);
		$this->assertTrue($object->param3===123);
		$this->assertTrue($object->backquote==="\\back'quote'");
		/*
		$this->assertTrue($object->object->param1===null);
		$this->assertTrue($object->object->param2==='123');
		$this->assertTrue($object->object->param3===array('param1'=>'kkk','ddd',''));
		*/
	}

	public function testException()
	{
		$config=new CConfiguration(array('invalid'=>'value'));
		$object=new MyClass;
		$this->setExpectedException('CException');
		$config->applyTo($object);
	}

	public function testCreateComponent()
	{
		$obj=Yii::createComponent(array('class'=>'MyClass','param2'=>3));
		$this->assertEquals(get_class($obj),'MyClass');
		$this->assertEquals($obj->param2,3);
	}
}


code:
<?php

Yii::import('system.collections.CList');

class ListItem
{
	public $data='data';
}

class CListTest extends CTestCase
{
	protected $list;
	protected $item1, $item2, $item3;

	public function setUp()
	{
		$this->list=new CList;
		$this->item1=new ListItem;
		$this->item2=new ListItem;
		$this->item3=new ListItem;
		$this->list->add($this->item1);
		$this->list->add($this->item2);
	}

	public function tearDown()
	{
		$this->list=null;
		$this->item1=null;
		$this->item2=null;
		$this->item3=null;
	}

	public function testConstruct()
	{
		$a=array(1,2,3);
		$list=new CList($a);
		$this->assertEquals(3,$list->getCount());
		$list2=new CList($this->list);
		$this->assertEquals(2,$list2->getCount());
	}

	public function testGetReadOnly()
	{
		$list = new CList(null, true);
		$this->assertEquals(true, $list->getReadOnly(), 'List is not read-only');
		$list = new CList(null, false);
		$this->assertEquals(false, $list->getReadOnly(), 'List is read-only');
	}

	public function testGetCount()
	{
		$this->assertEquals(2,$this->list->getCount());
		$this->assertEquals(2,$this->list->Count);
	}

	public function testAdd()
	{
		$this->list->add(null);
		$this->list->add($this->item3);
		$this->assertEquals(4,$this->list->getCount());
		$this->assertEquals(3,$this->list->indexOf($this->item3));
	}

	public function testInsertAt()
	{
		$this->list->insertAt(0,$this->item3);
		$this->assertEquals(3,$this->list->getCount());
		$this->assertEquals(2,$this->list->indexOf($this->item2));
		$this->assertEquals(0,$this->list->indexOf($this->item3));
		$this->assertEquals(1,$this->list->indexOf($this->item1));
		$this->setExpectedException('CException');
		$this->list->insertAt(4,$this->item3);
	}

	public function testCanNotInsertWhenReadOnly()
	{
		$list = new CList(array(), true);
		$this->setExpectedException('CException');
		$list->insertAt(1, 2);
	}

	public function testRemove()
	{
		$this->list->remove($this->item1);
		$this->assertEquals(1,$this->list->getCount());
		$this->assertEquals(-1,$this->list->indexOf($this->item1));
		$this->assertEquals(0,$this->list->indexOf($this->item2));

		$this->assertEquals(false,$this->list->remove($this->item1));

	}

	public function testRemoveAt()
	{
		$this->list->add($this->item3);
		$this->list->removeAt(1);
		$this->assertEquals(-1,$this->list->indexOf($this->item2));
		$this->assertEquals(1,$this->list->indexOf($this->item3));
		$this->assertEquals(0,$this->list->indexOf($this->item1));
		$this->setExpectedException('CException');
		$this->list->removeAt(2);
	}

	public function testCanNotRemoveWhenReadOnly()
	{
		$list = new CList(array(1, 2, 3), true);
		$this->setExpectedException('CException');
		$list->removeAt(2);
	}

	public function testClear()
	{
		$this->list->clear();
		$this->assertEquals(0,$this->list->getCount());
		$this->assertEquals(-1,$this->list->indexOf($this->item1));
		$this->assertEquals(-1,$this->list->indexOf($this->item2));
	}

	public function testContains()
	{
		$this->assertTrue($this->list->contains($this->item1));
		$this->assertTrue($this->list->contains($this->item2));
		$this->assertFalse($this->list->contains($this->item3));
	}

	public function testIndexOf()
	{
		$this->assertEquals(0,$this->list->indexOf($this->item1));
		$this->assertEquals(1,$this->list->indexOf($this->item2));
		$this->assertEquals(-1,$this->list->indexOf($this->item3));
	}

	public function testCopyFrom()
	{
		$array=array($this->item3,$this->item1);
		$this->list->copyFrom($array);
		$this->assertTrue(count($array)==2 && $this->list[0]===$this->item3 && $this->list[1]===$this->item1);
		$this->setExpectedException('CException');
		$this->list->copyFrom($this);
	}

	public function testMergeWith()
	{
		$array=array($this->item3,$this->item1);
		$this->list->mergeWith($array);
		$this->assertTrue($this->list->getCount()==4 && $this->list[0]===$this->item1 && $this->list[3]===$this->item1);
		$this->setExpectedException('CException');
		$this->list->mergeWith($this);
	}

	public function testToArray()
	{
		$array=$this->list->toArray();
		$this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2);
	}

	public function testArrayRead()
	{
		$this->assertTrue($this->list[0]===$this->item1);
		$this->assertTrue($this->list[1]===$this->item2);
		$this->setExpectedException('CException');
		$a=$this->list[2];
	}

	public function testGetIterator()
	{
		$n=0;
		$found=0;
		foreach($this->list as $index=>$item)
		{
			foreach($this->list as $a=>$b);	// test of iterator
			$n++;
			if($index===0 && $item===$this->item1)
				$found++;
			if($index===1 && $item===$this->item2)
				$found++;
		}
		$this->assertTrue($n==2 && $found==2);
	}

	public function testArrayMisc()
	{
		$this->assertEquals($this->list->Count,count($this->list));
		$this->assertTrue(isset($this->list[1]));
		$this->assertFalse(isset($this->list[2]));
	}

	public function testOffsetSetAdd()
	{
		$list = new CList(array(1, 2, 3));
		$list->offsetSet(null, 4);
		$this->assertEquals(array(1, 2, 3, 4), $list->toArray());
	}

	public function testOffsetSetReplace()
	{
		$list = new CList(array(1, 2, 3));
		$list->offsetSet(1, 4);
		$this->assertEquals(array(1, 4, 3), $list->toArray());
	}

	public function testOffsetUnset()
	{
		$list = new CList(array(1, 2, 3));
		$list->offsetUnset(1);
		$this->assertEquals(array(1, 3), $list->toArray());
	}

	public function testIteratorCurrent()
	{
		$list = new CList(array('value1', 'value2'));
		$val = $list->getIterator()->current();
		$this->assertEquals('value1', $val);
	}
}

repo_name: DaSourcerer/yii

license: mit


code:
<?php

Yii::import('system.collections.CMap');

class MapItem
{
	public $data='data';
}

class CMapTest extends CTestCase
{
	protected $map;
	protected $item1,$item2,$item3;

	public function setUp()
	{
		$this->map=new CMap;
		$this->item1=new MapItem;
		$this->item2=new MapItem;
		$this->item3=new MapItem;
		$this->map->add('key1',$this->item1);
		$this->map->add('key2',$this->item2);
	}

	public function tearDown()
	{
		$this->map=null;
		$this->item1=null;
		$this->item2=null;
		$this->item3=null;
	}

	public function testConstruct()
	{
		$a=array(1,2,'key3'=>3);
		$map=new CMap($a);
		$this->assertEquals(3,$map->getCount());
		$map2=new CMap($this->map);
		$this->assertEquals(2,$map2->getCount());
	}

	public function testGetReadOnly()
	{
		$map = new CMap(null, true);
		self::assertEquals(true, $map->getReadOnly(), 'List is not read-only');
	}

	public function testGetCount()
	{
		$this->assertEquals(2,$this->map->getCount());
	}

	public function testGetKeys()
	{
		$keys=$this->map->getKeys();
		$this->assertEquals(2,count($keys));
		$this->assertEquals('key1',$keys[0]);
		$this->assertEquals('key2',$keys[1]);
	}

	public function testAdd()
	{
		$this->map->add('key3',$this->item3);
		$this->assertEquals(3,$this->map->getCount());
		$this->assertTrue($this->map->contains('key3'));
	}

	public function testCanNotAddWhenReadOnly()
	{
		$map = new CMap(array(), true);
		$this->setExpectedException('CException');
		$map->add('key', 'value');
	}

	public function testRemove()
	{
		$this->map->remove('key1');
		$this->assertEquals(1,$this->map->getCount());
		$this->assertTrue(!$this->map->contains('key1'));
		$this->assertTrue($this->map->remove('unknown key')===null);
	}

	public function testCanNotRemoveWhenReadOnly()
	{
		$map = new CMap(array('key' => 'value'), true);
		$this->setExpectedException('CException');
		$map->remove('key');
	}

	public function testClear()
	{
		$this->map->clear();
		$this->assertEquals(0,$this->map->getCount());
		$this->assertTrue(!$this->map->contains('key1') && !$this->map->contains('key2'));
	}

	public function testContains()
	{
		$this->assertTrue($this->map->contains('key1'));
		$this->assertTrue($this->map->contains('key2'));
		$this->assertFalse($this->map->contains('key3'));
	}

	public function testCopyFrom()
	{
		$array=array('key3'=>$this->item3,'key4'=>$this->item1);
		$this->map->copyFrom($array);

		$this->assertEquals(2, $this->map->getCount());
		$this->assertEquals($this->item3, $this->map['key3']);
		$this->assertEquals($this->item1, $this->map['key4']);

		$this->setExpectedException('CException');
		$this->map->copyFrom($this);
	}

	public function testMergeWith()
	{
		$a=array('a'=>'v1','v2',array('2'),'c'=>array('3','c'=>'a'));
		$b=array('v22','a'=>'v11',array('2'),'c'=>array('c'=>'3','a'));
		$c=array('a'=>'v11','v2',array('2'),'c'=>array('3','c'=>'3','a'),'v22',array('2'));
		$map=new CMap($a);
		$map2=new CMap($b);
		$map->mergeWith($map2);
		$this->assertTrue($map->toArray()===$c);

		$array=array('key2'=>$this->item1,'key3'=>$this->item3);
		$this->map->mergeWith($array,false);
		$this->assertEquals(3,$this->map->getCount());
		$this->assertEquals($this->item1,$this->map['key2']);
		$this->assertEquals($this->item3,$this->map['key3']);
		$this->setExpectedException('CException');
		$this->map->mergeWith($this,false);
	}

	public function testRecursiveMergeWithTraversable(){
		$map = new CMap();
		$obj = new ArrayObject(array(
			'k1' => $this->item1,
			'k2' => $this->item2,
			'k3' => new ArrayObject(array(
				'k4' => $this->item3,
			))
		));
		$map->mergeWith($obj,true);

		$this->assertEquals(3, $map->getCount());
		$this->assertEquals($this->item1, $map['k1']);
		$this->assertEquals($this->item2, $map['k2']);
		$this->assertEquals($this->item3, $map['k3']['k4']);
	}

	public function testArrayRead()
	{
		$this->assertEquals($this->item1,$this->map['key1']);
		$this->assertEquals($this->item2,$this->map['key2']);
		$this->assertEquals(null,$this->map['key3']);
	}

	public function testArrayWrite()
	{
		$this->map['key3']=$this->item3;
		$this->assertEquals(3,$this->map->getCount());
		$this->assertEquals($this->item3,$this->map['key3']);

		$this->map['key1']=$this->item3;
		$this->assertEquals(3,$this->map->getCount());
		$this->assertEquals($this->item3,$this->map['key1']);

		unset($this->map['key2']);
		$this->assertEquals(2,$this->map->getCount());
		$this->assertTrue(!$this->map->contains('key2'));

		unset($this->map['unknown key']);
	}

	public function testArrayForeach()
	{
		$n=0;
		$found=0;
		foreach($this->map as $index=>$item)
		{
			$n++;
			if($index==='key1' && $item===$this->item1)
				$found++;
			if($index==='key2' && $item===$this->item2)
				$found++;
		}
		$this->assertTrue($n==2 && $found==2);
	}

	public function testArrayMisc()
	{
		$this->assertEquals($this->map->Count,count($this->map));
		$this->assertTrue(isset($this->map['key1']));
		$this->assertFalse(isset($this->map['unknown key']));
	}

	public function testToArray()
	{
		$map = new CMap(array('key' => 'value'));
		$this->assertEquals(array('key' => 'value'), $map->toArray());
	}

	public function testIteratorCurrent()
	{
		$map = new CMap(array('key1' => 'value1', 'key2' => 'value2'));
		$val = $map->getIterator()->current();
		$this->assertEquals('value1', $val);
	}
}


code:
<?php

Yii::import('system.collections.CQueue');

class CQueueTest extends CTestCase
{
	public function testConstruct()
	{
		$queue = new CQueue();
		$this->assertEquals(array(), $queue->toArray());
		$queue = new CQueue(array(1, 2, 3));
		$this->assertEquals(array(1, 2, 3), $queue->toArray());
	}

	public function testToArray()
	{
		$queue = new CQueue(array(1, 2, 3));
		$this->assertEquals(array(1, 2, 3), $queue->toArray());
	}

	public function testCopyFrom()
	{
		$queue = new CQueue(array(1, 2, 3));
		$data = array(4, 5, 6);
		$queue->copyFrom($data);
		$this->assertEquals(array(4, 5, 6), $queue->toArray());
	}

	public function testCanNotCopyFromNonTraversableTypes()
	{
		$queue = new CQueue();
		$data = new stdClass();
		$this->setExpectedException('CException');
		$queue->copyFrom($data);
	}

	public function testClear()
	{
		$queue = new CQueue(array(1, 2, 3));
		$queue->clear();
		$this->assertEquals(array(), $queue->toArray());
	}

	public function testContains()
	{
		$queue = new CQueue(array(1, 2, 3));
		$this->assertEquals(true, $queue->contains(2));
		$this->assertEquals(false, $queue->contains(4));
	}

	public function testPeek()
	{
		$queue = new CQueue(array(1));
		$this->assertEquals(1, $queue->peek());
	}

	public function testCanNotPeekAnEmptyQueue()
	{
		$queue = new CQueue();
		$this->setExpectedException('CException');
		$item = $queue->peek();
	}

	public function testDequeue()
	{
		$queue = new CQueue(array(1, 2, 3));
		$first = $queue->dequeue();
		$this->assertEquals(1, $first);
		$this->assertEquals(array(2, 3), $queue->toArray());
	}

	public function testCanNotDequeueAnEmptyQueue()
	{
		$queue = new CQueue();
		$this->setExpectedException('CException');
		$item = $queue->dequeue();
	}

	public function testEnqueue()
	{
		$queue = new CQueue();
		$queue->enqueue(1);
		$this->assertEquals(array(1), $queue->toArray());
	}

 	public function testGetIterator()
 	{
		$queue = new CQueue(array(1, 2));
		$this->assertInstanceOf('CQueueIterator', $queue->getIterator());
		$n = 0;
		$found = 0;
		foreach($queue as $index => $item)
		{
			foreach($queue as $a => $b); // test of iterator
			$n++;
			if($index === 0 && $item === 1)
				$found++;
			if($index === 1 && $item === 2)
				$found++;
		}
		$this->assertTrue($n == 2 && $found == 2);
	}

	public function testGetCount()
	{
    	$queue = new CQueue();
		$this->assertEquals(0, $queue->getCount());
		$queue = new CQueue(array(1, 2, 3));
		$this->assertEquals(3, $queue->getCount());
	}

	public function testCountable()
	{
		$queue = new CQueue();
		$this->assertEquals(0, count($queue));
		$queue = new CQueue(array(1, 2, 3));
		$this->assertEquals(3, count($queue));
	}
}


code:
<?php

Yii::import('system.collections.CStack');

class CStackTest extends CTestCase
{
	public function testConstruct()
	{
		$stack = new CStack();
		$this->assertEquals(array(), $stack->toArray());
		$stack = new CStack(array(1, 2, 3));
		$this->assertEquals(array(1, 2, 3), $stack->toArray());
	}

	public function testToArray()
	{
		$stack = new CStack(array(1, 2, 3));
		$this->assertEquals(array(1, 2, 3), $stack->toArray());
	}

	public function testCopyFrom()
	{
		$stack = new CStack(array(1, 2, 3));
		$data = array(4, 5, 6);
		$stack->copyFrom($data);
		$this->assertEquals(array(4, 5, 6), $stack->toArray());
	}

	public function testCanNotCopyFromNonTraversableTypes()
	{
		$stack = new CStack();
		$data = new stdClass();
		$this->setExpectedException('CException');
		$stack->copyFrom($data);
	}

	public function testClear()
	{
		$stack = new CStack(array(1, 2, 3));
		$stack->clear();
		$this->assertEquals(array(), $stack->toArray());
	}

	public function testContains()
	{
		$stack = new CStack(array(1, 2, 3));
		$this->assertEquals(true, $stack->contains(2));
		$this->assertEquals(false, $stack->contains(4));
	}

	public function testPeek()
	{
		$stack = new CStack(array(1));
		$this->assertEquals(1, $stack->peek());
	}

	public function testCanNotPeekAnEmptyStack()
	{
		$stack = new CStack();
		$this->setExpectedException('CException');
		$item = $stack->peek();
	}

	public function testPop()
	{
		$stack = new CStack(array(1, 2, 3));
		$last = $stack->pop();
		$this->assertEquals(3, $last);
		$this->assertEquals(array(1, 2), $stack->toArray());
	}

	public function testCanNotPopAnEmptyStack()
	{
		$stack = new CStack();
		$this->setExpectedException('CException');
		$item = $stack->pop();
	}

	public function testPush()
	{
		$stack = new CStack();
		$stack->push(1);
		$this->assertEquals(array(1), $stack->toArray());
	}

 	public function testGetIterator()
 	{
		$stack = new CStack(array(1, 2));
		$this->assertInstanceOf('CStackIterator', $stack->getIterator());
		$n = 0;
		$found = 0;
		foreach($stack as $index => $item)
		{
			foreach($stack as $a => $b); // test of iterator
			$n++;
			if($index === 0 && $item === 1)
				$found++;
			if($index === 1 && $item === 2)
				$found++;
		}
		$this->assertTrue($n == 2 && $found == 2);
	}

	public function testGetCount()
	{
		$stack = new CStack();
		$this->assertEquals(0, $stack->getCount());
		$stack = new CStack(array(1, 2, 3));
		$this->assertEquals(3, $stack->getCount());
	}

	public function testCount()
	{
		$stack = new CStack();
		$this->assertEquals(0, count($stack));
		$stack = new CStack(array(1, 2, 3));
		$this->assertEquals(3, count($stack));
	}
}


code:
<?php

Yii::import('system.collections.CTypedList');

class CTypedListTest extends CTestCase
{
	public function testClassType()
	{
		$list=new CTypedList('CComponent');
		$list[]=new CComponent;
		$this->setExpectedException('CException');
		$list[]=new stdClass;
	}

	public function testInterfaceType()
	{
		$list=new CTypedList('Traversable');
		$list[]=new CList;
		$this->setExpectedException('CException');
		$list[]=new CComponent;
	}
}


code:
<?php

return array(
	'param1'=>'value1',
	'param2'=>false,
	'param3'=>123,
	"backquote"=>"\\back'quote'",
/*	'object'=>array(
		'param1'=>null,
		'param2'=>'123',
		'param3'=>array(
			'param1'=>'kkk',
			'ddd',
			'',
		),
	),*/
);


code:
<?php

Yii::import('system.db.CDbConnection');

class CDbCommand2Test extends CTestCase
{
	private $_connection;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->_connection=new CDbConnection('sqlite::memory:');
		$this->_connection->active=true;
		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
	}

	public function tearDown()
	{
		$this->_connection->active=false;
	}

	public function testSelect()
	{
		$command=$this->_connection->createCommand();

		// default
		$command->select();
		$this->assertEquals('*', $command->select);

		// string input
		$command->select('id, username');
		$this->assertEquals('"id", "username"', $command->select);

		// string input with expression
		$command->select('id, count(id) as num');
		$this->assertEquals('id, count(id) as num', $command->select);

		// array input
		$command->select(array('id2', 'username2'));
		$this->assertEquals('"id2", "username2"', $command->select);

		// table prefix and expression
		$command->select(array('user.id', 'count(id) as num', 'profile.*'));
		$this->assertEquals('\'user\'."id", count(id) as num, \'profile\'.*', $command->select);

		// alias
		$command->select(array('id2 as id', 'profile.username2 AS username'));
		$this->assertEquals('"id2" AS "id", \'profile\'."username2" AS "username"', $command->select);

		// getter and setter
		$command->select=array('id2', 'username2');
		$this->assertEquals('"id2", "username2"', $command->select);
	}

	public function testDistinct()
	{
		$command=$this->_connection->createCommand();

		// default value
		$this->assertEquals(false, $command->distinct);

		// select distinct
		$command->selectDistinct('id, username');
		$this->assertEquals(true, $command->distinct);
		$this->assertEquals('"id", "username"', $command->select);

		// getter and setter
		$command->distinct=false;
		$this->assertEquals(false, $command->distinct);
		$command->distinct=true;
		$this->assertEquals(true, $command->distinct);
	}

	public function testFrom()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->from);

		// string input
		$command->from('user');
		$this->assertEquals('\'user\'', $command->from);
		$command->from('user, profile');
		$this->assertEquals('\'user\', \'profile\'', $command->from);

		// string input with expression
		$command->from('user, (select * from profile) p');
		$this->assertEquals('user, (select * from profile) p', $command->from);

		// array input
		$command->from(array('user', 'profile'));
		$this->assertEquals('\'user\', \'profile\'', $command->from);

		// table alias, expression, schema
		$command->from(array('user u', '(select * from profile) p', 'public.post'));
		$this->assertEquals('\'user\' \'u\', (select * from profile) p, \'public\'.\'post\'', $command->from);

		// getter and setter
		$command->from=array('user', 'profile');
		$this->assertEquals('\'user\', \'profile\'', $command->from);
	}

	public function testWhere()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->where);
		$this->assertEquals(array(), $command->params);

		// string input
		$command->where('id=1 or id=:id2', array(':id2'=>2));
		$this->assertEquals('id=1 or id=:id2', $command->where);
		$this->assertEquals(array(':id2'=>2), $command->params);

		// array input, and/or
		$command->where(array('and', 'id=1', 'id=2'));
		$this->assertEquals('(id=1) AND (id=2)', $command->where);
		$command->where(array('and', 'id=1', array('or', 'id=3', 'id=4'), 'id=2'));
		$this->assertEquals('(id=1) AND ((id=3) OR (id=4)) AND (id=2)', $command->where);

		// empty input
		$command->where(array());
		$this->assertEquals('', $command->where);

		// in, empty
		$command->where(array('in', 'id', array()));
		$this->assertEquals('0=1', $command->where);

		// in
		$command->where(array('in', 'id', array(1,'2',3)));
		$this->assertEquals("\"id\" IN (1, '2', 3)", $command->where);

		// not in, empty
		$command->where(array('not in', 'id', array()));
		$this->assertEquals('', $command->where);

		// not in
		$command->where(array('not in', 'id', array(1,'2',3)));
		$this->assertEquals("\"id\" NOT IN (1, '2', 3)", $command->where);

		// like, string
		$command->where(array('like', 'name', '%tester'));
		$this->assertEquals('"name" LIKE \'%tester\'', $command->where);

		$command->where(array('like', 'name', array('%tester', '%tester2')));
		$this->assertEquals('"name" LIKE \'%tester\' AND "name" LIKE \'%tester2\'', $command->where);

		$command->where(array('not like', 'name', array('tester%', 'tester2%')));
		$this->assertEquals('"name" NOT LIKE \'tester%\' AND "name" NOT LIKE \'tester2%\'', $command->where);

		$command->where(array('or like', 'name', array('%tester', '%tester2')));
		$this->assertEquals('"name" LIKE \'%tester\' OR "name" LIKE \'%tester2\'', $command->where);

		$command->where(array('or not like', 'name', array('%tester', '%tester2')));
		$this->assertEquals('"name" NOT LIKE \'%tester\' OR "name" NOT LIKE \'%tester2\'', $command->where);
	}

	public function testJoin()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->join);

		// inner join
		$command->join('user', 'user.id=t.id and id=:id', array(':id'=>1));
		$this->assertEquals(array('JOIN \'user\' ON user.id=t.id and id=:id'), $command->join);
		$this->assertEquals(array(':id'=>1), $command->params);

		// left join
		$join=$command->join;
		$command->leftJoin('user', 'user.id=t.id and id=:id');
		$join[]='LEFT JOIN \'user\' ON user.id=t.id and id=:id';
		$this->assertEquals($join, $command->join);

		// right join
		$command->rightJoin('user', 'user.id=t.id and id=:id');
		$join[]='RIGHT JOIN \'user\' ON user.id=t.id and id=:id';
		$this->assertEquals($join, $command->join);

		// cross join
		$command->crossJoin('user');
		$join[]='CROSS JOIN \'user\'';
		$this->assertEquals($join, $command->join);

		// natural join
		$command->naturalJoin('user');
		$join[]='NATURAL JOIN \'user\'';
		$this->assertEquals($join, $command->join);
	}

	public function testGroup()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->group);

		// string input
		$command->group('id, username');
		$this->assertEquals('"id", "username"', $command->group);

		// string input with expression
		$command->group('id, count(id)');
		$this->assertEquals('id, count(id)', $command->group);

		// array input
		$command->group(array('id2', 'username2'));
		$this->assertEquals('"id2", "username2"', $command->group);

		// table prefix and expression
		$command->group(array('user.id', 'count(id)'));
		$this->assertEquals('\'user\'."id", count(id)', $command->group);

		// getter and setter
		$command->group=array('id2', 'username2');
		$this->assertEquals('"id2", "username2"', $command->group);
	}

	public function testHaving()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->having);
		$this->assertEquals(array(), $command->params);

		// string input
		$command->having('id=1 or id=:id2', array(':id2'=>2));
		$this->assertEquals('id=1 or id=:id2', $command->having);
		$this->assertEquals(array(':id2'=>2), $command->params);

		// array input, and/or
		$command->having(array('and', 'id=1', 'id=2'));
		$this->assertEquals('(id=1) AND (id=2)', $command->having);
		$command->having(array('and', 'id=1', array('or', 'id=3', 'id=4'), 'id=2'));
		$this->assertEquals('(id=1) AND ((id=3) OR (id=4)) AND (id=2)', $command->having);
	}

	public function testOrder()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->order);

		// string input
		$command->order('id, username desc');
		$this->assertEquals('"id", "username" DESC', $command->order);

		// string input with expression
		$command->order('id, count(id) desc');
		$this->assertEquals('id, count(id) desc', $command->order);

		// array input
		$command->order(array('id2 asc', 'username2 DESC'));
		$this->assertEquals('"id2" ASC, "username2" DESC', $command->order);

		// table prefix and expression
		$command->order(array('user.id asc', 'count(id)'));
		$this->assertEquals('\'user\'."id" ASC, count(id)', $command->order);

		// getter and setter
		$command->order=array('id2 asc', 'username2');
		$this->assertEquals('"id2" ASC, "username2"', $command->order);
	}

	public function testLimit()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals(-1, $command->limit);

		$command->limit(10);
		$this->assertEquals(10, $command->limit);

		$command->limit(20,30);
		$this->assertEquals(20, $command->limit);
		$this->assertEquals(30, $command->offset);

		// invalid string
		$command->limit('abc');
		$this->assertEquals(0, $command->limit);
	}

	public function testOffset()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals(-1, $command->offset);

		$command->offset(10);
		$this->assertEquals(10, $command->offset);

		// invalid string
		$command->offset('abc');
		$this->assertEquals(0, $command->offset);
	}

	public function testUnion()
	{
		$command=$this->_connection->createCommand();

		// default
		$this->assertEquals('', $command->union);

		$command->union('select * from user');
		$this->assertEquals(array('select * from user'), $command->union);

		$command->union('select * from post');
		$this->assertEquals(array('select * from user', 'select * from post'), $command->union);
	}

	/*
	public function testInsert()
	{
		$command=$this->_connection->createCommand();

		$command->insert('user', array('id'=>1, 'username'=>'tester'));
		$this->assertEquals('INSERT INTO \'user\' ("id", "username") VALUES (:id, :username)', $command->text);
		$this->assertEquals(array(':id'=>1, ':username'=>'tester'), $command->params);
	}

	public function testUpdate()
	{
		$command=$this->_connection->createCommand();

		$command->update('user', array('id'=>1, 'username'=>'tester'), 'status=:status', array(':status'=>2));
		$this->assertEquals('UPDATE \'user\' SET "id"=:id, "username"=:username WHERE status=:status', $command->text);
		$this->assertEquals(array(':id'=>1, ':username'=>'tester', ':status'=>2), $command->params);
	}

	public function testDelete()
	{
		$command=$this->_connection->createCommand();

		$command->delete('user', 'status=:status', array(':status'=>2));
		$this->assertEquals('DELETE FROM \'user\' WHERE status=:status', $command->text);
		$this->assertEquals(array(':status'=>2), $command->params);
	}
	*/

	public function testQuery()
	{
		// simple query
		$command=$this->_connection->createCommand()
			->select('username, password')
			->from('users')
			->where('email=:email or email=:email2', array(':email'=>'email2', ':email2'=>'email4'))
			->order('username desc')
			->limit(2,1);

		$sql="SELECT \"username\", \"password\"\nFROM 'users'\nWHERE email=:email or email=:email2\nORDER BY \"username\" DESC LIMIT 2 OFFSET 1";
		$this->assertEquals($sql, $command->text);

		$rows=$command->queryAll();
		$this->assertEquals(1,count($rows));
		$this->assertEquals('user2',$rows[0]['username']);
		$this->assertEquals('pass2',$rows[0]['password']);
	}

	public function testArraySyntax()
	{
		$command=$this->_connection->createCommand(array(
			'select'=>'username, password',
			'from'=>'users',
			'where'=>'email=:email or email=:email2',
			'params'=>array(':email'=>'email2', ':email2'=>'email4'),
			'order'=>'username desc',
			'limit'=>2,
			'offset'=>1,
		));

		$sql="SELECT \"username\", \"password\"\nFROM 'users'\nWHERE email=:email or email=:email2\nORDER BY \"username\" DESC LIMIT 2 OFFSET 1";
		$this->assertEquals($sql, $command->text);

		$rows=$command->queryAll();
		$this->assertEquals(1,count($rows));
		$this->assertEquals('user2',$rows[0]['username']);
		$this->assertEquals('pass2',$rows[0]['password']);
	}
}


code:
<?php

Yii::import('system.db.CDbConnection');

class CDbCommandTest extends CTestCase
{
	private $_connection;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->_connection=new CDbConnection('sqlite::memory:');
		$this->_connection->active=true;
		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
	}

	public function tearDown()
	{
		$this->_connection->active=false;
	}

	public function testGetText()
	{
		$sql='SELECT * FROM posts';
		$command=$this->_connection->createCommand($sql);
		$this->assertEquals($command->text,$sql);
	}

	public function testSetText()
	{
		$sql='SELECT title FROM posts';
		$command=$this->_connection->createCommand($sql);
		$this->assertEquals($command->queryScalar(),'post 1');

		$newSql='SELECT id FROM posts';
		$command->text=$newSql;
		$this->assertEquals($command->text,$newSql);
		$this->assertEquals($command->queryScalar(),1);
	}

	public function testConnection()
	{
		$sql='SELECT title FROM posts';
		$command=$this->_connection->createCommand($sql);
		$this->assertEquals($command->connection,$this->_connection);
	}

	public function testPrepare()
	{
		$sql='SELECT title FROM posts';
		$command=$this->_connection->createCommand($sql);
		$this->assertEquals($command->pdoStatement,null);
		$command->prepare();
		$this->assertTrue($command->pdoStatement instanceof PDOStatement);
		$this->assertEquals($command->queryScalar(),'post 1');

		$command->text='Bad SQL';
		$this->setExpectedException('CException');
		$command->prepare();
	}

	public function testCancel()
	{
		$sql='SELECT title FROM posts';
		$command=$this->_connection->createCommand($sql);
		$command->prepare();
		$this->assertTrue($command->pdoStatement instanceof PDOStatement);
		$command->cancel();
		$this->assertEquals($command->pdoStatement,null);
	}

	public function testExecute()
	{
		$sql='INSERT INTO comments(content,post_id,author_id) VALUES (\'test comment\', 1, 1)';
		$command=$this->_connection->createCommand($sql);
		$this->assertEquals($command->execute(),1);
		$this->assertEquals($command->execute(),1);
		$command=$this->_connection->createCommand('SELECT * FROM comments WHERE content=\'test comment\'');
		$this->assertEquals($command->execute(),0);
		$command=$this->_connection->createCommand('SELECT COUNT(*) FROM comments WHERE content=\'test comment\'');
		$this->assertEquals($command->queryScalar(),2);

		$command=$this->_connection->createCommand('bad SQL');
		$this->setExpectedException('CException');
		$command->execute();
	}

	public function testQuery()
	{
		$sql='SELECT * FROM posts';
		$reader=$this->_connection->createCommand($sql)->query();
		$this->assertTrue($reader instanceof CDbDataReader);

		$sql='SELECT * FROM posts';
		$command=$this->_connection->createCommand($sql);
		$command->prepare();
		$reader=$command->query();
		$this->assertTrue($reader instanceof CDbDataReader);

		$command=$this->_connection->createCommand('bad SQL');
		$this->setExpectedException('CException');
		$command->query();
	}

	public function testBindParam()
	{
		$sql='INSERT INTO posts(title,create_time,author_id) VALUES (:title, :create_time, 1)';
		$command=$this->_connection->createCommand($sql);
		$title='test title';
		$createTime=time();
		$command->bindParam(':title',$title);
		$command->bindParam(':create_time',$createTime);
		$command->execute();

		$sql='SELECT create_time FROM posts WHERE title=:title';
		$command=$this->_connection->createCommand($sql);
		$command->bindParam(':title',$title);
		$this->assertEquals($command->queryScalar(),$createTime);

		$sql='INSERT INTO types (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, :blob_col, :numeric_col, :bool_col)';
		$command=$this->_connection->createCommand($sql);
		$intCol=123;
		$charCol='abc';
		$floatCol=1.23;
		$blobCol="\x10\x11\x12";
		$numericCol='1.23';
		$boolCol=false;
		$command->bindParam(':int_col',$intCol);
		$command->bindParam(':char_col',$charCol);
		$command->bindParam(':float_col',$floatCol);
		$command->bindParam(':blob_col',$blobCol);
		$command->bindParam(':numeric_col',$numericCol);
		$command->bindParam(':bool_col',$boolCol);
		$this->assertEquals(1,$command->execute());

		$sql='SELECT * FROM types';
		$row=$this->_connection->createCommand($sql)->queryRow();
		$this->assertEquals($row['int_col'],$intCol);
		$this->assertEquals($row['char_col'],$charCol);
		$this->assertEquals($row['float_col'],$floatCol);
		$this->assertEquals($row['blob_col'],$blobCol);
		$this->assertEquals($row['numeric_col'],$numericCol);
	}

	public function testBindValue()
	{
		$sql='INSERT INTO comments(content,post_id,author_id) VALUES (:content, 1, 1)';
		$command=$this->_connection->createCommand($sql);
		$command->bindValue(':content','test comment');
		$command->execute();

		$sql='SELECT post_id FROM comments WHERE content=:content';
		$command=$this->_connection->createCommand($sql);
		$command->bindValue(':content','test comment');
		$this->assertEquals($command->queryScalar(),1);
	}

	public function testQueryAll()
	{
		$rows=$this->_connection->createCommand('SELECT * FROM posts')->queryAll();
		$this->assertEquals(count($rows),5);
		$row=$rows[2];
		$this->assertEquals($row['id'],3);
		$this->assertEquals($row['title'],'post 3');

		$rows=$this->_connection->createCommand('SELECT * FROM posts WHERE id=10')->queryAll();
		$this->assertEquals($rows,array());
	}

	public function testQueryRow()
	{
		$sql='SELECT * FROM posts';
		$row=$this->_connection->createCommand($sql)->queryRow();
		$this->assertEquals($row['id'],1);
		$this->assertEquals($row['title'],'post 1');

		$sql='SELECT * FROM posts';
		$command=$this->_connection->createCommand($sql);
		$command->prepare();
		$row=$command->queryRow();
		$this->assertEquals($row['id'],1);
		$this->assertEquals($row['title'],'post 1');

		$sql='SELECT * FROM posts WHERE id=10';
		$command=$this->_connection->createCommand($sql);
		$this->assertFalse($command->queryRow());

		$command=$this->_connection->createCommand('bad SQL');
		$this->setExpectedException('CException');
		$command->queryRow();
	}

	public function testQueryColumn()
	{
		$sql='SELECT * FROM posts';
		$column=$this->_connection->createCommand($sql)->queryColumn();
		$this->assertEquals($column,range(1,5));

		$command=$this->_connection->createCommand('SELECT id FROM posts WHERE id=10');
		$this->assertEquals($command->queryColumn(),array());

		$command=$this->_connection->createCommand('bad SQL');
		$this->setExpectedException('CException');
		$command->queryColumn();
	}

	public function testQueryScalar()
	{
		$sql='SELECT * FROM posts';
		$this->assertEquals($this->_connection->createCommand($sql)->queryScalar(),1);

		$sql='SELECT id FROM posts';
		$command=$this->_connection->createCommand($sql);
		$command->prepare();
		$this->assertEquals($command->queryScalar(),1);

		$command=$this->_connection->createCommand('SELECT id FROM posts WHERE id=10');
		$this->assertFalse($command->queryScalar());

		$command=$this->_connection->createCommand('bad SQL');
		$this->setExpectedException('CException');
		$command->queryScalar();
	}

	public function testFetchMode(){
		$sql='SELECT * FROM posts';
		$command=$this->_connection->createCommand($sql);
		$result = $command->queryRow();
		$this->assertTrue(is_array($result));

		$sql='SELECT * FROM posts';
		$command=$this->_connection->createCommand($sql);
		$command->setFetchMode(PDO::FETCH_OBJ);
		$result = $command->queryRow();
		$this->assertTrue(is_object($result));
	}
}


code:
<?php
Yii::import('system.db.CDbConnection');

class CDbConnectionTest extends CTestCase
{
	private $_connection;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->_connection=new CDbConnection('sqlite::memory:');
	}

	public function tearDown()
	{
		$this->_connection->active=false;
	}

	public function testAutoConnect()
	{
		$db=new CDbConnection;
		$db->connectionString='sqlite::memory:';
		$this->assertFalse($db->active);
		$this->assertTrue($db->autoConnect);
		$db->init();
		$this->assertTrue($db->active);

		$db2=new CDbConnection;
		$db2->connectionString='sqlite::memory:';
		$db2->autoConnect=false;
		$this->assertFalse($db2->autoConnect);
		$this->assertFalse($db2->active);
		$db->init();
		$this->assertFalse($db2->active);
	}

	public function testInitialized()
	{
		$db=new CDbConnection;
		$db->autoConnect=false;
		$this->assertFalse($db->isInitialized);
		$db->init();
		$this->assertTrue($db->isInitialized);
	}

	public function testActive()
	{
	    $this->assertFalse($this->_connection->active);
	    $this->_connection->active=true;
	    $this->assertTrue($this->_connection->active);
	    $pdo=$this->_connection->pdoInstance;
	    $this->assertTrue($pdo instanceof PDO);

	    $this->_connection->active=true;
	    $this->assertEquals($pdo,$this->_connection->pdoInstance);

		$this->_connection->active=false;
	    $this->assertFalse($this->_connection->active);
	    $this->assertEquals($this->_connection->pdoInstance,null);

		$connection=new CDbConnection('unknown::memory:');
		$this->setExpectedException('CException');
		$connection->active=true;
	}

	public function testCreateCommand()
	{
		$sql='SELECT * FROM posts';
		$this->_connection->active=true;
		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
		$command=$this->_connection->createCommand($sql);
		$this->assertTrue($command instanceof CDbCommand);
	}

	public function testLastInsertID()
	{
	    $this->_connection->active=true;
	    $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
		$sql='INSERT INTO posts(title,create_time,author_id) VALUES(\'test post\',11000,1)';
		$this->_connection->createCommand($sql)->execute();
		$this->assertEquals($this->_connection->lastInsertID,6);
	}

	public function testQuoteValue()
	{
	    $this->_connection->active=true;
	    $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
		$str="this is 'my' name";
		$expectedStr="'this is ''my'' name'";
		$this->assertEquals($expectedStr,$this->_connection->quoteValue($str));
	}

	public function testColumnNameCase()
	{
	    $this->_connection->active=true;
	    $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
		$this->assertEquals(PDO::CASE_NATURAL,$this->_connection->ColumnCase);
		$this->_connection->columnCase=PDO::CASE_LOWER;
		$this->assertEquals(PDO::CASE_LOWER,$this->_connection->ColumnCase);
	}

	public function testNullConversion()
	{
	    $this->_connection->active=true;
	    $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
		$this->assertEquals(PDO::NULL_NATURAL,$this->_connection->NullConversion);
		$this->_connection->nullConversion=PDO::NULL_EMPTY_STRING;
		$this->assertEquals(PDO::NULL_EMPTY_STRING,$this->_connection->NullConversion);
	}
}


code:
<?php

Yii::import('system.db.CDbConnection');

class PostRecord extends CComponent
{
	public $param1;
	public $param2;
	public $id;
	private $_title;
	public $content;
	public $create_time;
	public $author_id;

	public function __construct($param1,$param2)
	{
		$this->param1=$param1;
		$this->param2=$param2;
	}

	public function getTitle()
	{
		return $this->_title;
	}

	public function setTitle($value)
	{
		$this->_title=$value;
	}
}

class CDbDataReaderTest extends CTestCase
{
	private $_connection;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->_connection=new CDbConnection('sqlite::memory:');
		$this->_connection->active=true;
		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
	}

	public function tearDown()
	{
		$this->_connection->active=false;
	}

	public function testRead()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		for($i=1;$i<=5;++$i)
		{
			$row=$reader->read();
			$this->assertEquals($row['id'],$i);
		}
		$this->assertFalse($reader->read());
	}

	public function testReadColumn()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		$this->assertEquals($reader->readColumn(0),1);
		$this->assertEquals($reader->readColumn(1),'post 2');
		$reader->readColumn(0);
		$reader->readColumn(0);
		$this->assertEquals($reader->readColumn(0),5);
		$this->assertFalse($reader->readColumn(0));
	}

	public function testReadObject()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		$object=$reader->readObject('PostRecord',array(null,'v2'));
		$this->assertEquals($object->id,1);
		$this->assertEquals($object->title,'post 1');
		$this->assertEquals($object->param1,null);
		$this->assertEquals($object->param2,'v2');
	}

	public function testReadAll()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		$rows=$reader->readAll();
		$this->assertEquals(count($rows),5);
		$row=$rows[2];
		$this->assertEquals($row['id'],3);
		$this->assertEquals($row['title'],'post 3');

		$reader=$this->_connection->createCommand('SELECT * FROM posts WHERE id=10')->query();
		$this->assertEquals($reader->readAll(),array());
	}

	public function testClose()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		$row=$reader->read();
		$row=$reader->read();
		$this->assertFalse($reader->isClosed);
		$reader->close();
		$this->assertTrue($reader->isClosed);
	}

	public function testRowCount()
	{
		// unable to test because SQLite doesn't support row count
	}

	public function testColumnCount()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		$this->assertEquals($reader->columnCount,5);

		$reader=$this->_connection->createCommand('SELECT * FROM posts WHERE id=11')->query();
		$this->assertEquals($reader->ColumnCount,5);
	}

	public function testForeach()
	{
		$ids=array();
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		foreach($reader as $row)
			$ids[]=$row['id'];
		$this->assertEquals(count($ids),5);
		$this->assertEquals($ids[3],4);

		$this->setExpectedException('CException');
		foreach($reader as $row)
			$ids[]=$row['id'];
	}

	public function testFetchMode()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();

		$reader->fetchMode=PDO::FETCH_NUM;
		$row=$reader->read();
		$this->assertFalse(isset($row['id']));
		$this->assertTrue(isset($row[0]));

		$reader->fetchMode=PDO::FETCH_ASSOC;
		$row=$reader->read();
		$this->assertTrue(isset($row['id']));
		$this->assertFalse(isset($row[0]));
	}

	public function testBindColumn()
	{
		$reader=$this->_connection->createCommand('SELECT * FROM posts')->query();
		$reader->bindColumn(1,$id);
		$reader->bindColumn(2,$title);
		$reader->read();
		$this->assertEquals($id,1);
		$this->assertEquals($title,'post 1');
		$reader->read();
		$this->assertEquals($id,2);
		$this->assertEquals($title,'post 2');
	}
}


code:
<?php

Yii::import('system.db.CDbConnection');

class CDbTransactionTest extends CTestCase
{
	private $_connection;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->_connection=new CDbConnection('sqlite::memory:');
		$this->_connection->active=true;
		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/data/sqlite.sql'));
	}

	public function tearDown()
	{
		$this->_connection->active=false;
	}

	public function testBeginTransaction()
	{
		$sql='INSERT INTO posts(id,title,create_time,author_id) VALUES(10,\'test post\',11000,1)';
		$transaction=$this->_connection->beginTransaction();
		try
		{
			$this->_connection->createCommand($sql)->execute();
			$this->_connection->createCommand($sql)->execute();
			$this->fail('Expected exception not raised');
			$transaction->commit();
		}
		catch(Exception $e)
		{
			$transaction->rollBack();
			$reader=$this->_connection->createCommand('SELECT * FROM posts WHERE id=10')->query();
			$this->assertFalse($reader->read());
		}
	}

	public function testCommit()
	{
		$sql='INSERT INTO posts(id,title,create_time,author_id) VALUES(10,\'test post\',11000,1)';
		$transaction=$this->_connection->beginTransaction();
		try
		{
			$this->_connection->createCommand($sql)->execute();
			$this->assertTrue($transaction->active);
			$transaction->commit();
			$this->assertFalse($transaction->active);
		}
		catch(Exception $e)
		{
			$transaction->rollBack();
			$this->fail('Unexpected exception');
		}
		$n=$this->_connection->createCommand('SELECT COUNT(*) FROM posts WHERE id=10')->queryScalar();
		$this->assertEquals($n,1);
	}
}


code:
<?php

Yii::import('system.db.CDbConnection');
Yii::import('system.db.ar.CActiveRecord');
require_once(dirname(__FILE__).'/../data/models2.php');

class CActiveRecord2Test extends CTestCase
{
	private $db;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_pgsql'))
			$this->markTestSkipped('PDO and PostgreSQL extensions are required.');

		$this->db=new CDbConnection('pgsql:host=127.0.0.1;dbname=yii','test','test');
		try
		{
			$this->db->active=true;
		}
		catch(Exception $e)
		{
			$schemaFile=realpath(dirname(__FILE__).'/../data/postgres.sql');
			$this->markTestSkipped("Please read $schemaFile for details on setting up the test environment for PostgreSQL test case.");
		}

		try	{ $this->db->createCommand('DROP SCHEMA test CASCADE')->execute(); } catch(Exception $e) { }
		try	{ $this->db->createCommand('DROP TABLE yii_types CASCADE')->execute(); } catch(Exception $e) { }

		$sqls=file_get_contents(dirname(__FILE__).'/../data/postgres.sql');
		foreach(explode(';',$sqls) as $sql)
		{
			if(trim($sql)!=='')
				$this->db->createCommand($sql)->execute();
		}
		$this->db->active=false;

		$config=array(
			'basePath'=>dirname(__FILE__),
			'components'=>array(
				'db'=>array(
					'class'=>'system.db.CDbConnection',
					'connectionString'=>'pgsql:host=127.0.0.1;dbname=yii',
					'username'=>'test',
					'password'=>'test',
				),
			),
		);
		$app=new TestApplication($config);
		$app->db->active=true;
		CActiveRecord::$db=$this->db=$app->db;
	}

	public function tearDown()
	{
		$this->db->active=false;
	}

	public function testModel()
	{
		$model=Post2::model();
		$this->assertTrue($model instanceof Post2);
		$this->assertTrue($model->dbConnection===$this->db);
		$this->assertTrue($model->dbConnection->active);
		$this->assertEquals('test.posts',$model->tableName());
		$this->assertEquals('id',$model->tableSchema->primaryKey);
		$this->assertEquals('test.posts_id_seq',$model->tableSchema->sequenceName);
		$this->assertEquals(array(),$model->attributeLabels());
		$this->assertEquals('Id',$model->getAttributeLabel('id'));
		$this->assertEquals('Author Id',$model->getAttributeLabel('author_id'));
		$this->assertTrue($model->getActiveRelation('author') instanceof CBelongsToRelation);
		$this->assertTrue($model->tableSchema instanceof CDbTableSchema);
		$this->assertTrue($model->commandBuilder instanceof CDbCommandBuilder);
		$this->assertTrue($model->hasAttribute('id'));
		$this->assertFalse($model->hasAttribute('comments'));
		$this->assertFalse($model->hasAttribute('foo'));
		$this->assertEquals(array(),$model->getAttributes(false));

		$post=new Post2;
		$this->assertNull($post->id);
		$this->assertNull($post->title);
		$post->setAttributes(array('id'=>3,'title'=>'test title'));
		$this->assertNull($post->id);
		$this->assertEquals('test title',$post->title);
	}

	public function testFind()
	{
		// test find() with various parameters
		$post=Post2::model()->find();
		$this->assertTrue($post instanceof Post2);
		$this->assertEquals(1,$post->id);

		$post=Post2::model()->find('id=5');
		$this->assertTrue($post instanceof Post2);
		$this->assertEquals(5,$post->id);

		$post=Post2::model()->find('id=:id',array(':id'=>2));
		$this->assertTrue($post instanceof Post2);
		$this->assertEquals(2,$post->id);

		$post=Post2::model()->find(array('condition'=>'id=:id','params'=>array(':id'=>3)));
		$this->assertTrue($post instanceof Post2);
		$this->assertEquals(3,$post->id);

		// test find() without result
		$post=Post2::model()->find('id=6');
		$this->assertNull($post);

		// test findAll() with various parameters
		$posts=Post2::model()->findAll();
		$this->assertEquals(5,count($posts));
		$this->assertTrue($posts[3] instanceof Post2);
		$this->assertEquals(4,$posts[3]->id);

		$posts=Post2::model()->findAll(new CDbCriteria(array('limit'=>3,'offset'=>1)));
		$this->assertEquals(3,count($posts));
		$this->assertTrue($posts[2] instanceof Post2);
		$this->assertEquals(4,$posts[2]->id);

		// test findAll() without result
		$posts=Post2::model()->findAll('id=6');
		$this->assertTrue($posts===array());

		// test findByPk
		$post=Post2::model()->findByPk(2);
		$this->assertEquals(2,$post->id);

		$post=Post2::model()->findByPk(array(3,2));
		$this->assertEquals(2,$post->id);

		$post=Post2::model()->findByPk(array());
		$this->assertNull($post);

		$post=Post2::model()->findByPk(6);
		$this->assertNull($post);

		// test findAllByPk
		$posts=Post2::model()->findAllByPk(2);
		$this->assertEquals(1,count($posts));
		$this->assertEquals(2,$posts[0]->id);

		$posts=Post2::model()->findAllByPk(array(4,3,2),'id<4');
		$this->assertEquals(2,count($posts));
		$this->assertEquals(2,$posts[0]->id);
		$this->assertEquals(3,$posts[1]->id);

		$posts=Post2::model()->findAllByPk(array());
		$this->assertTrue($posts===array());

		// test findByAttributes
		$post=Post2::model()->findByAttributes(array('author_id'=>2),array('order'=>'id DESC'));
		$this->assertEquals(4,$post->id);

		// test findAllByAttributes
		$posts=Post2::model()->findAllByAttributes(array('author_id'=>2));
		$this->assertEquals(3,count($posts));

		// test findBySql
		$post=Post2::model()->findBySql('select * from test.posts where id=:id',array(':id'=>2));
		$this->assertEquals(2,$post->id);

		// test findAllBySql
		$posts=Post2::model()->findAllBySql('select * from test.posts where id>:id',array(':id'=>2));
		$this->assertEquals(3,count($posts));

		// test count
		$this->assertEquals(5,Post2::model()->count());
		$this->assertEquals(3,Post2::model()->count(array('condition'=>'id>2')));

		// test countBySql
		$this->assertEquals(1,Post2::model()->countBySql('select id from test.posts limit 1'));

		// test exists
		$this->assertTrue(Post2::model()->exists('id=:id',array(':id'=>1)));
		$this->assertFalse(Post2::model()->exists('id=:id',array(':id'=>6)));
	}

	public function testInsert()
	{
		$post=new Post2;
		$this->assertEquals(array(),$post->getAttributes(false));
		$post->title='test post 1';
		$post->create_time='2004-10-19 10:23:54';
		$post->author_id=1;
		$post->content='test post content 1';
		$this->assertTrue($post->isNewRecord);
		$this->assertNull($post->id);
		$this->assertTrue($post->save());
		$this->assertEquals(array(
			'id'=>6,
			'title'=>'test post 1',
			'create_time'=>$post->create_time,
			'author_id'=>1,
			'content'=>'test post content 1'),$post->getAttributes());
		$this->assertFalse($post->isNewRecord);
		$this->assertEquals($post->getAttributes(false),Post2::model()->findByPk($post->id)->getAttributes(false));
	}

	public function testUpdate()
	{
		// test save
		$post=Post2::model()->findByPk(1);
		$this->assertFalse($post->isNewRecord);
		$this->assertEquals('post 1',$post->title);
		$post->title='test post 1';
		$this->assertTrue($post->save());
		$this->assertFalse($post->isNewRecord);
		$this->assertEquals('test post 1',$post->title);
		$this->assertEquals('test post 1',Post2::model()->findByPk(1)->title);

		// test updateByPk
		$this->assertEquals(2,Post2::model()->updateByPk(array(4,5),array('title'=>'test post')));
		$this->assertEquals('post 2',Post2::model()->findByPk(2)->title);
		$this->assertEquals('test post',Post2::model()->findByPk(4)->title);
		$this->assertEquals('test post',Post2::model()->findByPk(5)->title);

		// test updateAll
		$this->assertEquals(1,Post2::model()->updateAll(array('title'=>'test post'),'id=1'));
		$this->assertEquals('test post',Post2::model()->findByPk(1)->title);

		// test updateCounters
		$this->assertEquals(2,Post2::model()->findByPk(2)->author_id);
		$this->assertEquals(2,Post2::model()->findByPk(3)->author_id);
		$this->assertEquals(2,Post2::model()->findByPk(4)->author_id);
		$this->assertEquals(3,Post2::model()->updateCounters(array('author_id'=>-1),'id>2'));
		$this->assertEquals(2,Post2::model()->findByPk(2)->author_id);
		$this->assertEquals(1,Post2::model()->findByPk(3)->author_id);
		$this->assertEquals(1,Post2::model()->findByPk(4)->author_id);
		$this->assertEquals(2,Post2::model()->findByPk(5)->author_id);
	}

	public function testDelete()
	{
		$post=Post2::model()->findByPk(1);
		$this->assertTrue($post->delete());
		$this->assertNull(Post2::model()->findByPk(1));

		$this->assertTrue(Post2::model()->findByPk(2) instanceof Post2);
		$this->assertTrue(Post2::model()->findByPk(3) instanceof Post2);
		$this->assertEquals(2,Post2::model()->deleteByPk(array(2,3)));
		$this->assertNull(Post2::model()->findByPk(2));
		$this->assertNull(Post2::model()->findByPk(3));

		$this->assertTrue(Post2::model()->findByPk(5) instanceof Post2);
		$this->assertEquals(1,Post2::model()->deleteAll('id=5'));
		$this->assertNull(Post2::model()->findByPk(5));
	}

	public function testRefresh()
	{
		$post=Post2::model()->findByPk(1);
		$post2=Post2::model()->findByPk(1);
		$post2->title='new post';
		$post2->save();
		$this->assertEquals('post 1',$post->title);
		$this->assertTrue($post->refresh());
		$this->assertEquals('new post',$post->title);
	}

	public function testEquals()
	{
		$post=Post2::model()->findByPk(1);
		$post2=Post2::model()->findByPk(1);
		$post3=Post2::model()->findByPk(3);
		$this->assertEquals(1,$post->primaryKey);
		$this->assertTrue($post->equals($post2));
		$this->assertTrue($post2->equals($post));
		$this->assertFalse($post->equals($post3));
		$this->assertFalse($post3->equals($post));
	}

	public function testValidation()
	{
		$user=new User2;
		$user->password='passtest';
		$this->assertFalse($user->hasErrors());
		$this->assertEquals(array(),$user->errors);
		$this->assertEquals(array(),$user->getErrors('username'));
		$this->assertFalse($user->save());
		$this->assertNull($user->id);
		$this->assertTrue($user->isNewRecord);
		$this->assertTrue($user->hasErrors());
		$this->assertTrue($user->hasErrors('username'));
		$this->assertTrue($user->hasErrors('email'));
		$this->assertFalse($user->hasErrors('password'));
		$this->assertEquals(1,count($user->getErrors('username')));
		$this->assertEquals(1,count($user->getErrors('email')));
		$this->assertEquals(2,count($user->errors));

		$user->clearErrors();
		$this->assertFalse($user->hasErrors());
		$this->assertEquals(array(),$user->errors);
	}

	public function testCompositeKey()
	{
		$order=new Order2;
		$this->assertEquals(array('key1','key2'),$order->tableSchema->primaryKey);
		$order=Order2::model()->findByPk(array('key1'=>2,'key2'=>1));
		$this->assertEquals('order 21',$order->name);
		$orders=Order2::model()->findAllByPk(array(array('key1'=>2,'key2'=>1),array('key1'=>1,'key2'=>3)));
		$this->assertEquals('order 13',$orders[0]->name);
		$this->assertEquals('order 21',$orders[1]->name);
	}

	public function testDefault()
	{
		$type=new ComplexType2;
		$this->assertEquals(1,$type->int_col2);
		$this->assertEquals('something',$type->char_col2);
		$this->assertEquals(1.23,$type->real_col);
		$this->assertEquals(null,$type->numeric_col);
		$this->assertEquals(null,$type->time);
		$this->assertEquals(null,$type->bool_col);
		$this->assertEquals(true,$type->bool_col2);
	}

	public function testPublicAttribute()
	{
		$post=new PostExt2;
		$this->assertEquals(array('id'=>null,'title'=>'default title'),$post->getAttributes(false));
		$post=Post2::model()->findByPk(1);
		$this->assertEquals(array(
			'id'=>1,
			'title'=>'post 1',
			'create_time'=>'2004-10-19 10:23:54',
			'author_id'=>1,
			'content'=>'content 1'),$post->getAttributes(false));

		$post=new PostExt2;
		$post->title='test post';
		$post->create_time='2004-10-19 10:23:53';
		$post->author_id=1;
		$post->content='test';
		$post->save();
		$this->assertEquals(array(
			'id'=>6,
			'title'=>'test post',
			'create_time'=>'2004-10-19 10:23:53',
			'author_id'=>1,
			'content'=>'test'),$post->getAttributes(false));
	}

	public function testLazyRelation()
	{
		// test belongsTo
		$post=Post2::model()->findByPk(2);
		$this->assertTrue($post->author instanceof User2);
		$this->assertEquals(array(
			'id'=>2,
			'username'=>'user2',
			'password'=>'pass2',
			'email'=>'email2'),$post->author->getAttributes(false));

		// test hasOne
		$post=Post2::model()->findByPk(2);
		$this->assertTrue($post->firstComment instanceof Comment2);
		$this->assertEquals(array(
			'id'=>4,
			'content'=>'comment 4',
			'post_id'=>2,
			'author_id'=>2),$post->firstComment->getAttributes(false));
		$post=Post2::model()->findByPk(4);
		$this->assertNull($post->firstComment);

		// test hasMany
		$post=Post2::model()->findByPk(2);
		$this->assertEquals(2,count($post->comments));
		$this->assertEquals(array(
			'id'=>5,
			'content'=>'comment 5',
			'post_id'=>2,
			'author_id'=>2),$post->comments[0]->getAttributes(false));
		$this->assertEquals(array(
			'id'=>4,
			'content'=>'comment 4',
			'post_id'=>2,
			'author_id'=>2),$post->comments[1]->getAttributes(false));
		$post=Post2::model()->findByPk(4);
		$this->assertEquals(array(),$post->comments);

		// test manyMany
		$post=Post2::model()->findByPk(2);
		$this->assertEquals(2,count($post->categories));

		// TODO: when joining, need to replace both placeholders for the two joinin tables
		$this->assertEquals(array(
			'id'=>4,
			'name'=>'cat 4',
			'parent_id'=>1),$post->categories[0]->getAttributes(false));
		$this->assertEquals(array(
			'id'=>1,
			'name'=>'cat 1',
			'parent_id'=>null),$post->categories[1]->getAttributes(false));

		$post=Post2::model()->findByPk(4);
		$this->assertEquals(array(),$post->categories);

		// test self join
		$category=Category2::model()->findByPk(5);
		$this->assertEquals(array(),$category->posts);
		$this->assertEquals(2,count($category->children));
		$this->assertEquals(array(
			'id'=>6,
			'name'=>'cat 6',
			'parent_id'=>5),$category->children[0]->getAttributes(false));
		$this->assertEquals(array(
			'id'=>7,
			'name'=>'cat 7',
			'parent_id'=>5),$category->children[1]->getAttributes(false));
		$this->assertTrue($category->parent instanceof Category2);
		$this->assertEquals(array(
			'id'=>1,
			'name'=>'cat 1',
			'parent_id'=>null),$category->parent->getAttributes(false));

		$category=Category2::model()->findByPk(2);
		$this->assertEquals(1,count($category->posts));
		$this->assertEquals(array(),$category->children);
		$this->assertNull($category->parent);

		// test composite key
		$order=Order2::model()->findByPk(array('key1'=>1,'key2'=>2));
		$this->assertEquals(2,count($order->items));
		$order=Order2::model()->findByPk(array('key1'=>2,'key2'=>1));
		$this->assertEquals(0,count($order->items));
		$item=Item2::model()->findByPk(4);
		$this->assertTrue($item->order instanceof Order2);
		$this->assertEquals(array(
			'key1'=>2,
			'key2'=>2,
			'name'=>'order 22'),$item->order->getAttributes(false));
	}

	public function testEagerRelation()
	{
		$post=Post2::model()->with('author','firstComment','comments','categories')->findByPk(2);
		$this->assertEquals(array(
			'id'=>2,
			'username'=>'user2',
			'password'=>'pass2',
			'email'=>'email2'),$post->author->getAttributes(false));
		$this->assertTrue($post->firstComment instanceof Comment2);
		$this->assertEquals(array(
			'id'=>4,
			'content'=>'comment 4',
			'post_id'=>2,
			'author_id'=>2),$post->firstComment->getAttributes(false));
		$this->assertEquals(2,count($post->comments));
		$this->assertEquals(array(
			'id'=>5,
			'content'=>'comment 5',
			'post_id'=>2,
			'author_id'=>2),$post->comments[0]->getAttributes(false));
		$this->assertEquals(array(
			'id'=>4,
			'content'=>'comment 4',
			'post_id'=>2,
			'author_id'=>2),$post->comments[1]->getAttributes(false));
		$this->assertEquals(2,count($post->categories));

		$this->assertEquals(array(
			'id'=>4,
			'name'=>'cat 4',
			'parent_id'=>1),$post->categories[0]->getAttributes(false));
		$this->assertEquals(array(
			'id'=>1,
			'name'=>'cat 1',
			'parent_id'=>null),$post->categories[1]->getAttributes(false));

		$post=Post2::model()->with('author','firstComment','comments','categories')->findByPk(4);
		$this->assertEquals(array(
			'id'=>2,
			'username'=>'user2',
			'password'=>'pass2',
			'email'=>'email2'),$post->author->getAttributes(false));
		$this->assertNull($post->firstComment);
		$this->assertEquals(array(),$post->comments);
		$this->assertEquals(array(),$post->categories);
	}

	public function testLazyRecursiveRelation()
	{
		$post=PostExt2::model()->findByPk(2);
		$this->assertEquals(2,count($post->comments));
		$this->assertTrue($post->comments[0]->post instanceof Post2);
		$this->assertTrue($post->comments[1]->post instanceof Post2);
		$this->assertTrue($post->comments[0]->author instanceof User2);
		$this->assertTrue($post->comments[1]->author instanceof User2);
		$this->assertEquals(3,count($post->comments[0]->author->posts));
		$this->assertEquals(3,count($post->comments[1]->author->posts));
		$this->assertTrue($post->comments[0]->author->posts[1]->author instanceof User2);

		// test self join
		$category=Category2::model()->findByPk(1);
		$this->assertEquals(2,count($category->nodes));
		$this->assertTrue($category->nodes[0]->parent instanceof Category2);
		$this->assertTrue($category->nodes[1]->parent instanceof Category2);
		$this->assertEquals(0,count($category->nodes[0]->children));
		$this->assertEquals(2,count($category->nodes[1]->children));
	}

	public function testEagerRecursiveRelation()
	{
		$post=Post2::model()->with(array('comments'=>'author','categories'))->findByPk(2);
		$this->assertEquals(2,count($post->comments));
		$this->assertEquals(2,count($post->categories));
	}

	public function testRelationWithCondition()
	{
		$posts=Post2::model()->with('comments')->findAllByPk(array(2,3,4),array('order'=>'t.id'));
		$this->assertEquals(3,count($posts));
		$this->assertEquals(2,count($posts[0]->comments));
		$this->assertEquals(4,count($posts[1]->comments));
		$this->assertEquals(0,count($posts[2]->comments));

		$post=Post2::model()->with('comments')->findByAttributes(array('id'=>2));
		$this->assertTrue($post instanceof Post2);
		$this->assertEquals(2,count($post->comments));
		$posts=Post2::model()->with('comments')->findAllByAttributes(array('id'=>2));
		$this->assertEquals(1,count($posts));

		$post=Post2::model()->with('comments')->findBySql('select * from test.posts where id=:id',array(':id'=>2));
		$this->assertTrue($post instanceof Post2);
		$posts=Post2::model()->with('comments')->findAllBySql('select * from test.posts where id=:id1 OR id=:id2',array(':id1'=>2,':id2'=>3));
		$this->assertEquals(2,count($posts));

		$post=Post2::model()->with('comments','author')->find('t.id=:id',array(':id'=>2));
		$this->assertTrue($post instanceof Post2);

		$posts=Post2::model()->with('comments','author')->findAll(array(
			'select'=>'title',
			'condition'=>'t.id=:id',
			'limit'=>1,
			'offset'=>0,
			'order'=>'t.title',
			'params'=>array(':id'=>2)));
		$this->assertTrue($posts[0] instanceof Post2);

		$posts=Post2::model()->with('comments','author')->findAll(array(
			'select'=>'title',
			'condition'=>'t.id=:id',
			'limit'=>1,
			'offset'=>2,
			'order'=>'t.title',
			'params'=>array(':id'=>2)));
		$this->assertTrue($posts===array());
	}

	public function testSelfManyMany()
	{
		$user=User2::model()->findByPk(1);
		$this->assertTrue($user instanceof User2);
		$friends=$user->friends;
		$this->assertEquals(count($friends),2);
		$this->assertEquals($friends[0]->id,2);
		$this->assertEquals($friends[1]->id,3);

		$user=User2::model()->with('friends')->findByPk(1);
		$this->assertTrue($user instanceof User2);
		$friends=$user->friends;
		$this->assertEquals(count($friends),2);
		$this->assertEquals($friends[0]->id,2);
		$this->assertEquals($friends[1]->id,3);
	}

	public function testRelationalCount()
	{
		$count=Post2::model()->with('author','firstComment','comments','categories')->count();
		$this->assertEquals(5,$count);

		$count=Post2::model()->with('author','firstComment','comments','categories')->count('t.id=4');
		$this->assertEquals(1,$count);

		$count=Post2::model()->with('author','firstComment','comments','categories')->count('t.id=14');
		$this->assertEquals(0,$count);
	}

	public function testEmptyFinding()
	{
		$post=Post2::model()->with('author','firstComment','comments','categories')->find('t.id=100');
		$this->assertNull($post);

		$posts=Post2::model()->with('author','firstComment','comments','categories')->findAll('t.id=100');
		$this->assertTrue($posts===array());

		$post=Post2::model()->with('author','firstComment','comments','categories')->findBySql('SELECT * FROM test.posts WHERE id=100');
		$this->assertNull($post);

		Post2::model()->with('author','firstComment','comments','categories')->findAllBySql('SELECT * FROM test.posts WHERE id=100');
		$this->assertTrue($posts===array());
	}
}


code:
<?php

Yii::import('system.db.CDbConnection');
Yii::import('system.db.ar.CActiveRecord');

require_once(dirname(__FILE__).'/../data/models.php');

class CActiveRecordEventWrappersTest extends CTestCase
{
	private $_connection;

	public function setUp()
	{
		if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
			$this->markTestSkipped('PDO and SQLite extensions are required.');

		$this->_connection=new CDbConnection('sqlite::memory:');
		$this->_connection->active=true;
		$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/../data/sqlite.sql'));
		CActiveRecord::$db=$this->_connection;

		UserWithWrappers::clearCounters();
		PostWithWrappers::clearCounters();
		CommentWithWrappers::clearCounters();
	}

	public function tearDown()
	{
		$this->_connection->active=false;
	}

	public function testBeforeFind()
	{
		UserWithWrappers::model()->find();
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findByAttributes(array('username'=>'user1'));
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findByPk(1);
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findAll();
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findAllByAttributes(array('username'=>'user1'));
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findAllByPk(1);
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->findAllBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
	}

	public function testBeforeFindRelationalEager()
	{
		UserWithWrappers::model()->with('posts.comments')->find();
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findByAttributes(array('username'=>'user1'));
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findByPk(1);
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findAll();
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findAllByAttributes(array('username'=>'user1'));
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findAllByPk(1);
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
		UserWithWrappers::model()->with('posts.comments')->findAllBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
	}

	public function testBeforeFindRelationalLazy()
	{
		$user=UserWithWrappers::model()->find();
		$user->posts;
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$user=UserWithWrappers::model()->find();
		$user->posts(array('with'=>'comments'));
		$this->assertEquals(UserWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('beforeFind'),1);
		$this->assertEquals(CommentWithWrappers::getCounter('beforeFind'),1);
	}

	public function testAfterFind()
	{
		UserWithWrappers::model()->find();
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		UserWithWrappers::model()->findByAttributes(array('username'=>'user1'));
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		UserWithWrappers::model()->findByPk(1);
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		UserWithWrappers::model()->findBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		UserWithWrappers::model()->findAll();
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),4);
		UserWithWrappers::model()->findAllByAttributes(array('username'=>'user1'));
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		UserWithWrappers::model()->findAllByPk(1);
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		UserWithWrappers::model()->findAllBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),4);
	}

	public function testAfterFindRelational()
	{
		UserWithWrappers::model()->with('posts.comments')->find();
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),4);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),5);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),10);
		UserWithWrappers::model()->with('posts.comments')->findByAttributes(array('username'=>'user2'));
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),3);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),6);
		UserWithWrappers::model()->with('posts.comments')->findByPk(2);
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),3);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),6);
		UserWithWrappers::model()->with('posts.comments')->findBySql('SELECT * FROM users WHERE id=2');
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),3);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),6);
		UserWithWrappers::model()->with('posts.comments')->findAll();
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),4);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),5);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),10);
		UserWithWrappers::model()->with('posts.comments')->findAllByAttributes(array('username'=>'user2'));
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),3);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),6);
		UserWithWrappers::model()->with('posts.comments')->findAllByPk(2);
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),1);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),3);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),6);
		UserWithWrappers::model()->with('posts.comments')->findAllBySql('SELECT * FROM users');
		$this->assertEquals(UserWithWrappers::getCounter('afterFind'),4);
		$this->assertEquals(PostWithWrappers::getCounter('afterFind'),5);
		$this->assertEquals(CommentWithWrappers::getCounter('afterFind'),10);
	}
}