blob: afccee017f182ea86bf94e4e210f3c7803667701 [file] [log] [blame]
Pascal Kriete69c97a72011-04-20 21:44:54 -04001<?php
2
3
4// Need a way to change dependencies (core libs and laoded libs)
5// Need a way to set the CI class
6
Pascal Krietefe372e32011-04-21 00:59:45 -04007class CI_TestCase extends PHPUnit_Framework_TestCase {
Pascal Krieteae7b3f92011-04-21 01:21:27 -04008
9 protected $ci_config;
10 protected $ci_instance;
11 protected static $ci_test_instance;
Pascal Kriete69c97a72011-04-20 21:44:54 -040012
Pascal Krieteae7b3f92011-04-21 01:21:27 -040013 private $global_map = array(
Pascal Kriete69c97a72011-04-20 21:44:54 -040014 'benchmark' => 'bm',
15 'config' => 'cfg',
16 'hooks' => 'ext',
17 'utf8' => 'uni',
18 'router' => 'rtr',
19 'output' => 'out',
20 'security' => 'sec',
21 'input' => 'in',
22 'lang' => 'lang',
Pascal Kriete69c97a72011-04-20 21:44:54 -040023 // @todo the loader is an edge case
Greg Aker8da69032011-04-21 11:28:27 -050024 'loader' => 'load',
25 'model' => 'model'
Pascal Kriete69c97a72011-04-20 21:44:54 -040026 );
27
Eric Barnes68286a42011-04-21 22:00:33 -040028 // --------------------------------------------------------------------
29
Pascal Krietefe372e32011-04-21 00:59:45 -040030 public function __construct()
Pascal Kriete69c97a72011-04-20 21:44:54 -040031 {
32 parent::__construct();
Pascal Krieteae7b3f92011-04-21 01:21:27 -040033
34 $this->ci_config = array();
Pascal Kriete69c97a72011-04-20 21:44:54 -040035 }
36
37 // --------------------------------------------------------------------
38
Eric Barnes68286a42011-04-21 22:00:33 -040039 public function setUp()
40 {
41 if (method_exists($this, 'set_up'))
42 {
43 $this->set_up();
44 }
45 }
46
47 // --------------------------------------------------------------------
48
49 public function tearDown()
50 {
51 if (method_exists($this, 'tear_down'))
52 {
53 $this->tear_down();
54 }
55 }
56
57 // --------------------------------------------------------------------
58
Pascal Krieteae7b3f92011-04-21 01:21:27 -040059 function ci_set_config($key, $val = '')
Pascal Kriete69c97a72011-04-20 21:44:54 -040060 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -040061 if (is_array($key))
62 {
63 $this->ci_config = $key;
64 }
65 else
66 {
67 $this->ci_config[$key] = $val;
68 }
Pascal Krietefe372e32011-04-21 00:59:45 -040069 }
70
71 // --------------------------------------------------------------------
72
73 function ci_instance($obj = FALSE)
74 {
75 if ( ! is_object($obj))
76 {
77 return $this->ci_instance;
78 }
79
Pascal Kriete69c97a72011-04-20 21:44:54 -040080 $this->ci_instance = $obj;
81 }
82
83 // --------------------------------------------------------------------
84
Pascal Krietefe372e32011-04-21 00:59:45 -040085 function ci_instance_var($name, $obj = FALSE)
Pascal Kriete69c97a72011-04-20 21:44:54 -040086 {
Pascal Krietefe372e32011-04-21 00:59:45 -040087 if ( ! is_object($obj))
88 {
89 return $this->ci_instance->$name;
90 }
91
Pascal Kriete69c97a72011-04-20 21:44:54 -040092 $this->ci_instance->$name =& $obj;
93 }
94
95 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -040096
97 /**
98 * Grab a core class
99 *
100 * Loads the correct core class without extensions
101 * and returns a reference to the class name in the
102 * globals array with the correct key. This way the
103 * test can modify the variable it assigns to and
104 * still maintain the global.
105 */
106 function &ci_core_class($name)
107 {
108 $name = strtolower($name);
109
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400110 if (isset($this->global_map[$name]))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400111 {
112 $class_name = ucfirst($name);
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400113 $global_name = $this->global_map[$name];
Pascal Kriete69c97a72011-04-20 21:44:54 -0400114 }
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400115 elseif (in_array($name, $this->global_map))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400116 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400117 $class_name = ucfirst(array_search($name, $this->global_map));
Pascal Kriete69c97a72011-04-20 21:44:54 -0400118 $global_name = $name;
119 }
120 else
121 {
122 throw new Exception('Not a valid core class.');
123 }
124
125 if ( ! class_exists('CI_'.$class_name))
126 {
127 require_once BASEPATH.'core/'.$class_name.'.php';
128 }
129
130 $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
131 return $GLOBALS[strtoupper($global_name)];
132 }
133
134 // --------------------------------------------------------------------
135
136 // convenience function for global mocks
137 function ci_set_core_class($name, $obj)
138 {
139 $orig =& $this->ci_core_class($name);
140 $orig = $obj;
141 }
142
143 // --------------------------------------------------------------------
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400144 // Internals
145 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -0400146
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400147 /**
148 * Overwrite runBare
149 *
150 * PHPUnit instantiates the test classes before
151 * running them individually. So right before a test
152 * runs we set our instance. Normally this step would
153 * happen in setUp, but someone is bound to forget to
154 * call the parent method and debugging this is no fun.
155 */
156 public function runBare()
Pascal Krietefe372e32011-04-21 00:59:45 -0400157 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400158 self::$ci_test_instance = $this;
159 parent::runBare();
Pascal Krietefe372e32011-04-21 00:59:45 -0400160 }
161
162 // --------------------------------------------------------------------
163
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400164 public static function instance()
165 {
166 return self::$ci_test_instance;
167 }
168
169 // --------------------------------------------------------------------
170
171 function ci_get_config()
Pascal Krietefe372e32011-04-21 00:59:45 -0400172 {
173 return $this->ci_config;
174 }
Taufan Aditya8749bc72012-03-11 05:43:45 +0700175
176 // --------------------------------------------------------------------
177
178 /**
179 * This overload is useful to create a stub, that need to have a specific method.
180 */
181 function __call($method, $args)
182 {
183 if ($this->{$method} instanceof Closure)
184 {
185 return call_user_func_array($this->{$method},$args);
186 }
187 else
188 {
189 return parent::__call($method, $args);
190 }
191 }
Pascal Kriete69c97a72011-04-20 21:44:54 -0400192}
193
194// EOF