Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 98 lines (92 sloc) 3.261 kb
61d96d5
qiang.xue authored
1 <?php
ed259c3 Finished all test framework classes.
qiang.xue authored
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-2009 Yii Software LLC
8 * @license http://www.yiiframework.com/license/
9 */
61d96d5
qiang.xue authored
10
11 Yii::import('system.test.CTestCase');
12
dd334c0 refactored test framework.
qiang.xue authored
13 /**
ed259c3 Finished all test framework classes.
qiang.xue authored
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>
dd334c0 refactored test framework.
qiang.xue authored
28 *
ed259c3 Finished all test framework classes.
qiang.xue authored
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 * @author Qiang Xue <qiang.xue@gmail.com>
35 * @version $Id$
36 * @package system.test
37 * @since 1.1
dd334c0 refactored test framework.
qiang.xue authored
38 */
ff3445c Added CWebTestCase.php.
qiang.xue authored
39 class CDbTestCase extends CTestCase
61d96d5
qiang.xue authored
40 {
dd334c0 refactored test framework.
qiang.xue authored
41 /**
c3bdff4 * Added phpunit-based testing framework.
qiang.xue authored
42 * @var array a list of fixtures that should be loaded before each test method executes.
dd334c0 refactored test framework.
qiang.xue authored
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).
c3bdff4 * Added phpunit-based testing framework.
qiang.xue authored
46 * Defaults to false, meaning fixtures will not be used at all.
dd334c0 refactored test framework.
qiang.xue authored
47 */
c3bdff4 * Added phpunit-based testing framework.
qiang.xue authored
48 public $fixtures=false;
dd334c0 refactored test framework.
qiang.xue authored
49
ed259c3 Finished all test framework classes.
qiang.xue authored
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 the property name
54 * @return mixed the property value
55 */
61d96d5
qiang.xue authored
56 public function __get($name)
57 {
c3bdff4 * Added phpunit-based testing framework.
qiang.xue authored
58 if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
61d96d5
qiang.xue authored
59 return $rows;
60 else
61 throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
62 }
63
ed259c3 Finished all test framework classes.
qiang.xue authored
64 /**
65 * PHP magic method.
66 * This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.
67 * @param string method name
68 * @param string method parameters
69 * @return mixed the property value
70 */
61d96d5
qiang.xue authored
71 public function __call($name,$params)
72 {
c3bdff4 * Added phpunit-based testing framework.
qiang.xue authored
73 if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
dd334c0 refactored test framework.
qiang.xue authored
74 return $record;
61d96d5
qiang.xue authored
75 else
76 throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
77 }
78
ed259c3 Finished all test framework classes.
qiang.xue authored
79 /**
80 * @return CDbFixtureManager the database fixture manager
81 */
dd334c0 refactored test framework.
qiang.xue authored
82 public function getFixtureManager()
61d96d5
qiang.xue authored
83 {
84 return Yii::app()->getComponent('fixture');
85 }
86
ed259c3 Finished all test framework classes.
qiang.xue authored
87 /**
88 * Sets up the fixture before executing a test method.
89 * If you override this method, make sure the parent implementation is invoked.
90 * Otherwise, the database fixtures will not be managed properly.
91 */
6d19e4b Added support for creating test skeleton files.
qiang.xue authored
92 protected function setUp()
61d96d5
qiang.xue authored
93 {
94 parent::setUp();
c3bdff4 * Added phpunit-based testing framework.
qiang.xue authored
95 if(is_array($this->fixtures))
96 $this->getFixtureManager()->load($this->fixtures);
61d96d5
qiang.xue authored
97 }
98 }
Something went wrong with that request. Please try again.