3
* This file contains the CDbTestCase class.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
11
Yii::import('system.test.CTestCase');
14
* CDbTestCase is the base class for test cases about DB-related features.
16
* CDbTestCase provides database fixture management with the help of {@link CDbFixtureManager}.
17
* By declaring {@link fixtures} property, one can ensure the specified
18
* tables have the expected fixture state when executing each test method.
19
* In addition, CDbTestCase provides two ways to access the fixture data.
21
* For example, assume we declare {@link fixtures} to be:
23
* public $fixtures=array(
25
* 'comments' => 'Comment',
29
* We can access the original fixture data rows using <code>$this->posts</code>
30
* <code>$this->posts['first post']</code>. We can also retrieve an ActiveRecord instance
31
* corresponding to a fixture data row using <code>$this->posts('first post')</code>.
32
* Note, here 'first post' refers to a key to a row in the original fixture data.
34
* @author Qiang Xue <qiang.xue@gmail.com>
35
* @version $Id: CDbTestCase.php 2799 2011-01-01 19:31:13Z qiang.xue $
36
* @package system.test
39
abstract class CDbTestCase extends CTestCase
42
* @var array a list of fixtures that should be loaded before each test method executes.
43
* The array keys are fixture names, and the array values are either AR class names
44
* or table names. If table names, they must begin with a colon character (e.g. 'Post'
45
* means an AR class, while ':Post' means a table name).
46
* Defaults to false, meaning fixtures will not be used at all.
48
protected $fixtures=false;
52
* This method is overridden so that named fixture data can be accessed like a normal property.
53
* @param string $name the property name
54
* @return mixed the property value
56
public function __get($name)
58
if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
61
throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
66
* This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.
67
* @param string $name method name
68
* @param string $params method parameters
69
* @return mixed the property value
71
public function __call($name,$params)
73
if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
76
throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
80
* @return CDbFixtureManager the database fixture manager
82
public function getFixtureManager()
84
return Yii::app()->getComponent('fixture');
88
* @param string $name the fixture name (the key value in {@link fixtures}).
89
* @return array the named fixture data
91
public function getFixtureData($name)
93
return $this->getFixtureManager()->getRows($name);
97
* @param string $name the fixture name (the key value in {@link fixtures}).
98
* @param string $alias the alias of the fixture data row
99
* @return CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture.
100
* False is returned if there is no such fixture or the record cannot be found.
102
public function getFixtureRecord($name,$alias)
104
return $this->getFixtureManager()->getRecord($name,$alias);
108
* Sets up the fixture before executing a test method.
109
* If you override this method, make sure the parent implementation is invoked.
110
* Otherwise, the database fixtures will not be managed properly.
112
protected function setUp()
115
if(is_array($this->fixtures))
116
$this->getFixtureManager()->load($this->fixtures);
b'\\ No newline at end of file'