blob: eda9e1b603e11e5a2d3d6d75c9eed6fd38e524d1 [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 {
Andrey Andreevf243ce12012-06-09 23:34:21 +03004
Pascal Krieteae7b3f92011-04-21 01:21:27 -04005 protected $ci_config;
6 protected $ci_instance;
7 protected static $ci_test_instance;
Andrey Andreevf243ce12012-06-09 23:34:21 +03008
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 );
Andrey Andreevf243ce12012-06-09 23:34:21 +030022
Eric Barnes68286a42011-04-21 22:00:33 -040023 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030024
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 $this->ci_config = array();
Pascal Kriete69c97a72011-04-20 21:44:54 -040029 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030030
Pascal Kriete69c97a72011-04-20 21:44:54 -040031 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030032
Eric Barnes68286a42011-04-21 22:00:33 -040033 public function setUp()
34 {
35 if (method_exists($this, 'set_up'))
36 {
37 $this->set_up();
38 }
39 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030040
Eric Barnes68286a42011-04-21 22:00:33 -040041 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030042
43 public function tearDown()
Eric Barnes68286a42011-04-21 22:00:33 -040044 {
45 if (method_exists($this, 'tear_down'))
46 {
47 $this->tear_down();
48 }
49 }
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070050
51 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030052
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070053 public static function instance()
54 {
55 return self::$ci_test_instance;
56 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030057
Eric Barnes68286a42011-04-21 22:00:33 -040058 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030059
60 public function ci_set_config($key, $val = '')
Pascal Kriete69c97a72011-04-20 21:44:54 -040061 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -040062 if (is_array($key))
63 {
64 $this->ci_config = $key;
65 }
66 else
67 {
68 $this->ci_config[$key] = $val;
69 }
Pascal Krietefe372e32011-04-21 00:59:45 -040070 }
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070071
72 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030073
74 public function ci_get_config()
Taufan Adityae1dc9ea2012-03-28 16:49:49 +070075 {
76 return $this->ci_config;
77 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030078
Pascal Krietefe372e32011-04-21 00:59:45 -040079 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030080
81 public function ci_instance($obj = FALSE)
Pascal Krietefe372e32011-04-21 00:59:45 -040082 {
83 if ( ! is_object($obj))
84 {
85 return $this->ci_instance;
86 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030087
Pascal Kriete69c97a72011-04-20 21:44:54 -040088 $this->ci_instance = $obj;
89 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030090
Pascal Kriete69c97a72011-04-20 21:44:54 -040091 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +030092
93 public function ci_instance_var($name, $obj = FALSE)
Pascal Kriete69c97a72011-04-20 21:44:54 -040094 {
Pascal Krietefe372e32011-04-21 00:59:45 -040095 if ( ! is_object($obj))
96 {
97 return $this->ci_instance->$name;
98 }
Andrey Andreevf243ce12012-06-09 23:34:21 +030099
Pascal Kriete69c97a72011-04-20 21:44:54 -0400100 $this->ci_instance->$name =& $obj;
101 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300102
Pascal Kriete69c97a72011-04-20 21:44:54 -0400103 // --------------------------------------------------------------------
Pascal Kriete69c97a72011-04-20 21:44:54 -0400104
105 /**
106 * Grab a core class
107 *
108 * Loads the correct core class without extensions
109 * and returns a reference to the class name in the
110 * globals array with the correct key. This way the
111 * test can modify the variable it assigns to and
112 * still maintain the global.
113 */
Andrey Andreevf243ce12012-06-09 23:34:21 +0300114 public function &ci_core_class($name)
Pascal Kriete69c97a72011-04-20 21:44:54 -0400115 {
116 $name = strtolower($name);
Andrey Andreevf243ce12012-06-09 23:34:21 +0300117
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400118 if (isset($this->global_map[$name]))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400119 {
120 $class_name = ucfirst($name);
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400121 $global_name = $this->global_map[$name];
Pascal Kriete69c97a72011-04-20 21:44:54 -0400122 }
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400123 elseif (in_array($name, $this->global_map))
Pascal Kriete69c97a72011-04-20 21:44:54 -0400124 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400125 $class_name = ucfirst(array_search($name, $this->global_map));
Pascal Kriete69c97a72011-04-20 21:44:54 -0400126 $global_name = $name;
127 }
128 else
129 {
130 throw new Exception('Not a valid core class.');
131 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300132
Pascal Kriete69c97a72011-04-20 21:44:54 -0400133 if ( ! class_exists('CI_'.$class_name))
134 {
135 require_once BASEPATH.'core/'.$class_name.'.php';
136 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300137
Pascal Kriete69c97a72011-04-20 21:44:54 -0400138 $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
139 return $GLOBALS[strtoupper($global_name)];
140 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300141
Pascal Kriete69c97a72011-04-20 21:44:54 -0400142 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +0300143
Pascal Kriete69c97a72011-04-20 21:44:54 -0400144 // convenience function for global mocks
Andrey Andreevf243ce12012-06-09 23:34:21 +0300145 public function ci_set_core_class($name, $obj)
Pascal Kriete69c97a72011-04-20 21:44:54 -0400146 {
147 $orig =& $this->ci_core_class($name);
148 $orig = $obj;
149 }
Andrey Andreevf243ce12012-06-09 23:34:21 +0300150
Pascal Kriete69c97a72011-04-20 21:44:54 -0400151 // --------------------------------------------------------------------
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400152 // Internals
153 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +0300154
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400155 /**
156 * Overwrite runBare
157 *
158 * PHPUnit instantiates the test classes before
159 * running them individually. So right before a test
160 * runs we set our instance. Normally this step would
161 * happen in setUp, but someone is bound to forget to
162 * call the parent method and debugging this is no fun.
163 */
164 public function runBare()
Pascal Krietefe372e32011-04-21 00:59:45 -0400165 {
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400166 self::$ci_test_instance = $this;
167 parent::runBare();
Pascal Krietefe372e32011-04-21 00:59:45 -0400168 }
Taufan Adityae1dc9ea2012-03-28 16:49:49 +0700169
Pascal Krietefe372e32011-04-21 00:59:45 -0400170 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +0300171
172 public function helper($name)
Pascal Krieteae7b3f92011-04-21 01:21:27 -0400173 {
Taufan Adityae1dc9ea2012-03-28 16:49:49 +0700174 require_once(BASEPATH.'helpers/'.$name.'_helper.php');
Pascal Krietefe372e32011-04-21 00:59:45 -0400175 }
Taufan Aditya8749bc72012-03-11 05:43:45 +0700176
177 // --------------------------------------------------------------------
Andrey Andreevf243ce12012-06-09 23:34:21 +0300178
Taufan Aditya8749bc72012-03-11 05:43:45 +0700179 /**
180 * This overload is useful to create a stub, that need to have a specific method.
181 */
Andrey Andreevf243ce12012-06-09 23:34:21 +0300182 public function __call($method, $args)
Taufan Aditya8749bc72012-03-11 05:43:45 +0700183 {
Andrey Andreevf243ce12012-06-09 23:34:21 +0300184 if ($this->{$method} instanceof Closure)
Taufan Aditya8749bc72012-03-11 05:43:45 +0700185 {
186 return call_user_func_array($this->{$method},$args);
Andrey Andreevf243ce12012-06-09 23:34:21 +0300187 }
188 else
Taufan Aditya8749bc72012-03-11 05:43:45 +0700189 {
190 return parent::__call($method, $args);
191 }
192 }
Pascal Kriete69c97a72011-04-20 21:44:54 -0400193
Andrey Andreevf243ce12012-06-09 23:34:21 +0300194}