From ca16c4ff1aa0cf5ebfbe877e9be755c0b7d2061c Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 28 Mar 2012 15:15:30 +0700 Subject: Adding autoloader and mocks directory --- tests/mocks/autoloader.php | 33 ++++++++ tests/mocks/ci_testcase.php | 189 ++++++++++++++++++++++++++++++++++++++++++++ tests/mocks/core/common.php | 132 +++++++++++++++++++++++++++++++ tests/mocks/core/loader.php | 33 ++++++++ tests/mocks/core/uri.php | 27 +++++++ 5 files changed, 414 insertions(+) create mode 100644 tests/mocks/autoloader.php create mode 100644 tests/mocks/ci_testcase.php create mode 100644 tests/mocks/core/common.php create mode 100644 tests/mocks/core/loader.php create mode 100644 tests/mocks/core/uri.php (limited to 'tests/mocks') diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php new file mode 100644 index 000000000..442389f81 --- /dev/null +++ b/tests/mocks/autoloader.php @@ -0,0 +1,33 @@ + 'bm', + 'config' => 'cfg', + 'hooks' => 'ext', + 'utf8' => 'uni', + 'router' => 'rtr', + 'output' => 'out', + 'security' => 'sec', + 'input' => 'in', + 'lang' => 'lang', + '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; + } + + // -------------------------------------------------------------------- + + /** + * 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 diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php new file mode 100644 index 000000000..fc94d7fff --- /dev/null +++ b/tests/mocks/core/common.php @@ -0,0 +1,132 @@ +ci_instance(); + return $instance; +} + +// -------------------------------------------------------------------- + +function &get_config() { + $test = CI_TestCase::instance(); + $config = $test->ci_get_config(); + + return $config; +} + +function config_item($item) +{ + $config =& get_config(); + + if ( ! isset($config[$item])) + { + return FALSE; + } + + return $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 = CI_TestCase::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 RuntimeException('CI Error: '.$message); +} + +function show_404($page = '', $log_error = TRUE) +{ + throw new RuntimeException('CI Error: 404'); +} + +function _exception_handler($severity, $message, $filepath, $line) +{ + throw new RuntimeException('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 diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php new file mode 100644 index 000000000..115ccc3de --- /dev/null +++ b/tests/mocks/core/loader.php @@ -0,0 +1,33 @@ +models_dir = vfsStream::newDirectory('models')->at(vfsStreamWrapper::getRoot()); + $this->libs_dir = vfsStream::newDirectory('libraries')->at(vfsStreamWrapper::getRoot()); + $this->helpers_dir = vfsStream::newDirectory('helpers')->at(vfsStreamWrapper::getRoot()); + $this->views_dir = vfsStream::newDirectory('views')->at(vfsStreamWrapper::getRoot()); + + $this->_ci_ob_level = ob_get_level(); + $this->_ci_library_paths = array(vfsStream::url('application').'/', BASEPATH); + $this->_ci_helper_paths = array(vfsStream::url('application').'/', BASEPATH); + $this->_ci_model_paths = array(vfsStream::url('application').'/'); + $this->_ci_view_paths = array(vfsStream::url('application').'/views/' => TRUE); + } +} \ No newline at end of file diff --git a/tests/mocks/core/uri.php b/tests/mocks/core/uri.php new file mode 100644 index 000000000..207c7dc41 --- /dev/null +++ b/tests/mocks/core/uri.php @@ -0,0 +1,27 @@ +ci_core_class('cfg'); + + // set predictable config values + $test->ci_set_config(array( + 'index_page' => 'index.php', + 'base_url' => 'http://example.com/', + 'subclass_prefix' => 'MY_' + )); + + $this->config = new $cls; + + } + + protected function _is_cli_request() + { + return FALSE; + } +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From ac5373a8979537f5454af6b911108541140a35d7 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 28 Mar 2012 16:03:38 +0700 Subject: Adding core and libraries mock classes --- tests/mocks/autoloader.php | 64 +++++++++++++++++++++++++++++++----- tests/mocks/core/loader.php | 3 -- tests/mocks/core/uri.php | 2 -- tests/mocks/database/.gitkeep | 0 tests/mocks/libraries/parser.php | 3 ++ tests/mocks/libraries/table.php | 3 ++ tests/mocks/libraries/typography.php | 3 ++ tests/mocks/libraries/useragent.php | 3 ++ 8 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 tests/mocks/database/.gitkeep create mode 100644 tests/mocks/libraries/parser.php create mode 100644 tests/mocks/libraries/table.php create mode 100644 tests/mocks/libraries/typography.php create mode 100644 tests/mocks/libraries/useragent.php (limited to 'tests/mocks') diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 442389f81..88070e508 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -6,25 +6,73 @@ // // Prototype : // -// include_once('Mock_Core_Common') // Will load ./mocks/core/common.php -// $mock_loader = new Mock_Core_Loader(); // Will load ./mocks/core/loader.php -// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php +// include_once('Mock_Core_Common') // Will load ./mocks/core/common.php +// $mock_loader = new Mock_Core_Loader(); // Will load ./mocks/core/loader.php +// $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php +// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php +// and so on... function autoload($class) { - $class = (strpos($class, 'Mock_') === 0) ? str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class) : $class; $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; - $file = $dir.strtolower($class).'.php'; + + $ci_core = array( + 'Benchmark', 'Config', 'Controller', + 'Exceptions', 'Hooks', 'Input', + 'Lang', 'Loader', 'Model', + 'Output', 'Router', 'Security', + 'URI', 'Utf8', + ); + + $ci_libraries = array( + 'Calendar', 'Cart', 'Driver', + 'Email', 'Encrypt', 'Form_validation', + 'Ftp', 'Image_lib', 'Javascript', + 'Log', 'Migration', 'Pagination', + 'Parser', 'Profiler', 'Session', + 'Table', 'Trackback', 'Typography', + 'Unit_test', 'Upload', 'User_agent', + 'Xmlrpc', 'Zip', + ); + + if (strpos($class, 'Mock_') === 0) + { + $class = str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class); + $class = strtolower($class); + } + elseif (strpos($class, 'CI_') === 0) + { + $fragments = explode('_', $class, 2); + $subclass = next($fragments); + + if (in_array($subclass, $ci_core)) + { + $dir = BASEPATH.'core'.DIRECTORY_SEPARATOR; + $class = $subclass; + } + elseif (in_array($subclass, $ci_libraries)) + { + $dir = BASEPATH.'libraries'.DIRECTORY_SEPARATOR; + $class = $subclass; + } + else + { + $class = strtolower($class); + } + } + + $file = $dir.$class.'.php'; if ( ! file_exists($file)) { $trace = debug_backtrace(); - // If the autoload call came from `class_exists`, we skipped - // and return FALSE - if ($trace[2]['function'] == 'class_exists') + // If the autoload call came from `class_exists` or `file_exists`, + // we skipped and return FALSE + if ($trace[2]['function'] == 'class_exists' OR $trace[2]['function'] == 'file_exists') { return FALSE; } + var_dump($file);die; throw new InvalidArgumentException("Unable to load $class."); } diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php index 115ccc3de..d4b29bb3d 100644 --- a/tests/mocks/core/loader.php +++ b/tests/mocks/core/loader.php @@ -1,8 +1,5 @@ Date: Wed, 28 Mar 2012 16:24:23 +0700 Subject: Implementation of Mock class, remove ugly reflection class --- tests/mocks/libraries/table.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'tests/mocks') diff --git a/tests/mocks/libraries/table.php b/tests/mocks/libraries/table.php index a1e998bb4..1a6ff8d35 100644 --- a/tests/mocks/libraries/table.php +++ b/tests/mocks/libraries/table.php @@ -1,3 +1,15 @@ Date: Wed, 28 Mar 2012 16:49:49 +0700 Subject: Remove include or require declaration from all helpers test --- tests/mocks/autoloader.php | 3 +-- tests/mocks/ci_testcase.php | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) (limited to 'tests/mocks') diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index 88070e508..bf3fbc508 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -6,8 +6,7 @@ // // Prototype : // -// include_once('Mock_Core_Common') // Will load ./mocks/core/common.php -// $mock_loader = new Mock_Core_Loader(); // Will load ./mocks/core/loader.php +// include_once('Mock_Core_Loader') // Will load ./mocks/core/loader.php // $mock_table = new Mock_Libraries_Table(); // Will load ./mocks/libraries/table.php // $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php // and so on... diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php index 5c83974b3..f327e6b07 100644 --- a/tests/mocks/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -48,6 +48,13 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->tear_down(); } } + + // -------------------------------------------------------------------- + + public static function instance() + { + return self::$ci_test_instance; + } // -------------------------------------------------------------------- @@ -62,6 +69,13 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->ci_config[$key] = $val; } } + + // -------------------------------------------------------------------- + + function ci_get_config() + { + return $this->ci_config; + } // -------------------------------------------------------------------- @@ -153,19 +167,12 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { self::$ci_test_instance = $this; parent::runBare(); } - - // -------------------------------------------------------------------- - - public static function instance() - { - return self::$ci_test_instance; - } - + // -------------------------------------------------------------------- - function ci_get_config() + function helper($name) { - return $this->ci_config; + require_once(BASEPATH.'helpers/'.$name.'_helper.php'); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From bdb84af43792f507eaab5ab0c4b3ec1be17a1a54 Mon Sep 17 00:00:00 2001 From: Taufan Aditya Date: Wed, 28 Mar 2012 17:03:59 +0700 Subject: Remove debugging line --- tests/mocks/autoloader.php | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/mocks') diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php index bf3fbc508..dd5929206 100644 --- a/tests/mocks/autoloader.php +++ b/tests/mocks/autoloader.php @@ -71,7 +71,6 @@ function autoload($class) { return FALSE; } - var_dump($file);die; throw new InvalidArgumentException("Unable to load $class."); } -- cgit v1.2.3-24-g4f1b