diff options
author | Greg Aker <greg.aker@ellislab.com> | 2011-05-06 18:38:04 +0200 |
---|---|---|
committer | Greg Aker <greg.aker@ellislab.com> | 2011-05-06 18:38:04 +0200 |
commit | bd4c87543818bd4573308743d44e12b78349ecd0 (patch) | |
tree | 8afedb90cf13a1a65f89de70dbb4f0c26d4ccf05 /tests/lib/ci_testcase.php | |
parent | 02958b5b78835a484c1038d77f4bcfc5ae273a2d (diff) | |
parent | 827f3de2733e85ede6311feb2e4bf73ecf209eb3 (diff) |
merging in changes
Diffstat (limited to 'tests/lib/ci_testcase.php')
-rw-r--r-- | tests/lib/ci_testcase.php | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php new file mode 100644 index 000000000..8ca71fdf2 --- /dev/null +++ b/tests/lib/ci_testcase.php @@ -0,0 +1,177 @@ +<?php + + +// Need a way to change dependencies (core libs and laoded libs) +// Need a way to set the CI class + +class CI_TestCase extends PHPUnit_Framework_TestCase { + + protected $ci_config; + protected $ci_instance; + protected static $ci_test_instance; + + private $global_map = array( + 'benchmark' => 'bm', + 'config' => 'cfg', + 'hooks' => 'ext', + 'utf8' => 'uni', + 'router' => 'rtr', + 'output' => 'out', + 'security' => 'sec', + 'input' => 'in', + 'lang' => 'lang', + // @todo the loader is an edge case + 'loader' => 'load', + 'model' => 'model' + ); + + // -------------------------------------------------------------------- + + public function __construct() + { + parent::__construct(); + + $this->ci_config = array(); + } + + // -------------------------------------------------------------------- + + public function setUp() + { + if (method_exists($this, 'set_up')) + { + $this->set_up(); + } + } + + // -------------------------------------------------------------------- + + public function tearDown() + { + if (method_exists($this, 'tear_down')) + { + $this->tear_down(); + } + } + + // -------------------------------------------------------------------- + + function ci_set_config($key, $val = '') + { + if (is_array($key)) + { + $this->ci_config = $key; + } + else + { + $this->ci_config[$key] = $val; + } + } + + // -------------------------------------------------------------------- + + function ci_instance($obj = FALSE) + { + if ( ! is_object($obj)) + { + return $this->ci_instance; + } + + $this->ci_instance = $obj; + } + + // -------------------------------------------------------------------- + + function ci_instance_var($name, $obj = FALSE) + { + if ( ! is_object($obj)) + { + return $this->ci_instance->$name; + } + + $this->ci_instance->$name =& $obj; + } + + // -------------------------------------------------------------------- + + /** + * Grab a core class + * + * Loads the correct core class without extensions + * and returns a reference to the class name in the + * globals array with the correct key. This way the + * test can modify the variable it assigns to and + * still maintain the global. + */ + function &ci_core_class($name) + { + $name = strtolower($name); + + if (isset($this->global_map[$name])) + { + $class_name = ucfirst($name); + $global_name = $this->global_map[$name]; + } + elseif (in_array($name, $this->global_map)) + { + $class_name = ucfirst(array_search($name, $this->global_map)); + $global_name = $name; + } + else + { + throw new Exception('Not a valid core class.'); + } + + if ( ! class_exists('CI_'.$class_name)) + { + require_once BASEPATH.'core/'.$class_name.'.php'; + } + + $GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name; + return $GLOBALS[strtoupper($global_name)]; + } + + // -------------------------------------------------------------------- + + // convenience function for global mocks + function ci_set_core_class($name, $obj) + { + $orig =& $this->ci_core_class($name); + $orig = $obj; + } + + // -------------------------------------------------------------------- + // Internals + // -------------------------------------------------------------------- + + /** + * Overwrite runBare + * + * PHPUnit instantiates the test classes before + * running them individually. So right before a test + * runs we set our instance. Normally this step would + * happen in setUp, but someone is bound to forget to + * call the parent method and debugging this is no fun. + */ + public function runBare() + { + self::$ci_test_instance = $this; + parent::runBare(); + } + + // -------------------------------------------------------------------- + + public static function instance() + { + return self::$ci_test_instance; + } + + // -------------------------------------------------------------------- + + function ci_get_config() + { + return $this->ci_config; + } +} + +// EOF
\ No newline at end of file |