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/Bootstrap.php | 1 + tests/codeigniter/core/Common_test.php | 3 +- tests/codeigniter/database/.gitkeep | 0 tests/codeigniter/libraries/Parser_test.php | 7 +- tests/codeigniter/libraries/Table_test.php | 7 +- tests/codeigniter/libraries/Typography_test.php | 7 +- tests/codeigniter/libraries/User_agent_test.php | 91 ------------------------- tests/codeigniter/libraries/Useragent_test.php | 87 +++++++++++++++++++++++ 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 + 16 files changed, 163 insertions(+), 121 deletions(-) create mode 100644 tests/codeigniter/database/.gitkeep delete mode 100644 tests/codeigniter/libraries/User_agent_test.php create mode 100644 tests/codeigniter/libraries/Useragent_test.php 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 diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 62c7d0d52..9f89d1be8 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -13,6 +13,7 @@ define('APPPATH', PROJECT_BASE.'application/'); define('VIEWPATH', PROJECT_BASE.''); // Prep our test environment +require_once 'vfsStream/vfsStream.php'; include_once $dir.'/mocks/core/common.php'; include_once $dir.'/mocks/autoloader.php'; spl_autoload_register('autoload'); diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php index 29b512d8a..dded2e824 100644 --- a/tests/codeigniter/core/Common_test.php +++ b/tests/codeigniter/core/Common_test.php @@ -1,7 +1,6 @@ parser = new CI_Parser(); + $obj->parser = new Mock_Libraries_Parser(); $this->ci_instance($obj); diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index 0208a465a..7d0e4087f 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -1,14 +1,11 @@ table = new CI_table(); + $obj->table = new Mock_Libraries_Table(); $this->ci_instance($obj); diff --git a/tests/codeigniter/libraries/Typography_test.php b/tests/codeigniter/libraries/Typography_test.php index a0533bae0..250aefb24 100644 --- a/tests/codeigniter/libraries/Typography_test.php +++ b/tests/codeigniter/libraries/Typography_test.php @@ -1,14 +1,11 @@ type = new CI_Typography(); + $obj->type = new Mock_Libraries_Typography(); $this->ci_instance($obj); diff --git a/tests/codeigniter/libraries/User_agent_test.php b/tests/codeigniter/libraries/User_agent_test.php deleted file mode 100644 index 6f9e87196..000000000 --- a/tests/codeigniter/libraries/User_agent_test.php +++ /dev/null @@ -1,91 +0,0 @@ -_user_agent; - - $obj = new StdClass; - $obj->agent = new CI_User_agent(); - - $this->ci_instance($obj); - - $this->agent = $obj->agent; - } - - // -------------------------------------------------------------------- - - public function test_accept_lang() - { - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; - $this->assertTrue($this->agent->accept_lang()); - unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); - $this->assertTrue($this->agent->accept_lang('en')); - $this->assertFalse($this->agent->accept_lang('fr')); - } - - // -------------------------------------------------------------------- - - public function test_mobile() - { - // Mobile Not Set - $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua; - $this->assertEquals('', $this->agent->mobile()); - unset($_SERVER['HTTP_USER_AGENT']); - } - - // -------------------------------------------------------------------- - - public function test_util_is_functions() - { - $this->assertTrue($this->agent->is_browser()); - $this->assertFalse($this->agent->is_robot()); - $this->assertFalse($this->agent->is_mobile()); - $this->assertFalse($this->agent->is_referral()); - } - - // -------------------------------------------------------------------- - - public function test_agent_string() - { - $this->assertEquals($this->_user_agent, $this->agent->agent_string()); - } - - // -------------------------------------------------------------------- - - public function test_browser_info() - { - $this->assertEquals('Mac OS X', $this->agent->platform()); - $this->assertEquals('Safari', $this->agent->browser()); - $this->assertEquals('533.20.27', $this->agent->version()); - $this->assertEquals('', $this->agent->robot()); - $this->assertEquals('', $this->agent->referrer()); - } - - // -------------------------------------------------------------------- - - public function test_charsets() - { - $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; - - $charsets = $this->agent->charsets(); - - $this->assertEquals('utf8', $charsets[0]); - - unset($_SERVER['HTTP_ACCEPT_CHARSET']); - - $this->assertFalse($this->agent->accept_charset()); - } - - // -------------------------------------------------------------------- - -} \ No newline at end of file diff --git a/tests/codeigniter/libraries/Useragent_test.php b/tests/codeigniter/libraries/Useragent_test.php new file mode 100644 index 000000000..7dad7ac54 --- /dev/null +++ b/tests/codeigniter/libraries/Useragent_test.php @@ -0,0 +1,87 @@ +_user_agent; + + $obj = new StdClass; + $obj->agent = new Mock_Libraries_UserAgent(); + + $this->ci_instance($obj); + + $this->agent = $obj->agent; + } + + // -------------------------------------------------------------------- + + public function test_accept_lang() + { + $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en'; + $this->assertTrue($this->agent->accept_lang()); + unset($_SERVER['HTTP_ACCEPT_LANGUAGE']); + $this->assertTrue($this->agent->accept_lang('en')); + $this->assertFalse($this->agent->accept_lang('fr')); + } + + // -------------------------------------------------------------------- + + public function test_mobile() + { + // Mobile Not Set + $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua; + $this->assertEquals('', $this->agent->mobile()); + unset($_SERVER['HTTP_USER_AGENT']); + } + + // -------------------------------------------------------------------- + + public function test_util_is_functions() + { + $this->assertTrue($this->agent->is_browser()); + $this->assertFalse($this->agent->is_robot()); + $this->assertFalse($this->agent->is_mobile()); + $this->assertFalse($this->agent->is_referral()); + } + + // -------------------------------------------------------------------- + + public function test_agent_string() + { + $this->assertEquals($this->_user_agent, $this->agent->agent_string()); + } + + // -------------------------------------------------------------------- + + public function test_browser_info() + { + $this->assertEquals('Mac OS X', $this->agent->platform()); + $this->assertEquals('Safari', $this->agent->browser()); + $this->assertEquals('533.20.27', $this->agent->version()); + $this->assertEquals('', $this->agent->robot()); + $this->assertEquals('', $this->agent->referrer()); + } + + // -------------------------------------------------------------------- + + public function test_charsets() + { + $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; + + $charsets = $this->agent->charsets(); + + $this->assertEquals('utf8', $charsets[0]); + + unset($_SERVER['HTTP_ACCEPT_CHARSET']); + + $this->assertFalse($this->agent->accept_charset()); + } + + // -------------------------------------------------------------------- + +} \ No newline at end of file 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 @@