blob: f327e6b0752c1fa436148a65ba08ca0bc6f71119 [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 }
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070051
52 // --------------------------------------------------------------------
53
54 public static function instance()
55 {
56 return self::$ci_test_instance;
57 }
Eric Barnes68286a42011-04-21 22:00:33 -040058
59 // --------------------------------------------------------------------
60
Pascal Krieteae7b3f92011-04-21 01:21:27 -040061 function ci_set_config($key, $val = '')
Pascal Kriete69c97a72011-04-20 21:44:54 -040062 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -040063 if (is_array($key))
64 {
65 $this->ci_config = $key;
66 }
67 else
68 {
69 $this->ci_config[$key] = $val;
70 }
Pascal Krietefe372e32011-04-21 00:59:45 -040071 }
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070072
73 // --------------------------------------------------------------------
74
75 function ci_get_config()
76 {
77 return $this->ci_config;
78 }
Pascal Krietefe372e32011-04-21 00:59:45 -040079
80 // --------------------------------------------------------------------
81
82 function ci_instance($obj = FALSE)
83 {
84 if ( ! is_object($obj))
85 {
86 return $this->ci_instance;
87 }
88
Pascal Kriete69c97a72011-04-20 21:44:54 -040089 $this->ci_instance = $obj;
90 }
91
92 // --------------------------------------------------------------------
93
Pascal Krietefe372e32011-04-21 00:59:45 -040094 function ci_instance_var($name, $obj = FALSE)
Pascal Kriete69c97a72011-04-20 21:44:54 -040095 {
Pascal Krietefe372e32011-04-21 00:59:45 -040096 if ( ! is_object($obj))
97 {
98 return $this->ci_instance->$name;
99 }
100
Pascal Kriete69c97a72011-04-20 21:44:54 -0400101 $this->ci_instance->$name =& $obj;
102 }
103
104 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -0400105
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
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400119 if (isset($this->global_map[$name]))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400120 {
121 $class_name = ucfirst($name);
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400122 $global_name = $this->global_map[$name];
Pascal Kriete69c97a72011-04-20 21:44:54 -0400123 }
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400124 elseif (in_array($name, $this->global_map))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400125 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400126 $class_name = ucfirst(array_search($name, $this->global_map));
Pascal Kriete69c97a72011-04-20 21:44:54 -0400127 $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 // --------------------------------------------------------------------
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400153 // Internals
154 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -0400155
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400156 /**
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()
Pascal Krietefe372e32011-04-21 00:59:45 -0400166 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400167 self::$ci_test_instance = $this;
168 parent::runBare();
Pascal Krietefe372e32011-04-21 00:59:45 -0400169 }
Taufan Adityae1dc9ea2012-03-28 16:49:49 +0700170
Pascal Krietefe372e32011-04-21 00:59:45 -0400171 // --------------------------------------------------------------------
172
Taufan Adityae1dc9ea2012-03-28 16:49:49 +0700173 function helper($name)
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400174 {
Taufan Adityae1dc9ea2012-03-28 16:49:49 +0700175 require_once(BASEPATH.'helpers/'.$name.'_helper.php');
Pascal Krietefe372e32011-04-21 00:59:45 -0400176 }
Taufan Aditya8749bc72012-03-11 05:43:45 +0700177
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 }
Pascal Kriete69c97a72011-04-20 21:44:54 -0400194}
195
196// EOF