blob: f327e6b0752c1fa436148a65ba08ca0bc6f71119 [file] [log] [blame]
Andrey Andreev9c9591c2012-04-06 21:47:49 +03001<?php
2
3class CI_TestCase extends PHPUnit_Framework_TestCase {
4
5 protected $ci_config;
6 protected $ci_instance;
7 protected static $ci_test_instance;
8
9 private $global_map = array(
10 'benchmark' => 'bm',
11 'config' => 'cfg',
12 'hooks' => 'ext',
13 'utf8' => 'uni',
14 'router' => 'rtr',
15 'output' => 'out',
16 'security' => 'sec',
17 'input' => 'in',
18 'lang' => 'lang',
19 'loader' => 'load',
20 'model' => 'model'
21 );
22
23 // --------------------------------------------------------------------
24
25 public function __construct()
26 {
27 parent::__construct();
28
29 $this->ci_config = array();
30 }
31
32 // --------------------------------------------------------------------
33
34 public function setUp()
35 {
36 if (method_exists($this, 'set_up'))
37 {
38 $this->set_up();
39 }
40 }
41
42 // --------------------------------------------------------------------
43
44 public function tearDown()
45 {
46 if (method_exists($this, 'tear_down'))
47 {
48 $this->tear_down();
49 }
50 }
51
52 // --------------------------------------------------------------------
53
54 public static function instance()
55 {
56 return self::$ci_test_instance;
57 }
58
59 // --------------------------------------------------------------------
60
61 function ci_set_config($key, $val = '')
62 {
63 if (is_array($key))
64 {
65 $this->ci_config = $key;
66 }
67 else
68 {
69 $this->ci_config[$key] = $val;
70 }
71 }
72
73 // --------------------------------------------------------------------
74
75 function ci_get_config()
76 {
77 return $this->ci_config;
78 }
79
80 // --------------------------------------------------------------------
81
82 function ci_instance($obj = FALSE)
83 {
84 if ( ! is_object($obj))
85 {
86 return $this->ci_instance;
87 }
88
89 $this->ci_instance = $obj;
90 }
91
92 // --------------------------------------------------------------------
93
94 function ci_instance_var($name, $obj = FALSE)
95 {
96 if ( ! is_object($obj))
97 {
98 return $this->ci_instance->$name;
99 }
100
101 $this->ci_instance->$name =& $obj;
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Grab a core class
108 *
109 * Loads the correct core class without extensions
110 * and returns a reference to the class name in the
111 * globals array with the correct key. This way the
112 * test can modify the variable it assigns to and
113 * still maintain the global.
114 */
115 function &ci_core_class($name)
116 {
117 $name = strtolower($name);
118
119 if (isset($this->global_map[$name]))
120 {
121 $class_name = ucfirst($name);
122 $global_name = $this->global_map[$name];
123 }
124 elseif (in_array($name, $this->global_map))
125 {
126 $class_name = ucfirst(array_search($name, $this->global_map));
127 $global_name = $name;
128 }
129 else
130 {
131 throw new Exception('Not a valid core class.');
132 }
133
134 if ( ! class_exists('CI_'.$class_name))
135 {
136 require_once BASEPATH.'core/'.$class_name.'.php';
137 }
138
139 $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
140 return $GLOBALS[strtoupper($global_name)];
141 }
142
143 // --------------------------------------------------------------------
144
145 // convenience function for global mocks
146 function ci_set_core_class($name, $obj)
147 {
148 $orig =& $this->ci_core_class($name);
149 $orig = $obj;
150 }
151
152 // --------------------------------------------------------------------
153 // Internals
154 // --------------------------------------------------------------------
155
156 /**
157 * Overwrite runBare
158 *
159 * PHPUnit instantiates the test classes before
160 * running them individually. So right before a test
161 * runs we set our instance. Normally this step would
162 * happen in setUp, but someone is bound to forget to
163 * call the parent method and debugging this is no fun.
164 */
165 public function runBare()
166 {
167 self::$ci_test_instance = $this;
168 parent::runBare();
169 }
170
171 // --------------------------------------------------------------------
172
173 function helper($name)
174 {
175 require_once(BASEPATH.'helpers/'.$name.'_helper.php');
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * This overload is useful to create a stub, that need to have a specific method.
182 */
183 function __call($method, $args)
184 {
185 if ($this->{$method} instanceof Closure)
186 {
187 return call_user_func_array($this->{$method},$args);
188 }
189 else
190 {
191 return parent::__call($method, $args);
192 }
193 }
194}
195
196// EOF