blob: 5c83974b3faf31c867210d41384ff3fa02602caf [file] [log] [blame]
Pascal Kriete69c97a72011-04-20 21:44:54 -04001<?php
2
Pascal Krietefe372e32011-04-21 00:59:45 -04003class CI_TestCase extends PHPUnit_Framework_TestCase {
Pascal Krieteae7b3f92011-04-21 01:21:27 -04004
5 protected $ci_config;
6 protected $ci_instance;
7 protected static $ci_test_instance;
Pascal Kriete69c97a72011-04-20 21:44:54 -04008
Pascal Krieteae7b3f92011-04-21 01:21:27 -04009 private $global_map = array(
Pascal Kriete69c97a72011-04-20 21:44:54 -040010 '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',
Greg Aker8da69032011-04-21 11:28:27 -050019 'loader' => 'load',
20 'model' => 'model'
Pascal Kriete69c97a72011-04-20 21:44:54 -040021 );
22
Eric Barnes68286a42011-04-21 22:00:33 -040023 // --------------------------------------------------------------------
24
Pascal Krietefe372e32011-04-21 00:59:45 -040025 public function __construct()
Pascal Kriete69c97a72011-04-20 21:44:54 -040026 {
27 parent::__construct();
Pascal Krieteae7b3f92011-04-21 01:21:27 -040028
29 $this->ci_config = array();
Pascal Kriete69c97a72011-04-20 21:44:54 -040030 }
31
32 // --------------------------------------------------------------------
33
Eric Barnes68286a42011-04-21 22:00:33 -040034 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
Pascal Krieteae7b3f92011-04-21 01:21:27 -040054 function ci_set_config($key, $val = '')
Pascal Kriete69c97a72011-04-20 21:44:54 -040055 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -040056 if (is_array($key))
57 {
58 $this->ci_config = $key;
59 }
60 else
61 {
62 $this->ci_config[$key] = $val;
63 }
Pascal Krietefe372e32011-04-21 00:59:45 -040064 }
65
66 // --------------------------------------------------------------------
67
68 function ci_instance($obj = FALSE)
69 {
70 if ( ! is_object($obj))
71 {
72 return $this->ci_instance;
73 }
74
Pascal Kriete69c97a72011-04-20 21:44:54 -040075 $this->ci_instance = $obj;
76 }
77
78 // --------------------------------------------------------------------
79
Pascal Krietefe372e32011-04-21 00:59:45 -040080 function ci_instance_var($name, $obj = FALSE)
Pascal Kriete69c97a72011-04-20 21:44:54 -040081 {
Pascal Krietefe372e32011-04-21 00:59:45 -040082 if ( ! is_object($obj))
83 {
84 return $this->ci_instance->$name;
85 }
86
Pascal Kriete69c97a72011-04-20 21:44:54 -040087 $this->ci_instance->$name =& $obj;
88 }
89
90 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -040091
92 /**
93 * Grab a core class
94 *
95 * Loads the correct core class without extensions
96 * and returns a reference to the class name in the
97 * globals array with the correct key. This way the
98 * test can modify the variable it assigns to and
99 * still maintain the global.
100 */
101 function &ci_core_class($name)
102 {
103 $name = strtolower($name);
104
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400105 if (isset($this->global_map[$name]))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400106 {
107 $class_name = ucfirst($name);
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400108 $global_name = $this->global_map[$name];
Pascal Kriete69c97a72011-04-20 21:44:54 -0400109 }
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400110 elseif (in_array($name, $this->global_map))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400111 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400112 $class_name = ucfirst(array_search($name, $this->global_map));
Pascal Kriete69c97a72011-04-20 21:44:54 -0400113 $global_name = $name;
114 }
115 else
116 {
117 throw new Exception('Not a valid core class.');
118 }
119
120 if ( ! class_exists('CI_'.$class_name))
121 {
122 require_once BASEPATH.'core/'.$class_name.'.php';
123 }
124
125 $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
126 return $GLOBALS[strtoupper($global_name)];
127 }
128
129 // --------------------------------------------------------------------
130
131 // convenience function for global mocks
132 function ci_set_core_class($name, $obj)
133 {
134 $orig =& $this->ci_core_class($name);
135 $orig = $obj;
136 }
137
138 // --------------------------------------------------------------------
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400139 // Internals
140 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -0400141
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400142 /**
143 * Overwrite runBare
144 *
145 * PHPUnit instantiates the test classes before
146 * running them individually. So right before a test
147 * runs we set our instance. Normally this step would
148 * happen in setUp, but someone is bound to forget to
149 * call the parent method and debugging this is no fun.
150 */
151 public function runBare()
Pascal Krietefe372e32011-04-21 00:59:45 -0400152 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400153 self::$ci_test_instance = $this;
154 parent::runBare();
Pascal Krietefe372e32011-04-21 00:59:45 -0400155 }
156
157 // --------------------------------------------------------------------
158
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400159 public static function instance()
160 {
161 return self::$ci_test_instance;
162 }
163
164 // --------------------------------------------------------------------
165
166 function ci_get_config()
Pascal Krietefe372e32011-04-21 00:59:45 -0400167 {
168 return $this->ci_config;
169 }
Taufan Aditya8749bc72012-03-11 05:43:45 +0700170
171 // --------------------------------------------------------------------
172
173 /**
174 * This overload is useful to create a stub, that need to have a specific method.
175 */
176 function __call($method, $args)
177 {
178 if ($this->{$method} instanceof Closure)
179 {
180 return call_user_func_array($this->{$method},$args);
181 }
182 else
183 {
184 return parent::__call($method, $args);
185 }
186 }
Pascal Kriete69c97a72011-04-20 21:44:54 -0400187}
188
189// EOF