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 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 5 | protected $ci_config; |
| 6 | protected $ci_instance; |
| 7 | protected static $ci_test_instance; |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 8 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 9 | private $global_map = array( |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 10 | '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 Aker | 8da6903 | 2011-04-21 11:28:27 -0500 | [diff] [blame] | 19 | 'loader' => 'load', |
| 20 | 'model' => 'model' |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 21 | ); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 22 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 23 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 24 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 25 | public function __construct() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 26 | { |
| 27 | parent::__construct(); |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 28 | $this->ci_config = array(); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 29 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 30 | |
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 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 33 | public function setUp() |
| 34 | { |
| 35 | if (method_exists($this, 'set_up')) |
| 36 | { |
| 37 | $this->set_up(); |
| 38 | } |
| 39 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 40 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 41 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 42 | |
| 43 | public function tearDown() |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 44 | { |
| 45 | if (method_exists($this, 'tear_down')) |
| 46 | { |
| 47 | $this->tear_down(); |
| 48 | } |
| 49 | } |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 50 | |
| 51 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 52 | |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 53 | public static function instance() |
| 54 | { |
| 55 | return self::$ci_test_instance; |
| 56 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 57 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 58 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 59 | |
| 60 | public function ci_set_config($key, $val = '') |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 61 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 62 | if (is_array($key)) |
| 63 | { |
| 64 | $this->ci_config = $key; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | $this->ci_config[$key] = $val; |
| 69 | } |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 70 | } |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 71 | |
| 72 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 73 | |
| 74 | public function ci_get_config() |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 75 | { |
| 76 | return $this->ci_config; |
| 77 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 78 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 79 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 80 | |
| 81 | public function ci_instance($obj = FALSE) |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 82 | { |
| 83 | if ( ! is_object($obj)) |
| 84 | { |
| 85 | return $this->ci_instance; |
| 86 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 87 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 88 | $this->ci_instance = $obj; |
| 89 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 90 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 91 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 92 | |
| 93 | public function ci_instance_var($name, $obj = FALSE) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 94 | { |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 95 | if ( ! is_object($obj)) |
| 96 | { |
| 97 | return $this->ci_instance->$name; |
| 98 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 99 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 100 | $this->ci_instance->$name =& $obj; |
| 101 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 102 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 103 | // -------------------------------------------------------------------- |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 104 | |
| 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 Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 114 | public function &ci_core_class($name) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 115 | { |
| 116 | $name = strtolower($name); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 117 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 118 | if (isset($this->global_map[$name])) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 119 | { |
| 120 | $class_name = ucfirst($name); |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 121 | $global_name = $this->global_map[$name]; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 122 | } |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 123 | elseif (in_array($name, $this->global_map)) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 124 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 125 | $class_name = ucfirst(array_search($name, $this->global_map)); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 126 | $global_name = $name; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | throw new Exception('Not a valid core class.'); |
| 131 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 132 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 133 | if ( ! class_exists('CI_'.$class_name)) |
| 134 | { |
| 135 | require_once BASEPATH.'core/'.$class_name.'.php'; |
| 136 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 137 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 138 | $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; |
| 139 | return $GLOBALS[strtoupper($global_name)]; |
| 140 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 141 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 142 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 143 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 144 | // convenience function for global mocks |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 145 | public function ci_set_core_class($name, $obj) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 146 | { |
| 147 | $orig =& $this->ci_core_class($name); |
| 148 | $orig = $obj; |
| 149 | } |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 150 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 151 | // -------------------------------------------------------------------- |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 152 | // Internals |
| 153 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 154 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 155 | /** |
| 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 Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 165 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 166 | self::$ci_test_instance = $this; |
| 167 | parent::runBare(); |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 168 | } |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 169 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 170 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 171 | |
| 172 | public function helper($name) |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 173 | { |
Taufan Aditya | e1dc9ea | 2012-03-28 16:49:49 +0700 | [diff] [blame] | 174 | require_once(BASEPATH.'helpers/'.$name.'_helper.php'); |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 175 | } |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 176 | |
| 177 | // -------------------------------------------------------------------- |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 178 | |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 179 | /** |
| 180 | * This overload is useful to create a stub, that need to have a specific method. |
| 181 | */ |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 182 | public function __call($method, $args) |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 183 | { |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 184 | if ($this->{$method} instanceof Closure) |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 185 | { |
| 186 | return call_user_func_array($this->{$method},$args); |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 187 | } |
| 188 | else |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 189 | { |
| 190 | return parent::__call($method, $args); |
| 191 | } |
| 192 | } |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 193 | |
Andrey Andreev | f243ce1 | 2012-06-09 23:34:21 +0300 | [diff] [blame] | 194 | } |