Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 1 | <?php |
| 2 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 3 | class CI_TestCase extends PHPUnit_Framework_TestCase { |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 4 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 5 | public $ci_vfs_root; |
| 6 | public $ci_app_root; |
| 7 | public $ci_base_root; |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 8 | protected $ci_instance; |
| 9 | protected static $ci_test_instance; |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 10 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 11 | private $global_map = array( |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 12 | 'benchmark' => 'bm', |
| 13 | 'config' => 'cfg', |
| 14 | 'hooks' => 'ext', |
| 15 | 'utf8' => 'uni', |
| 16 | 'router' => 'rtr', |
| 17 | 'output' => 'out', |
| 18 | 'security' => 'sec', |
| 19 | 'input' => 'in', |
| 20 | 'lang' => 'lang', |
Greg Aker | 8da6903 | 2011-04-21 11:28:27 -0500 | [diff] [blame] | 21 | 'loader' => 'load', |
| 22 | 'model' => 'model' |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 23 | ); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 24 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 25 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 26 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 27 | public function __construct() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 28 | { |
| 29 | parent::__construct(); |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 30 | $this->ci_instance = new StdClass(); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 31 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 32 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 33 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 34 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 35 | public function setUp() |
| 36 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 37 | // Setup VFS with base directories |
| 38 | $this->ci_vfs_root = vfsStream::setup(); |
| 39 | $this->ci_app_root = vfsStream::newDirectory('application')->at($this->ci_vfs_root); |
| 40 | $this->ci_base_root = vfsStream::newDirectory('system')->at($this->ci_vfs_root); |
| 41 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 42 | if (method_exists($this, 'set_up')) |
| 43 | { |
| 44 | $this->set_up(); |
| 45 | } |
| 46 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 47 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 48 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 49 | |
| 50 | public function tearDown() |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 51 | { |
| 52 | if (method_exists($this, 'tear_down')) |
| 53 | { |
| 54 | $this->tear_down(); |
| 55 | } |
| 56 | } |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 57 | |
| 58 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 59 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 60 | public static function instance() |
| 61 | { |
| 62 | return self::$ci_test_instance; |
| 63 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 64 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 65 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 66 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 67 | public function ci_set_config($key = '', $val = '') |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 68 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 69 | // Add test config |
| 70 | if ( ! isset($this->ci_instance->config)) |
| 71 | { |
| 72 | $this->ci_instance->config = new CI_TestConfig(); |
| 73 | } |
| 74 | |
| 75 | // Empty key means just do setup above |
| 76 | if ($key === '') |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 81 | if (is_array($key)) |
| 82 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 83 | $this->ci_instance->config->config = $key; |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 87 | $this->ci_instance->config->config[$key] = $val; |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 88 | } |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 89 | } |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 90 | |
| 91 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 92 | |
| 93 | public function ci_get_config() |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 94 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 95 | return isset($this->ci_instance->config) ? $this->ci_instance->config->config : array(); |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 96 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 97 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 98 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 99 | |
| 100 | public function ci_instance($obj = FALSE) |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 101 | { |
| 102 | if ( ! is_object($obj)) |
| 103 | { |
| 104 | return $this->ci_instance; |
| 105 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 106 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 107 | $this->ci_instance = $obj; |
| 108 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 109 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 110 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 111 | |
| 112 | public function ci_instance_var($name, $obj = FALSE) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 113 | { |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 114 | if ( ! is_object($obj)) |
| 115 | { |
| 116 | return $this->ci_instance->$name; |
| 117 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 118 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 119 | $this->ci_instance->$name =& $obj; |
| 120 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 121 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 122 | // -------------------------------------------------------------------- |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Grab a core class |
| 126 | * |
| 127 | * Loads the correct core class without extensions |
| 128 | * and returns a reference to the class name in the |
| 129 | * globals array with the correct key. This way the |
| 130 | * test can modify the variable it assigns to and |
| 131 | * still maintain the global. |
| 132 | */ |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 133 | public function &ci_core_class($name) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 134 | { |
| 135 | $name = strtolower($name); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 136 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 137 | if (isset($this->global_map[$name])) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 138 | { |
| 139 | $class_name = ucfirst($name); |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 140 | $global_name = $this->global_map[$name]; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 141 | } |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 142 | elseif (in_array($name, $this->global_map)) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 143 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 144 | $class_name = ucfirst(array_search($name, $this->global_map)); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 145 | $global_name = $name; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | throw new Exception('Not a valid core class.'); |
| 150 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 151 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 152 | if ( ! class_exists('CI_'.$class_name)) |
| 153 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 154 | require_once SYSTEM_PATH.'core/'.$class_name.'.php'; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 155 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 156 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 157 | $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; |
| 158 | return $GLOBALS[strtoupper($global_name)]; |
| 159 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 160 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 161 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 162 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 163 | // convenience function for global mocks |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 164 | public function ci_set_core_class($name, $obj) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 165 | { |
| 166 | $orig =& $this->ci_core_class($name); |
| 167 | $orig = $obj; |
| 168 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 169 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 170 | /** |
| 171 | * Create VFS directory |
| 172 | * |
| 173 | * @param string Directory name |
| 174 | * @param object Optional root to create in |
| 175 | * @return object New directory object |
| 176 | */ |
| 177 | public function ci_vfs_mkdir($name, $root = NULL) |
| 178 | { |
| 179 | // Check for root |
| 180 | if ( ! $root) |
| 181 | { |
| 182 | $root = $this->ci_vfs_root; |
| 183 | } |
| 184 | |
| 185 | // Return new directory object |
| 186 | return vfsStream::newDirectory($name)->at($root); |
| 187 | } |
| 188 | |
| 189 | // -------------------------------------------------------------------- |
| 190 | |
| 191 | /** |
| 192 | * Create VFS content |
| 193 | * |
| 194 | * @param string File name |
| 195 | * @param string File content |
| 196 | * @param object VFS directory object |
| 197 | * @param mixed Optional subdirectory path or array of subs |
| 198 | * @return void |
| 199 | */ |
| 200 | public function ci_vfs_create($file, $content = '', $root = NULL, $path = NULL) |
| 201 | { |
| 202 | // Check for array |
| 203 | if (is_array($file)) |
| 204 | { |
| 205 | foreach ($file as $name => $content) |
| 206 | { |
| 207 | $this->ci_vfs_create($name, $content, $root, $path); |
| 208 | } |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Assert .php extension if none given |
| 213 | if (pathinfo($file, PATHINFO_EXTENSION) == '') |
| 214 | { |
| 215 | $file .= '.php'; |
| 216 | } |
| 217 | |
| 218 | // Build content |
| 219 | $tree = array($file => $content); |
| 220 | |
| 221 | // Check for path |
| 222 | $subs = array(); |
| 223 | if ($path) |
| 224 | { |
| 225 | // Explode if not array |
| 226 | $subs = is_array($path) ? $path : explode('/', trim($path, '/')); |
| 227 | } |
| 228 | |
| 229 | // Check for root |
| 230 | if ( ! $root) |
| 231 | { |
| 232 | // Use base VFS root |
| 233 | $root = $this->ci_vfs_root; |
| 234 | } |
| 235 | |
| 236 | // Handle subdirectories |
| 237 | while (($dir = array_shift($subs))) |
| 238 | { |
| 239 | // See if subdir exists under current root |
| 240 | $dir_root = $root->getChild($dir); |
| 241 | if ($dir_root) |
| 242 | { |
| 243 | // Yes - recurse into subdir |
| 244 | $root = $dir_root; |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | // No - put subdirectory back and quit |
| 249 | array_unshift($subs, $dir); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // Create any remaining subdirectories |
| 255 | if ($subs) |
| 256 | { |
| 257 | foreach (array_reverse($subs) as $dir) |
| 258 | { |
| 259 | // Wrap content in subdirectory for creation |
| 260 | $tree = array($dir => $tree); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // Create tree |
| 265 | vfsStream::create($tree, $root); |
| 266 | } |
| 267 | |
| 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * Clone a real file into VFS |
| 272 | * |
| 273 | * @param string Path from base directory |
| 274 | * @return bool TRUE on success, otherwise FALSE |
| 275 | */ |
| 276 | public function ci_vfs_clone($path) |
| 277 | { |
| 278 | // Get real file contents |
| 279 | $content = file_get_contents(PROJECT_BASE.$path); |
| 280 | if ($content === FALSE) |
| 281 | { |
| 282 | // Couldn't find file to clone |
| 283 | return FALSE; |
| 284 | } |
| 285 | |
| 286 | $this->ci_vfs_create(basename($path), $content, NULL, dirname($path)); |
| 287 | return TRUE; |
| 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Helper to get a VFS URL path |
| 294 | * |
| 295 | * @param string Path |
| 296 | * @param string Optional base path |
| 297 | * @return string Path URL |
| 298 | */ |
| 299 | public function ci_vfs_path($path, $base = '') |
| 300 | { |
| 301 | // Check for base path |
| 302 | if ($base) |
| 303 | { |
| 304 | // Prepend to path |
| 305 | $path = rtrim($base, '/').'/'.ltrim($path, '/'); |
| 306 | |
| 307 | // Is it already in URL form? |
| 308 | if (strpos($path, '://') !== FALSE) |
| 309 | { |
| 310 | // Done - return path |
| 311 | return $path; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // Trim leading slash and return URL |
| 316 | return vfsStream::url(ltrim($path, '/')); |
| 317 | } |
| 318 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 319 | // -------------------------------------------------------------------- |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 320 | // Internals |
| 321 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 322 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 323 | /** |
| 324 | * Overwrite runBare |
| 325 | * |
| 326 | * PHPUnit instantiates the test classes before |
| 327 | * running them individually. So right before a test |
| 328 | * runs we set our instance. Normally this step would |
| 329 | * happen in setUp, but someone is bound to forget to |
| 330 | * call the parent method and debugging this is no fun. |
| 331 | */ |
| 332 | public function runBare() |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 333 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 334 | self::$ci_test_instance = $this; |
| 335 | parent::runBare(); |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 336 | } |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 337 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 338 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 339 | |
| 340 | public function helper($name) |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 341 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame^] | 342 | require_once(SYSTEM_PATH.'helpers/'.$name.'_helper.php'); |
| 343 | } |
| 344 | |
| 345 | // -------------------------------------------------------------------- |
| 346 | |
| 347 | public function lang($name) |
| 348 | { |
| 349 | require(SYSTEM_PATH.'language/english/'.$name.'_lang.php'); |
| 350 | return $lang; |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 351 | } |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 352 | |
| 353 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 354 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 355 | /** |
| 356 | * This overload is useful to create a stub, that need to have a specific method. |
| 357 | */ |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 358 | public function __call($method, $args) |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 359 | { |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 360 | if ($this->{$method} instanceof Closure) |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 361 | { |
| 362 | return call_user_func_array($this->{$method},$args); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 363 | } |
| 364 | else |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 365 | { |
| 366 | return parent::__call($method, $args); |
| 367 | } |
| 368 | } |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 369 | |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 370 | } |