~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/test/CDbTestCase.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * @author Qiang Xue <qiang.xue@gmail.com>
 
35
 * @version $Id: CDbTestCase.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
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
         * @return mixed the property value
 
55
         */
 
56
        public function __get($name)
 
57
        {
 
58
                if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
 
59
                        return $rows;
 
60
                else
 
61
                        throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
 
62
        }
 
63
 
 
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 $name method name
 
68
         * @param string $params method parameters
 
69
         * @return mixed the property value
 
70
         */
 
71
        public function __call($name,$params)
 
72
        {
 
73
                if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
 
74
                        return $record;
 
75
                else
 
76
                        throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
 
77
        }
 
78
 
 
79
        /**
 
80
         * @return CDbFixtureManager the database fixture manager
 
81
         */
 
82
        public function getFixtureManager()
 
83
        {
 
84
                return Yii::app()->getComponent('fixture');
 
85
        }
 
86
 
 
87
        /**
 
88
         * @param string $name the fixture name (the key value in {@link fixtures}).
 
89
         * @return array the named fixture data
 
90
         */
 
91
        public function getFixtureData($name)
 
92
        {
 
93
                return $this->getFixtureManager()->getRows($name);
 
94
        }
 
95
 
 
96
        /**
 
97
         * @param string $name the fixture name (the key value in {@link fixtures}).
 
98
         * @param string $alias the alias of the fixture data row
 
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
        /**
 
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
         */
 
112
        protected function setUp()
 
113
        {
 
114
                parent::setUp();
 
115
                if(is_array($this->fixtures))
 
116
                        $this->getFixtureManager()->load($this->fixtures);
 
117
        }
 
118
}
 
 
b'\\ No newline at end of file'