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 { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 4 | |
| 5 | protected $ci_config; |
| 6 | protected $ci_instance; |
| 7 | protected static $ci_test_instance; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [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 | ); |
| 22 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 23 | // -------------------------------------------------------------------- |
| 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 | |
| 29 | $this->ci_config = array(); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // -------------------------------------------------------------------- |
| 33 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 34 | 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 | } |
| 51 | |
| 52 | // -------------------------------------------------------------------- |
| 53 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 54 | function ci_set_config($key, $val = '') |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 55 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 56 | if (is_array($key)) |
| 57 | { |
| 58 | $this->ci_config = $key; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | $this->ci_config[$key] = $val; |
| 63 | } |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
| 68 | function ci_instance($obj = FALSE) |
| 69 | { |
| 70 | if ( ! is_object($obj)) |
| 71 | { |
| 72 | return $this->ci_instance; |
| 73 | } |
| 74 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 75 | $this->ci_instance = $obj; |
| 76 | } |
| 77 | |
| 78 | // -------------------------------------------------------------------- |
| 79 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 80 | function ci_instance_var($name, $obj = FALSE) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 81 | { |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 82 | if ( ! is_object($obj)) |
| 83 | { |
| 84 | return $this->ci_instance->$name; |
| 85 | } |
| 86 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 87 | $this->ci_instance->$name =& $obj; |
| 88 | } |
| 89 | |
| 90 | // -------------------------------------------------------------------- |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Grab a core class |
| 94 | * |
| 95 | * Loads the correct core class without extensions |
| 96 | * and returns a reference to the class name in the |
| 97 | * globals array with the correct key. This way the |
| 98 | * test can modify the variable it assigns to and |
| 99 | * still maintain the global. |
| 100 | */ |
| 101 | function &ci_core_class($name) |
| 102 | { |
| 103 | $name = strtolower($name); |
| 104 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 105 | if (isset($this->global_map[$name])) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 106 | { |
| 107 | $class_name = ucfirst($name); |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 108 | $global_name = $this->global_map[$name]; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 109 | } |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 110 | elseif (in_array($name, $this->global_map)) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 111 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 112 | $class_name = ucfirst(array_search($name, $this->global_map)); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 113 | $global_name = $name; |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | throw new Exception('Not a valid core class.'); |
| 118 | } |
| 119 | |
| 120 | if ( ! class_exists('CI_'.$class_name)) |
| 121 | { |
| 122 | require_once BASEPATH.'core/'.$class_name.'.php'; |
| 123 | } |
| 124 | |
| 125 | $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; |
| 126 | return $GLOBALS[strtoupper($global_name)]; |
| 127 | } |
| 128 | |
| 129 | // -------------------------------------------------------------------- |
| 130 | |
| 131 | // convenience function for global mocks |
| 132 | function ci_set_core_class($name, $obj) |
| 133 | { |
| 134 | $orig =& $this->ci_core_class($name); |
| 135 | $orig = $obj; |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 139 | // Internals |
| 140 | // -------------------------------------------------------------------- |
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 | /** |
| 143 | * Overwrite runBare |
| 144 | * |
| 145 | * PHPUnit instantiates the test classes before |
| 146 | * running them individually. So right before a test |
| 147 | * runs we set our instance. Normally this step would |
| 148 | * happen in setUp, but someone is bound to forget to |
| 149 | * call the parent method and debugging this is no fun. |
| 150 | */ |
| 151 | public function runBare() |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 152 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 153 | self::$ci_test_instance = $this; |
| 154 | parent::runBare(); |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
| 158 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 159 | public static function instance() |
| 160 | { |
| 161 | return self::$ci_test_instance; |
| 162 | } |
| 163 | |
| 164 | // -------------------------------------------------------------------- |
| 165 | |
| 166 | function ci_get_config() |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 167 | { |
| 168 | return $this->ci_config; |
| 169 | } |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * This overload is useful to create a stub, that need to have a specific method. |
| 175 | */ |
| 176 | function __call($method, $args) |
| 177 | { |
| 178 | if ($this->{$method} instanceof Closure) |
| 179 | { |
| 180 | return call_user_func_array($this->{$method},$args); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | return parent::__call($method, $args); |
| 185 | } |
| 186 | } |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // EOF |