Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 1 | <?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 Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 7 | class CI_TestCase extends PHPUnit_Framework_TestCase { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 8 | |
| 9 | protected $ci_config; |
| 10 | protected $ci_instance; |
| 11 | protected static $ci_test_instance; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 12 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 13 | private $global_map = array( |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 14 | '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 Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 23 | // @todo the loader is an edge case |
Greg Aker | 8da6903 | 2011-04-21 11:28:27 -0500 | [diff] [blame] | 24 | 'loader' => 'load', |
| 25 | 'model' => 'model' |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 26 | ); |
| 27 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 28 | // -------------------------------------------------------------------- |
| 29 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 30 | public function __construct() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 31 | { |
| 32 | parent::__construct(); |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 33 | |
| 34 | $this->ci_config = array(); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // -------------------------------------------------------------------- |
| 38 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 39 | 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 Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 59 | function ci_set_config($key, $val = '') |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 60 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 61 | if (is_array($key)) |
| 62 | { |
| 63 | $this->ci_config = $key; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | $this->ci_config[$key] = $val; |
| 68 | } |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
| 73 | function ci_instance($obj = FALSE) |
| 74 | { |
| 75 | if ( ! is_object($obj)) |
| 76 | { |
| 77 | return $this->ci_instance; |
| 78 | } |
| 79 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 80 | $this->ci_instance = $obj; |
| 81 | } |
| 82 | |
| 83 | // -------------------------------------------------------------------- |
| 84 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 85 | function ci_instance_var($name, $obj = FALSE) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 86 | { |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 87 | if ( ! is_object($obj)) |
| 88 | { |
| 89 | return $this->ci_instance->$name; |
| 90 | } |
| 91 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 92 | $this->ci_instance->$name =& $obj; |
| 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 96 | |
| 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 Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 110 | if (isset($this->global_map[$name])) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 111 | { |
| 112 | $class_name = ucfirst($name); |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 113 | $global_name = $this->global_map[$name]; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 114 | } |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 115 | elseif (in_array($name, $this->global_map)) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 116 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 117 | $class_name = ucfirst(array_search($name, $this->global_map)); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 118 | $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 Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 144 | // Internals |
| 145 | // -------------------------------------------------------------------- |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 146 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 147 | /** |
| 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 Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 157 | { |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 158 | self::$ci_test_instance = $this; |
| 159 | parent::runBare(); |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
Pascal Kriete | ae7b3f9 | 2011-04-21 01:21:27 -0400 | [diff] [blame] | 164 | public static function instance() |
| 165 | { |
| 166 | return self::$ci_test_instance; |
| 167 | } |
| 168 | |
| 169 | // -------------------------------------------------------------------- |
| 170 | |
| 171 | function ci_get_config() |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 172 | { |
| 173 | return $this->ci_config; |
| 174 | } |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 175 | |
| 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 Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // EOF |