1: <?php
2: /**
3: * This file contains the CWebTestCase 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: require_once('PHPUnit/Extensions/SeleniumTestCase.php');
13:
14: /**
15: * CWebTestCase is the base class for Web-based functional test case classes.
16: *
17: * It extends PHPUnit_Extensions_SeleniumTestCase and provides the database
18: * fixture management feature like {@link CDbTestCase}.
19: *
20: * @property CDbFixtureManager $fixtureManager The database fixture manager.
21: *
22: * @author Qiang Xue <qiang.xue@gmail.com>
23: * @package system.test
24: * @since 1.1
25: */
26: abstract class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase
27: {
28: /**
29: * @var array a list of fixtures that should be loaded before each test method executes.
30: * The array keys are fixture names, and the array values are either AR class names
31: * or table names. If table names, they must begin with a colon character (e.g. 'Post'
32: * means an AR class, while ':Post' means a table name).
33: * Defaults to false, meaning fixtures will not be used at all.
34: */
35: protected $fixtures=false;
36:
37: /**
38: * PHP magic method.
39: * This method is overridden so that named fixture data can be accessed like a normal property.
40: * @param string $name the property name
41: * @throws Exception if unknown property is used
42: * @return mixed the property value
43: */
44: public function __get($name)
45: {
46: if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
47: return $rows;
48: else
49: throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
50: }
51:
52: /**
53: * PHP magic method.
54: * This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.
55: * @param string $name method name
56: * @param string $params method parameters
57: * @return mixed the property value
58: */
59: public function __call($name,$params)
60: {
61: if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
62: return $record;
63: else
64: return parent::__call($name,$params);
65: }
66:
67: /**
68: * @return CDbFixtureManager the database fixture manager
69: */
70: public function getFixtureManager()
71: {
72: return Yii::app()->getComponent('fixture');
73: }
74:
75: /**
76: * @param string $name the fixture name (the key value in {@link fixtures}).
77: * @return array the named fixture data
78: */
79: public function getFixtureData($name)
80: {
81: return $this->getFixtureManager()->getRows($name);
82: }
83:
84: /**
85: * @param string $name the fixture name (the key value in {@link fixtures}).
86: * @param string $alias the alias of the fixture data row
87: * @return CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture.
88: * False is returned if there is no such fixture or the record cannot be found.
89: */
90: public function getFixtureRecord($name,$alias)
91: {
92: return $this->getFixtureManager()->getRecord($name,$alias);
93: }
94:
95: /**
96: * Sets up the fixture before executing a test method.
97: * If you override this method, make sure the parent implementation is invoked.
98: * Otherwise, the database fixtures will not be managed properly.
99: */
100: protected function setUp()
101: {
102: parent::setUp();
103: if(is_array($this->fixtures))
104: $this->getFixtureManager()->load($this->fixtures);
105: }
106: }
107: