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 2008-2013 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: *
35: * @author Qiang Xue <qiang.xue@gmail.com>
36: * @package system.test
37: * @since 1.1
38: */
39: abstract class CDbTestCase extends CTestCase
40: {
41: /**
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.
47: */
48: protected $fixtures=false;
49:
50: /**
51: * PHP magic method.
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: * @throws Exception if unknown property is used
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: * @throws Exception if unknown method is used
71: * @return mixed the property value
72: */
73: public function __call($name,$params)
74: {
75: if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
76: return $record;
77: else
78: throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
79: }
80:
81: /**
82: * @return CDbFixtureManager the database fixture manager
83: */
84: public function getFixtureManager()
85: {
86: return Yii::app()->getComponent('fixture');
87: }
88:
89: /**
90: * @param string $name the fixture name (the key value in {@link fixtures}).
91: * @return array the named fixture data
92: */
93: public function getFixtureData($name)
94: {
95: return $this->getFixtureManager()->getRows($name);
96: }
97:
98: /**
99: * @param string $name the fixture name (the key value in {@link fixtures}).
100: * @param string $alias the alias of the fixture data row
101: * @return CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture.
102: * False is returned if there is no such fixture or the record cannot be found.
103: */
104: public function getFixtureRecord($name,$alias)
105: {
106: return $this->getFixtureManager()->getRecord($name,$alias);
107: }
108:
109: /**
110: * Sets up the fixture before executing a test method.
111: * If you override this method, make sure the parent implementation is invoked.
112: * Otherwise, the database fixtures will not be managed properly.
113: */
114: protected function setUp()
115: {
116: parent::setUp();
117: if(is_array($this->fixtures))
118: $this->getFixtureManager()->load($this->fixtures);
119: }
120: }