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 | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 8 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 9 | public static $global_map = array( |
| 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', |
| 19 | |
| 20 | // @todo the loader is an edge case |
| 21 | 'loader' => 'load' |
| 22 | ); |
| 23 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 24 | protected $ci_config = array(); |
| 25 | |
| 26 | protected $ci_instance; |
| 27 | protected static $ci_test_instance; |
| 28 | |
| 29 | |
| 30 | public function __construct() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 31 | { |
| 32 | parent::__construct(); |
| 33 | } |
| 34 | |
| 35 | // -------------------------------------------------------------------- |
| 36 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 37 | /** |
| 38 | * Overwrite runBare |
| 39 | * |
| 40 | * PHPUnit instantiates the test classes before |
| 41 | * running them individually. So right before a test |
| 42 | * runs we set our instance. Normally this step would |
| 43 | * happen in setUp, but someone is bound to forget to |
| 44 | * call the parent method and debugging this is no fun. |
| 45 | */ |
| 46 | public function runBare() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 47 | { |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 48 | self::$ci_test_instance = $this; |
| 49 | parent::runBare(); |
| 50 | } |
| 51 | |
| 52 | // -------------------------------------------------------------------- |
| 53 | |
| 54 | public static function instance() |
| 55 | { |
| 56 | return self::$ci_test_instance; |
| 57 | } |
| 58 | |
| 59 | // -------------------------------------------------------------------- |
| 60 | |
| 61 | function ci_instance($obj = FALSE) |
| 62 | { |
| 63 | if ( ! is_object($obj)) |
| 64 | { |
| 65 | return $this->ci_instance; |
| 66 | } |
| 67 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 68 | $this->ci_instance = $obj; |
| 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 73 | function ci_instance_var($name, $obj = FALSE) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 74 | { |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 75 | if ( ! is_object($obj)) |
| 76 | { |
| 77 | return $this->ci_instance->$name; |
| 78 | } |
| 79 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 80 | $this->ci_instance->$name =& $obj; |
| 81 | } |
| 82 | |
| 83 | // -------------------------------------------------------------------- |
| 84 | |
| 85 | // Set a class to a mock before it is loaded |
| 86 | function ci_library($name) |
| 87 | { |
| 88 | |
| 89 | } |
| 90 | |
| 91 | // -------------------------------------------------------------------- |
| 92 | |
| 93 | /** |
| 94 | * Grab a core class |
| 95 | * |
| 96 | * Loads the correct core class without extensions |
| 97 | * and returns a reference to the class name in the |
| 98 | * globals array with the correct key. This way the |
| 99 | * test can modify the variable it assigns to and |
| 100 | * still maintain the global. |
| 101 | */ |
| 102 | function &ci_core_class($name) |
| 103 | { |
| 104 | $name = strtolower($name); |
| 105 | |
| 106 | if (isset(self::$global_map[$name])) |
| 107 | { |
| 108 | $class_name = ucfirst($name); |
| 109 | $global_name = self::$global_map[$name]; |
| 110 | } |
| 111 | elseif (in_array($name, self::$global_map)) |
| 112 | { |
| 113 | $class_name = ucfirst(array_search($name, self::$global_map)); |
| 114 | $global_name = $name; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | throw new Exception('Not a valid core class.'); |
| 119 | } |
| 120 | |
| 121 | if ( ! class_exists('CI_'.$class_name)) |
| 122 | { |
| 123 | require_once BASEPATH.'core/'.$class_name.'.php'; |
| 124 | } |
| 125 | |
| 126 | $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; |
| 127 | return $GLOBALS[strtoupper($global_name)]; |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | // convenience function for global mocks |
| 133 | function ci_set_core_class($name, $obj) |
| 134 | { |
| 135 | $orig =& $this->ci_core_class($name); |
| 136 | $orig = $obj; |
| 137 | } |
| 138 | |
| 139 | // -------------------------------------------------------------------- |
| 140 | |
Pascal Kriete | fe372e3 | 2011-04-21 00:59:45 -0400 | [diff] [blame] | 141 | function ci_set_config($key, $val = '') |
| 142 | { |
| 143 | if (is_array($key)) |
| 144 | { |
| 145 | $this->ci_config = $key; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | $this->ci_config[$key] = $val; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | function ci_config_array() |
| 156 | { |
| 157 | return $this->ci_config; |
| 158 | } |
| 159 | |
| 160 | // -------------------------------------------------------------------- |
| 161 | |
| 162 | function ci_config_item($item) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 163 | { |
| 164 | return ''; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // EOF |