Skip to content
This repository
Newer
Older
100644 118 lines (110 sloc) 3.977 kb
61d96d5d »
2009-06-10
1 <?php
ed259c39 »
2009-06-12 Finished all test framework classes.
2 /**
3 * This file contains the CDbTestCase class.
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.yiiframework.com/
4be0af69 »
2011-01-01 changed copyright year.
7 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
ed259c39 »
2009-06-12 Finished all test framework classes.
8 * @license http://www.yiiframework.com/license/
9 */
61d96d5d »
2009-06-10
10
11 Yii::import('system.test.CTestCase');
12
dd334c0a »
2009-06-12 refactored test framework.
13 /**
ed259c39 »
2009-06-12 Finished all test framework classes.
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>
dd334c0a »
2009-06-12 refactored test framework.
28 *
ed259c39 »
2009-06-12 Finished all test framework classes.
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
dd334c0a »
2009-06-12 refactored test framework.
38 */
1fb0b805 »
2009-06-17 test documentation.
39 abstract class CDbTestCase extends CTestCase
61d96d5d »
2009-06-10
40 {
dd334c0a »
2009-06-12 refactored test framework.
41 /**
c3bdff42 »
2009-06-12 * Added phpunit-based testing framework.
42 * @var array a list of fixtures that should be loaded before each test method executes.
dd334c0a »
2009-06-12 refactored test framework.
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).
c3bdff42 »
2009-06-12 * Added phpunit-based testing framework.
46 * Defaults to false, meaning fixtures will not be used at all.
dd334c0a »
2009-06-12 refactored test framework.
47 */
84da5185 »
2009-06-19
48 protected $fixtures=false;
dd334c0a »
2009-06-12 refactored test framework.
49
ed259c39 »
2009-06-12 Finished all test framework classes.
50 /**
51 * PHP magic method.
52 * This method is overridden so that named fixture data can be accessed like a normal property.
72b97456 »
2010-09-23 Updated PHPDoc @param parameter names
53 * @param string $name the property name
ed259c39 »
2009-06-12 Finished all test framework classes.
54 * @return mixed the property value
55 */
61d96d5d »
2009-06-10
56 public function __get($name)
57 {
c3bdff42 »
2009-06-12 * Added phpunit-based testing framework.
58 if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
61d96d5d »
2009-06-10
59 return $rows;
60 else
61 throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
62 }
63
ed259c39 »
2009-06-12 Finished all test framework classes.
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.
72b97456 »
2010-09-23 Updated PHPDoc @param parameter names
67 * @param string $name method name
68 * @param string $params method parameters
ed259c39 »
2009-06-12 Finished all test framework classes.
69 * @return mixed the property value
70 */
61d96d5d »
2009-06-10
71 public function __call($name,$params)
72 {
c3bdff42 »
2009-06-12 * Added phpunit-based testing framework.
73 if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
dd334c0a »
2009-06-12 refactored test framework.
74 return $record;
61d96d5d »
2009-06-10
75 else
76 throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
77 }
78
ed259c39 »
2009-06-12 Finished all test framework classes.
79 /**
80 * @return CDbFixtureManager the database fixture manager
81 */
dd334c0a »
2009-06-12 refactored test framework.
82 public function getFixtureManager()
61d96d5d »
2009-06-10
83 {
84 return Yii::app()->getComponent('fixture');
85 }
86
ed259c39 »
2009-06-12 Finished all test framework classes.
87 /**
72b97456 »
2010-09-23 Updated PHPDoc @param parameter names
88 * @param string $name the fixture name (the key value in {@link fixtures}).
91930c0a »
2009-10-29 Added getFixtureData() and getFixtureRecord() to CDbTestCase and CWeb…
89 * @return array the named fixture data
90 */
91 public function getFixtureData($name)
92 {
93 return $this->getFixtureManager()->getRows($name);
94 }
95
96 /**
72b97456 »
2010-09-23 Updated PHPDoc @param parameter names
97 * @param string $name the fixture name (the key value in {@link fixtures}).
98 * @param string $alias the alias of the fixture data row
91930c0a »
2009-10-29 Added getFixtureData() and getFixtureRecord() to CDbTestCase and CWeb…
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.
101 */
102 public function getFixtureRecord($name,$alias)
103 {
104 return $this->getFixtureManager()->getRecord($name,$alias);
105 }
106
107 /**
ed259c39 »
2009-06-12 Finished all test framework classes.
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.
111 */
6d19e4b1 »
2009-06-12 Added support for creating test skeleton files.
112 protected function setUp()
61d96d5d »
2009-06-10
113 {
114 parent::setUp();
c3bdff42 »
2009-06-12 * Added phpunit-based testing framework.
115 if(is_array($this->fixtures))
116 $this->getFixtureManager()->load($this->fixtures);
61d96d5d »
2009-06-10
117 }
118 }
Something went wrong with that request. Please try again.