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