From 69c97a71476e4eaa6c629022fcd4ec7f36d4ec0d Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Wed, 20 Apr 2011 21:44:54 -0400 Subject: Adding early bootstrap ideas for core test suite --- tests/lib/ci_testcase.php | 110 ++++++++++++++++++++++++++++++++++++++++++ tests/lib/common.php | 120 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 tests/lib/ci_testcase.php create mode 100644 tests/lib/common.php (limited to 'tests/lib') diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php new file mode 100644 index 000000000..a8a272db2 --- /dev/null +++ b/tests/lib/ci_testcase.php @@ -0,0 +1,110 @@ + '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' + ); + + function __construct() + { + parent::__construct(); + } + + // -------------------------------------------------------------------- + + // Change what get_instance returns + function ci_instance($obj) + { + $this->ci_instance = $obj; + } + + // -------------------------------------------------------------------- + + function ci_set_instance_var($name, $obj) + { + $this->ci_instance->$name =& $obj; + } + + // -------------------------------------------------------------------- + + // Set a class to a mock before it is loaded + function ci_library($name) + { + + } + + // -------------------------------------------------------------------- + + /** + * 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(self::$global_map[$name])) + { + $class_name = ucfirst($name); + $global_name = self::$global_map[$name]; + } + elseif (in_array($name, self::$global_map)) + { + $class_name = ucfirst(array_search($name, self::$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; + } + + // -------------------------------------------------------------------- + + static function ci_config($item) + { + return ''; + } +} + +// EOF \ No newline at end of file diff --git a/tests/lib/common.php b/tests/lib/common.php new file mode 100644 index 000000000..482721a9a --- /dev/null +++ b/tests/lib/common.php @@ -0,0 +1,120 @@ +ci_instance; +} + +// Config Stuff | @todo High priority! +// -------------------------------------------------------------------- + +function get_config() { die('implement me'); } + +function config_item($item) +{ + return CodeIgniterTestCase::ci_config($item); +} + +// -------------------------------------------------------------------- + +function load_class($class, $directory = 'libraries', $prefix = 'CI_') +{ + if ($directory != 'core' OR $prefix != 'CI_') + { + throw new Exception('Not Implemented: Non-core load_class()'); + } + + $test = CodeIgniterTestCase::$test_instance; + + $obj =& $test->ci_core_class($class); + + if (is_string($obj)) + { + throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.''); + } + + return $obj; +} + +// This is sort of meh. Should probably be mocked up with +// controllable output, so that we can test some of our +// security code. The function itself will be tested in the +// bootstrap testsuite. +// -------------------------------------------------------------------- + +function remove_invisible_characters($str, $url_encoded = TRUE) +{ + $non_displayables = array(); + + // every control character except newline (dec 10) + // carriage return (dec 13), and horizontal tab (dec 09) + + if ($url_encoded) + { + $non_displayables[] = '/%0[0-8bcef]/'; // url encoded 00-08, 11, 12, 14, 15 + $non_displayables[] = '/%1[0-9a-f]/'; // url encoded 16-31 + } + + $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S'; // 00-08, 11, 12, 14-31, 127 + + do + { + $str = preg_replace($non_displayables, '', $str, -1, $count); + } + while ($count); + + return $str; +} + + +// Clean up error messages +// -------------------------------------------------------------------- + +function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') +{ + throw new Exception('CI Error: '.$message); +} + +function show_404($page = '', $log_error = TRUE) +{ + throw new Exception('CI Error: 404'); +} + +function _exception_handler($severity, $message, $filepath, $line) +{ + throw new Exception('CI Exception: '.$message.' | '.$filepath.' | '.$line); +} + + +// We assume a few things about our environment ... +// -------------------------------------------------------------------- + +function is_php($version = '5.0.0') +{ + return ! (version_compare(PHP_VERSION, $version) < 0); +} + +function is_really_writable($file) +{ + return is_writable($file); +} + +function is_loaded() +{ + throw new Exception('Bad Isolation: mock up environment'); +} + +function log_message($level = 'error', $message, $php_error = FALSE) +{ + return TRUE; +} + +function set_status_header($code = 200, $text = '') +{ + return TRUE; +} + +// EOF \ No newline at end of file -- cgit v1.2.3-24-g4f1b From fe372e30a8f05ec4a77dc9a5ea2ec471b3488bd3 Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Thu, 21 Apr 2011 00:59:45 -0400 Subject: Fixing up the scope soup, and adding a way to set core config items. --- tests/lib/ci_testcase.php | 74 ++++++++++++++++++++++++++++++++++++++++++----- tests/lib/common.php | 18 ++++++++---- 2 files changed, 78 insertions(+), 14 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php index a8a272db2..04216e2a8 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/lib/ci_testcase.php @@ -4,10 +4,8 @@ // Need a way to change dependencies (core libs and laoded libs) // Need a way to set the CI class -class CodeIgniterTestCase extends PHPUnit_Framework_TestCase { +class CI_TestCase extends PHPUnit_Framework_TestCase { - public $ci_instance; - public static $test_instance; public static $global_map = array( 'benchmark' => 'bm', 'config' => 'cfg', @@ -23,23 +21,62 @@ class CodeIgniterTestCase extends PHPUnit_Framework_TestCase { 'loader' => 'load' ); - function __construct() + protected $ci_config = array(); + + protected $ci_instance; + protected static $ci_test_instance; + + + public function __construct() { parent::__construct(); } // -------------------------------------------------------------------- - // Change what get_instance returns - function ci_instance($obj) + /** + * 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_instance($obj = FALSE) + { + if ( ! is_object($obj)) + { + return $this->ci_instance; + } + $this->ci_instance = $obj; } // -------------------------------------------------------------------- - function ci_set_instance_var($name, $obj) + function ci_instance_var($name, $obj = FALSE) { + if ( ! is_object($obj)) + { + return $this->ci_instance->$name; + } + $this->ci_instance->$name =& $obj; } @@ -101,7 +138,28 @@ class CodeIgniterTestCase extends PHPUnit_Framework_TestCase { // -------------------------------------------------------------------- - static function ci_config($item) + function ci_set_config($key, $val = '') + { + if (is_array($key)) + { + $this->ci_config = $key; + } + else + { + $this->ci_config[$key] = $val; + } + } + + // -------------------------------------------------------------------- + + function ci_config_array() + { + return $this->ci_config; + } + + // -------------------------------------------------------------------- + + function ci_config_item($item) { return ''; } diff --git a/tests/lib/common.php b/tests/lib/common.php index 482721a9a..994e9bc22 100644 --- a/tests/lib/common.php +++ b/tests/lib/common.php @@ -4,18 +4,24 @@ function &get_instance() { - $test = CodeIgniterTestCase::$test_instance; - return $test->ci_instance; + $test = CI_TestCase::instance(); + $instance = $test->ci_instance(); + return $instance; } -// Config Stuff | @todo High priority! // -------------------------------------------------------------------- -function get_config() { die('implement me'); } +function &get_config() { + $test = CI_TestCase::instance(); + $config = $test->ci_config_array(); + + return $config; +} function config_item($item) { - return CodeIgniterTestCase::ci_config($item); + $test = CI_TestCase::instance(); + return $test->ci_config_item($item); } // -------------------------------------------------------------------- @@ -27,7 +33,7 @@ function load_class($class, $directory = 'libraries', $prefix = 'CI_') throw new Exception('Not Implemented: Non-core load_class()'); } - $test = CodeIgniterTestCase::$test_instance; + $test = CI_TestCase::instance(); $obj =& $test->ci_core_class($class); -- cgit v1.2.3-24-g4f1b From ae7b3f9265ab5d5776d729d6258f3ad2b311ac8c Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Thu, 21 Apr 2011 01:21:27 -0400 Subject: a bit of shuffling around in CI_TestCase --- tests/lib/ci_testcase.php | 89 ++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 51 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php index 04216e2a8..c8c6bc900 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/lib/ci_testcase.php @@ -5,8 +5,12 @@ // 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; - public static $global_map = array( + private $global_map = array( 'benchmark' => 'bm', 'config' => 'cfg', 'hooks' => 'ext', @@ -21,39 +25,25 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { 'loader' => 'load' ); - protected $ci_config = array(); - - protected $ci_instance; - protected static $ci_test_instance; - - public function __construct() { parent::__construct(); + + $this->ci_config = array(); } // -------------------------------------------------------------------- - /** - * 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() + function ci_set_config($key, $val = '') { - return self::$ci_test_instance; + if (is_array($key)) + { + $this->ci_config = $key; + } + else + { + $this->ci_config[$key] = $val; + } } // -------------------------------------------------------------------- @@ -80,14 +70,6 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->ci_instance->$name =& $obj; } - // -------------------------------------------------------------------- - - // Set a class to a mock before it is loaded - function ci_library($name) - { - - } - // -------------------------------------------------------------------- /** @@ -103,14 +85,14 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { { $name = strtolower($name); - if (isset(self::$global_map[$name])) + if (isset($this->global_map[$name])) { $class_name = ucfirst($name); - $global_name = self::$global_map[$name]; + $global_name = $this->global_map[$name]; } - elseif (in_array($name, self::$global_map)) + elseif (in_array($name, $this->global_map)) { - $class_name = ucfirst(array_search($name, self::$global_map)); + $class_name = ucfirst(array_search($name, $this->global_map)); $global_name = $name; } else @@ -136,32 +118,37 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $orig = $obj; } + // -------------------------------------------------------------------- + // Internals // -------------------------------------------------------------------- - function ci_set_config($key, $val = '') + /** + * 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() { - if (is_array($key)) - { - $this->ci_config = $key; - } - else - { - $this->ci_config[$key] = $val; - } + self::$ci_test_instance = $this; + parent::runBare(); } // -------------------------------------------------------------------- - function ci_config_array() + public static function instance() { - return $this->ci_config; + return self::$ci_test_instance; } // -------------------------------------------------------------------- - function ci_config_item($item) + function ci_get_config() { - return ''; + return $this->ci_config; } } -- cgit v1.2.3-24-g4f1b From 88b296311090acdb84719148d3c722ca30bd3ed7 Mon Sep 17 00:00:00 2001 From: Pascal Kriete Date: Thu, 21 Apr 2011 01:21:55 -0400 Subject: Making config_item work again after I pulled it from CI_TestCase --- tests/lib/common.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/common.php b/tests/lib/common.php index 994e9bc22..6d29eb0d6 100644 --- a/tests/lib/common.php +++ b/tests/lib/common.php @@ -13,15 +13,21 @@ function &get_instance() function &get_config() { $test = CI_TestCase::instance(); - $config = $test->ci_config_array(); + $config = $test->ci_get_config(); return $config; } function config_item($item) { - $test = CI_TestCase::instance(); - return $test->ci_config_item($item); + $config =& get_config(); + + if ( ! isset($config[$item])) + { + return FALSE; + } + + return $config[$item]; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 8da69039f6d855eb4f88de73702155e6899d2d23 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Thu, 21 Apr 2011 11:28:27 -0500 Subject: Working on tests in the loader. Have generic model mocking working with vfsStream. --- tests/lib/ci_testcase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php index c8c6bc900..10539a3af 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/lib/ci_testcase.php @@ -20,9 +20,9 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { 'security' => 'sec', 'input' => 'in', 'lang' => 'lang', - // @todo the loader is an edge case - 'loader' => 'load' + 'loader' => 'load', + 'model' => 'model' ); public function __construct() -- cgit v1.2.3-24-g4f1b From 68286a4dcc1ed4d904ad992173c1b3621bf6fced Mon Sep 17 00:00:00 2001 From: Eric Barnes Date: Thu, 21 Apr 2011 22:00:33 -0400 Subject: Reworked unit tests to match rest of framework and added a few more. --- tests/lib/ci_testcase.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests/lib') diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php index 10539a3af..8ca71fdf2 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/lib/ci_testcase.php @@ -25,6 +25,8 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { 'model' => 'model' ); + // -------------------------------------------------------------------- + public function __construct() { parent::__construct(); @@ -34,6 +36,26 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { // -------------------------------------------------------------------- + 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)) -- cgit v1.2.3-24-g4f1b From 7729faa553c0ec93a13533003a53dc66078467a8 Mon Sep 17 00:00:00 2001 From: Hamza Bhatti Date: Sat, 10 Mar 2012 13:07:05 +0400 Subject: Fix test errors in Loader_test.php and URI_test.php Change exceptions from Exception to RuntimeException since PHPUnit 3.6 doesn't like you to expect generic exceptions. The error it gives is: InvalidArgumentException: You must not expect the generic exception class travis-ci.org/#!/tiyowan/CodeIgniter/builds/832518 This issue addressed by using exceptions that are more specific. --- tests/lib/common.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/lib') diff --git a/tests/lib/common.php b/tests/lib/common.php index 6d29eb0d6..4a832587d 100644 --- a/tests/lib/common.php +++ b/tests/lib/common.php @@ -87,17 +87,17 @@ function remove_invisible_characters($str, $url_encoded = TRUE) function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered') { - throw new Exception('CI Error: '.$message); + throw new RuntimeException('CI Error: '.$message); } function show_404($page = '', $log_error = TRUE) { - throw new Exception('CI Error: 404'); + throw new RuntimeException('CI Error: 404'); } function _exception_handler($severity, $message, $filepath, $line) { - throw new Exception('CI Exception: '.$message.' | '.$filepath.' | '.$line); + throw new RuntimeException('CI Exception: '.$message.' | '.$filepath.' | '.$line); } @@ -129,4 +129,4 @@ function set_status_header($code = 200, $text = '') return TRUE; } -// EOF \ No newline at end of file +// EOF -- cgit v1.2.3-24-g4f1b From 8749bc7e836c196dfef37d3b7b5a67736a15092c Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Sun, 11 Mar 2012 05:43:45 +0700 Subject: Fix incomplete and skipped test --- tests/lib/ci_testcase.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests/lib') diff --git a/tests/lib/ci_testcase.php b/tests/lib/ci_testcase.php index 8ca71fdf2..afccee017 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/lib/ci_testcase.php @@ -172,6 +172,23 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { { return $this->ci_config; } + + // -------------------------------------------------------------------- + + /** + * This overload is useful to create a stub, that need to have a specific method. + */ + function __call($method, $args) + { + if ($this->{$method} instanceof Closure) + { + return call_user_func_array($this->{$method},$args); + } + else + { + return parent::__call($method, $args); + } + } } // EOF \ No newline at end of file -- cgit v1.2.3-24-g4f1b