Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
CDbTestCase.php
1 <?php
2 /**
3  * This file contains the CDbTestCase class.
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.yiiframework.com/
7  * @copyright Copyright &copy; 2008-2011 Yii Software LLC
8  * @license http://www.yiiframework.com/license/
9  */
10 
11 Yii::import('system.test.CTestCase');
12 
13 /**
14  * CDbTestCase is the base class for test cases about DB-related features.
15  *
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.
20  *
21  * For example, assume we declare {@link fixtures} to be:
22  * <pre>
23  * public $fixtures=array(
24  * 'posts' => 'Post',
25  * 'comments' => 'Comment',
26  * );
27  * </pre>
28  *
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.
33  *
34  * @property CDbFixtureManager $fixtureManager The database fixture manager.
35  *
36  * @author Qiang Xue <qiang.xue@gmail.com>
37  * @package system.test
38  * @since 1.1
39  */
40 abstract class CDbTestCase extends CTestCase
41 {
42  /**
43  * @var array a list of fixtures that should be loaded before each test method executes.
44  * The array keys are fixture names, and the array values are either AR class names
45  * or table names. If table names, they must begin with a colon character (e.g. 'Post'
46  * means an AR class, while ':post' means a table name).
47  * Defaults to false, meaning fixtures will not be used at all.
48  */
49  protected $fixtures=false;
50 
51  /**
52  * PHP magic method.
53  * This method is overridden so that named fixture data can be accessed like a normal property.
54  * @param string $name the property name
55  * @return mixed the property value
56  */
57  public function __get($name)
58  {
59  if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
60  return $rows;
61  else
62  throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
63  }
64 
65  /**
66  * PHP magic method.
67  * This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.
68  * @param string $name method name
69  * @param string $params method parameters
70  * @return mixed the property value
71  */
72  public function __call($name,$params)
73  {
74  if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
75  return $record;
76  else
77  throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
78  }
79 
80  /**
81  * @return CDbFixtureManager the database fixture manager
82  */
83  public function getFixtureManager()
84  {
85  return Yii::app()->getComponent('fixture');
86  }
87 
88  /**
89  * @param string $name the fixture name (the key value in {@link fixtures}).
90  * @return array the named fixture data
91  */
92  public function getFixtureData($name)
93  {
94  return $this->getFixtureManager()->getRows($name);
95  }
96 
97  /**
98  * @param string $name the fixture name (the key value in {@link fixtures}).
99  * @param string $alias the alias of the fixture data row
100  * @return CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture.
101  * False is returned if there is no such fixture or the record cannot be found.
102  */
103  public function getFixtureRecord($name,$alias)
104  {
105  return $this->getFixtureManager()->getRecord($name,$alias);
106  }
107 
108  /**
109  * Sets up the fixture before executing a test method.
110  * If you override this method, make sure the parent implementation is invoked.
111  * Otherwise, the database fixtures will not be managed properly.
112  */
113  protected function setUp()
114  {
115  parent::setUp();
116  if(is_array($this->fixtures))
117  $this->getFixtureManager()->load($this->fixtures);
118  }
119 }