From 6030719346e9b0eb0e0ea99c679cadeb1fe4afde Mon Sep 17 00:00:00 2001 From: dchill42 Date: Mon, 27 Aug 2012 23:59:31 -0400 Subject: Improved VFS usage in Loader and Config units, added Loader driver test, and moved config load testing to Config unit Signed-off-by: dchill42 --- tests/codeigniter/core/Config_test.php | 52 +++++- tests/codeigniter/core/Loader_test.php | 287 ++++++++++++++++++++++++++++----- 2 files changed, 294 insertions(+), 45 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 30cb90a28..7782a7898 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -90,4 +90,54 @@ class Config_test extends CI_TestCase { $this->assertEquals('http://example.com/system/', $this->config->system_url()); } -} \ No newline at end of file + // -------------------------------------------------------------------- + + public function test_load() + { + // Create VFS tree of application config files + $file1 = 'test.php'; + $file2 = 'secttest'; + $key1 = 'testconfig'; + $val1 = 'my_value'; + $cfg1 = array( + $key1 => $val1 + ); + $cfg2 = array( + 'one' => 'prime', + 'two' => 2, + 'three' => true + ); + $tree = array( + 'application' => array( + 'config' => array( + $file1 => ' 'config->_config_paths = array(vfsStream::url('application').'/'); + + // Test regular load + $this->assertTrue($this->config->load($file1)); + $this->assertEquals($val1, $this->config->item($key1)); + + // Test section load + $this->assertTrue($this->config->load($file2, TRUE)); + $this->assertEquals($cfg2, $this->config->item($file2)); + + // Test graceful fail + $this->assertFalse($this->config->load('not_config_file', FALSE, TRUE)); + + // Test regular fail + $file3 = 'absentia'; + $this->setExpectedException( + 'RuntimeException', + 'CI Error: The configuration file '.$file3.'.php does not exist.' + ); + $this->assertNull($this->config->load($file3)); + } + +} diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index fdea962b7..bb8bfd733 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -18,54 +18,123 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::library + */ public function test_library() { $this->_setup_config_mock(); + // Create libraries directory with test library + $lib = 'unit_test_lib'; + $class = 'CI_'.ucfirst($lib); + $content = '_create_content('libraries', $lib, $content, NULL, TRUE); + // Test loading as an array. - $this->assertNull($this->load->library(array('table'))); - $this->assertTrue(class_exists('CI_Table'), 'Table class exists'); - $this->assertAttributeInstanceOf('CI_Table', 'table', $this->ci_obj); + $this->assertNull($this->load->library(array($lib))); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); // Test no lib given - $this->assertEquals(FALSE, $this->load->library()); + $this->assertFalse($this->load->library()); // Test a string given to params - $this->assertEquals(NULL, $this->load->library('table', ' ')); + $this->assertNull($this->load->library($lib, ' ')); + } + + // -------------------------------------------------------------------- + + /** + * @covers CI_Loader::library + */ + public function test_library_config() + { + $this->_setup_config_mock(); + + // Create libraries directory with test library + $lib = 'unit_test_config_lib'; + $class = 'CI_'.ucfirst($lib); + $content = 'config = $params; } } '; + $this->_create_content('libraries', $lib, $content, NULL, TRUE); + + // Create config file + $cfg = array( + 'foo' => 'bar', + 'bar' => 'baz', + 'baz' => false + ); + $this->_create_content('config', $lib, 'assertNull($this->load->library($lib, NULL, $obj)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + $this->assertEquals($cfg, $this->ci_obj->$obj->config); } // -------------------------------------------------------------------- + /** + * @covers CI_Loader::library + */ public function test_load_library_in_application_dir() { $this->_setup_config_mock(); - $content = '_create_content('libraries', $lib, $content); - $model = vfsStream::newFile('Super_test_library.php')->withContent($content)->at($this->load->libs_dir); - $this->assertNull($this->load->library('super_test_library')); + // Load library + $this->assertNull($this->load->library($lib)); // Was the model class instantiated. - $this->assertTrue(class_exists('Super_test_library')); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); } // -------------------------------------------------------------------- - private function _setup_config_mock() + /** + * @covers CI_Loader::driver + */ + public function test_driver() { - // Mock up a config object until we - // figure out how to test the library configs - $config = $this->getMock('CI_Config', NULL, array(), '', FALSE); - $config->expects($this->any()) - ->method('load') - ->will($this->returnValue(TRUE)); + $this->_setup_config_mock(); - // Add the mock to our stdClass - $this->ci_instance_var('config', $config); + // Create libraries directory with test driver + $driver = 'unit_test_driver'; + $dir = ucfirst($driver); + $class = 'CI_'.$dir; + $content = '_create_content('libraries', $driver, $content, $dir, TRUE); + + // Test loading as an array. + $this->assertNull($this->load->driver(array($driver))); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $driver, $this->ci_obj); + + // Test loading as a library with a name + $obj = 'testdrive'; + $this->assertNull($this->load->library($driver, NULL, $obj)); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + + // Test no driver given + $this->assertFalse($this->load->driver()); + + // Test a string given to params + $this->assertNull($this->load->driver($driver, ' ')); } // -------------------------------------------------------------------- + /** + * @covers CI_Loader::model + */ public function test_non_existent_model() { $this->setExpectedException( @@ -79,20 +148,23 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- /** - * @coverts CI_Loader::model + * @covers CI_Loader::model */ public function test_models() { $this->ci_set_core_class('model', 'CI_Model'); - $content = 'withContent($content)->at($this->load->models_dir); + // Create models directory with test model + $model = 'unit_test_model'; + $class = ucfirst($model); + $content = '_create_content('models', $model, $content); - $this->assertNull($this->load->model('unit_test_model')); + // Load model + $this->assertNull($this->load->model($model)); // Was the model class instantiated. - $this->assertTrue(class_exists('Unit_test_model')); + $this->assertTrue(class_exists($class)); // Test no model given $this->assertNull($this->load->model('')); @@ -109,26 +181,27 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- /** - * @coverts CI_Loader::view + * @covers CI_Loader::view */ public function test_load_view() { $this->ci_set_core_class('output', 'CI_Output'); + // Create views directory with test view + $view = 'unit_test_view'; $content = 'This is my test page. '; - $view = vfsStream::newFile('unit_test_view.php')->withContent($content)->at($this->load->views_dir); + $this->_create_content('views', $view, $content); // Use the optional return parameter in this test, so the view is not // run through the output class. - $this->assertEquals('This is my test page. World!', - $this->load->view('unit_test_view', array('hello' => "World!"), TRUE)); - + $out = $this->load->view($view, array('hello' => "World!"), TRUE); + $this->assertEquals('This is my test page. World!', $out); } // -------------------------------------------------------------------- /** - * @coverts CI_Loader::view + * @covers CI_Loader::view */ public function test_non_existent_view() { @@ -142,16 +215,22 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::file + */ public function test_file() { + // Create views directory with test file + $dir = 'views'; + $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; - $file = vfsStream::newFile('ci_test_mock_file.php')->withContent($content)->at($this->load->views_dir); + $this->_create_content($dir, $file, $content); // Just like load->view(), take the output class out of the mix here. - $load = $this->load->file(vfsStream::url('application').'/views/ci_test_mock_file.php', TRUE); - - $this->assertEquals($content, $load); + $out = $this->load->file($this->load->app_path.$dir.'/'.$file.'.php', TRUE); + $this->assertEquals($content, $out); + // Test non-existent file $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: ci_test_file_not_exists' @@ -162,6 +241,9 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::vars + */ public function test_vars() { $this->assertNull($this->load->vars(array('foo' => 'bar'))); @@ -170,10 +252,22 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::helper + */ public function test_helper() { - $this->assertEquals(NULL, $this->load->helper('array')); + // Create helper directory in app path with test helper + $helper = 'test'; + $func = '_my_helper_test_func'; + $content = '_create_content('helpers', $helper.'_helper', $content); + + // Load helper + $this->assertEquals(NULL, $this->load->helper($helper)); + $this->assertTrue(function_exists($func), $func.' does not exist'); + // Test non-existent helper $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: helpers/bad_helper.php' @@ -184,9 +278,31 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::helper + */ public function test_loading_multiple_helpers() { - $this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string'))); + // Create helper directory in base path with test helpers + $helpers = array(); + $funcs = array(); + $files = array(); + for ($i = 1; $i <= 3; ++$i) { + $helper = 'test'.$i; + $helpers[] = $helper; + $func = '_my_helper_test_func'.$i; + $funcs[] = $func; + $files[$helper.'_helper'] = '_create_content('helpers', $files, NULL, NULL, TRUE); + + // Load helpers + $this->assertEquals(NULL, $this->load->helpers($helpers)); + + // Verify helper existence + foreach ($funcs as $func) { + $this->assertTrue(function_exists($func), $func.' does not exist'); + } } // -------------------------------------------------------------------- @@ -198,6 +314,52 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::add_package_path + * @covers CI_Loader::get_package_paths + * @covers CI_Loader::remove_package_path + */ + public function test_packages() + { + $this->_setup_config_mock(); + + // Create third-party directory in app path with model + $dir = 'third-party'; + $lib = 'unit_test_package'; + $class = 'CI_'.ucfirst($lib); + $content = '_create_content($dir, $lib, $content); + + // Test failed load without path + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->load->library($lib); + + // Clear exception and get paths + $this->setExpectedException(NULL); + $paths = $this->load->get_package_paths(TRUE); + + // Add path and verify + $path = $this->load->app_path.$dir; + $this->assertNull($this->load->add_package_path($path)); + $this->assertContains($path, $this->load->get_package_paths(TRUE)); + + // Test successful load + $this->assertNull($this->load->library($lib)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + + // Remove path and verify restored paths + $this->assertNull($this->load->remove_package_path($path)); + $this->assertEquals($paths, $this->load->get_package_paths(TRUE)); + } + + // -------------------------------------------------------------------- + + /** + * @covers CI_Loader::config + */ public function test_load_config() { $this->_setup_config_mock(); @@ -206,16 +368,53 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_load_bad_config() + private function _setup_config_mock() { - $this->_setup_config_mock(); + // Mock up a config object so config() has something to call + $config = $this->getMock('CI_Config', array('load'), array(), '', FALSE); + $config->expects($this->any()) + ->method('load') + ->will($this->returnValue(TRUE)); - $this->setExpectedException( - 'RuntimeException', - 'CI Error: The configuration file foobar.php does not exist.' - ); + // Reinitialize config paths to use VFS + $config->_config_paths = array($this->load->app_path); - $this->load->config('foobar', FALSE); + // Add the mock to our stdClass + $this->ci_instance_var('config', $config); + } + + // -------------------------------------------------------------------- + + private function _create_content($dir, $file, $content, $sub = NULL, $base = FALSE) + { + // Create structure containing directory + $tree = array($dir => array()); + + // Check for subdirectory + if ($sub) { + // Add subdirectory to tree and get reference + $tree[$dir][$sub] = array(); + $leaf =& $tree[$dir][$sub]; + } + else { + // Get reference to main directory + $leaf =& $tree[$dir]; + } + + // Check for multiple files + if (is_array($file)) { + // Add multiple files to directory + foreach ($file as $name => $data) { + $leaf[$name.'.php'] = $data; + } + } + else { + // Add single file with content + $leaf[$file.'.php'] = $content; + } + + // Create structure under app or base path + vfsStream::create($tree, $base ? $this->load->base_root : $this->load->app_root); } -} \ No newline at end of file +} -- cgit v1.2.3-24-g4f1b From 60d100f0fde3fba4fa4015f44f490b7ecac16138 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Wed, 29 Aug 2012 12:58:26 -0400 Subject: Added autoloader unit test with minor supporting change in Loader Signed-off-by: dchill42 --- tests/codeigniter/core/Loader_test.php | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index bb8bfd733..db79e6a8b 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -368,6 +368,76 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- + /** + * @covers CI_Loader::_ci_autoloader + */ + public function test_autoloader() + { + $this->_setup_config_mock(); + + // Create helper directory in app path with test helper + $helper = 'autohelp'; + $hlp_func = '_autohelp_test_func'; + $this->_create_content('helpers', $helper.'_helper', '_create_content('libraries', $lib, ' array($drv.'.php' => 'load->base_root->getChild('libraries')); + + // Create package directory in app path with model + $dir = 'testdir'; + $path = $this->load->app_path.$dir.'/'; + $model = 'automod'; + $mod_class = ucfirst($model); + $this->_create_content($dir, $model, ' array($path), + 'helper' => array($helper), + 'libraries' => array($lib), + 'drivers' => array($drv), + 'model' => array($model), + 'config' => array() + ); + $this->_create_content('config', 'autoload', 'load->autoload(); + + // Verify path + $this->assertContains($path, $this->load->get_package_paths()); + + // Verify helper + $this->assertTrue(function_exists($hlp_func), $hlp_func.' does not exist'); + + // Verify library + $this->assertTrue(class_exists($lib_class), $lib_class.' does not exist'); + $this->assertAttributeInstanceOf($lib_class, $lib, $this->ci_obj); + + // Verify driver + $this->assertTrue(class_exists($drv_class), $drv_class.' does not exist'); + $this->assertAttributeInstanceOf($drv_class, $drv, $this->ci_obj); + + // Verify model + $this->assertTrue(class_exists($mod_class), $mod_class.' does not exist'); + $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); + } + + // -------------------------------------------------------------------- + private function _setup_config_mock() { // Mock up a config object so config() has something to call -- cgit v1.2.3-24-g4f1b From 7ecc5cda6647a4b316b44dc40d5925d9ef63c908 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Fri, 12 Oct 2012 16:25:51 -0400 Subject: Integrated vfsStream better and made paths constants VFS-based Signed-off-by: dchill42 --- tests/codeigniter/core/Lang_test.php | 2 + tests/codeigniter/core/Loader_test.php | 124 +++++------------------ tests/codeigniter/helpers/date_helper_test.php | 2 + tests/codeigniter/helpers/form_helper_test.php | 8 +- tests/codeigniter/helpers/number_helper_test.php | 17 +--- tests/codeigniter/helpers/text_helper_test.php | 1 + tests/codeigniter/libraries/Encrypt_test.php | 7 +- tests/codeigniter/libraries/Parser_test.php | 8 +- tests/codeigniter/libraries/Session_test.php | 12 ++- tests/codeigniter/libraries/Table_test.php | 8 +- tests/codeigniter/libraries/Typography_test.php | 8 +- tests/codeigniter/libraries/Upload_test.php | 57 ++++++----- tests/codeigniter/libraries/Useragent_test.php | 7 +- 13 files changed, 87 insertions(+), 174 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Lang_test.php b/tests/codeigniter/core/Lang_test.php index a410dabfa..3364362e0 100644 --- a/tests/codeigniter/core/Lang_test.php +++ b/tests/codeigniter/core/Lang_test.php @@ -17,6 +17,7 @@ class Lang_test extends CI_TestCase { public function test_load() { + $this->ci_vfs_clone('system/language/english/profiler_lang.php'); $this->assertTrue($this->lang->load('profiler', 'english')); $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } @@ -25,6 +26,7 @@ class Lang_test extends CI_TestCase { public function test_load_with_unspecified_language() { + $this->ci_vfs_clone('system/language/english/profiler_lang.php'); $this->assertTrue($this->lang->load('profiler')); $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index db79e6a8b..69b2afb63 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -9,11 +9,11 @@ class Loader_test extends CI_TestCase { // Instantiate a new loader $this->load = new Mock_Core_Loader(); - // mock up a ci instance - $this->ci_obj = new stdClass; + // Get CI instance + $this->ci_obj = $this->ci_instance(); - // Fix get_instance() - $this->ci_instance($this->ci_obj); + // Set subclass prefix + $this->ci_set_config('subclass_prefix', 'MY_'); } // -------------------------------------------------------------------- @@ -23,13 +23,10 @@ class Loader_test extends CI_TestCase { */ public function test_library() { - $this->_setup_config_mock(); - // Create libraries directory with test library $lib = 'unit_test_lib'; $class = 'CI_'.ucfirst($lib); - $content = '_create_content('libraries', $lib, $content, NULL, TRUE); + $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); // Test loading as an array. $this->assertNull($this->load->library(array($lib))); @@ -50,13 +47,11 @@ class Loader_test extends CI_TestCase { */ public function test_library_config() { - $this->_setup_config_mock(); - // Create libraries directory with test library $lib = 'unit_test_config_lib'; $class = 'CI_'.ucfirst($lib); - $content = 'config = $params; } } '; - $this->_create_content('libraries', $lib, $content, NULL, TRUE); + $content = 'config = $params; } }'; + $this->ci_vfs_create($lib, $content, $this->ci_base_root, 'libraries'); // Create config file $cfg = array( @@ -64,7 +59,7 @@ class Loader_test extends CI_TestCase { 'bar' => 'baz', 'baz' => false ); - $this->_create_content('config', $lib, 'ci_vfs_create($lib, 'ci_app_root, 'config'); // Test object name and config $obj = 'testy'; @@ -81,13 +76,10 @@ class Loader_test extends CI_TestCase { */ public function test_load_library_in_application_dir() { - $this->_setup_config_mock(); - // Create libraries directory in app path with test library $lib = 'super_test_library'; $class = ucfirst($lib); - $content = '_create_content('libraries', $lib, $content); + $this->ci_vfs_create($lib, 'ci_app_root, 'libraries'); // Load library $this->assertNull($this->load->library($lib)); @@ -104,14 +96,12 @@ class Loader_test extends CI_TestCase { */ public function test_driver() { - $this->_setup_config_mock(); - // Create libraries directory with test driver $driver = 'unit_test_driver'; $dir = ucfirst($driver); $class = 'CI_'.$dir; $content = '_create_content('libraries', $driver, $content, $dir, TRUE); + $this->ci_vfs_create($driver, $content, $this->ci_base_root, 'libraries/'.$dir); // Test loading as an array. $this->assertNull($this->load->driver(array($driver))); @@ -158,7 +148,7 @@ class Loader_test extends CI_TestCase { $model = 'unit_test_model'; $class = ucfirst($model); $content = '_create_content('models', $model, $content); + $this->ci_vfs_create($model, $content, $this->ci_app_root, 'models'); // Load model $this->assertNull($this->load->model($model)); @@ -190,7 +180,7 @@ class Loader_test extends CI_TestCase { // Create views directory with test view $view = 'unit_test_view'; $content = 'This is my test page. '; - $this->_create_content('views', $view, $content); + $this->ci_vfs_create($view, $content, $this->ci_app_root, 'views'); // Use the optional return parameter in this test, so the view is not // run through the output class. @@ -224,10 +214,10 @@ class Loader_test extends CI_TestCase { $dir = 'views'; $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; - $this->_create_content($dir, $file, $content); + $this->ci_vfs_create($file, $content, $this->ci_app_root, $dir); // Just like load->view(), take the output class out of the mix here. - $out = $this->load->file($this->load->app_path.$dir.'/'.$file.'.php', TRUE); + $out = $this->load->file(APPPATH.$dir.'/'.$file.'.php', TRUE); $this->assertEquals($content, $out); // Test non-existent file @@ -261,7 +251,7 @@ class Loader_test extends CI_TestCase { $helper = 'test'; $func = '_my_helper_test_func'; $content = '_create_content('helpers', $helper.'_helper', $content); + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Load helper $this->assertEquals(NULL, $this->load->helper($helper)); @@ -294,7 +284,7 @@ class Loader_test extends CI_TestCase { $funcs[] = $func; $files[$helper.'_helper'] = '_create_content('helpers', $files, NULL, NULL, TRUE); + $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers'); // Load helpers $this->assertEquals(NULL, $this->load->helpers($helpers)); @@ -321,14 +311,12 @@ class Loader_test extends CI_TestCase { */ public function test_packages() { - $this->_setup_config_mock(); - // Create third-party directory in app path with model $dir = 'third-party'; $lib = 'unit_test_package'; $class = 'CI_'.ucfirst($lib); $content = '_create_content($dir, $lib, $content); + $this->ci_vfs_create($lib, $content, $this->ci_app_root, $dir); // Test failed load without path $this->setExpectedException( @@ -342,7 +330,7 @@ class Loader_test extends CI_TestCase { $paths = $this->load->get_package_paths(TRUE); // Add path and verify - $path = $this->load->app_path.$dir; + $path = APPPATH.$dir; $this->assertNull($this->load->add_package_path($path)); $this->assertContains($path, $this->load->get_package_paths(TRUE)); @@ -362,7 +350,6 @@ class Loader_test extends CI_TestCase { */ public function test_load_config() { - $this->_setup_config_mock(); $this->assertNull($this->load->config('config', FALSE)); } @@ -373,35 +360,29 @@ class Loader_test extends CI_TestCase { */ public function test_autoloader() { - $this->_setup_config_mock(); - // Create helper directory in app path with test helper $helper = 'autohelp'; $hlp_func = '_autohelp_test_func'; - $this->_create_content('helpers', $helper.'_helper', 'ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Create libraries directory in base path with test library $lib = 'autolib'; $lib_class = 'CI_'.ucfirst($lib); - $this->_create_content('libraries', $lib, 'ci_vfs_create($lib, 'ci_base_root, 'libraries'); - // Create libraries subdirectory with test driver - // Since libraries/ now exists, we have to look it up and - // add the subdir directly instead of using _create_content + // Create test driver $drv = 'autodrv'; $subdir = ucfirst($drv); $drv_class = 'CI_'.$subdir; - $tree = array( - $subdir => array($drv.'.php' => 'load->base_root->getChild('libraries')); + $this->ci_vfs_create($drv, 'ci_base_root, 'libraries/'.$subdir); // Create package directory in app path with model $dir = 'testdir'; - $path = $this->load->app_path.$dir.'/'; + $path = APPPATH.$dir.'/'; $model = 'automod'; $mod_class = ucfirst($model); - $this->_create_content($dir, $model, 'ci_vfs_create($model, 'ci_app_root, $dir.'/models'); // Create autoloader config $cfg = array( @@ -412,7 +393,7 @@ class Loader_test extends CI_TestCase { 'model' => array($model), 'config' => array() ); - $this->_create_content('config', 'autoload', 'ci_vfs_create('autoload', 'ci_app_root, 'config'); // Run autoloader $this->load->autoload(); @@ -436,55 +417,4 @@ class Loader_test extends CI_TestCase { $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); } - // -------------------------------------------------------------------- - - private function _setup_config_mock() - { - // Mock up a config object so config() has something to call - $config = $this->getMock('CI_Config', array('load'), array(), '', FALSE); - $config->expects($this->any()) - ->method('load') - ->will($this->returnValue(TRUE)); - - // Reinitialize config paths to use VFS - $config->_config_paths = array($this->load->app_path); - - // Add the mock to our stdClass - $this->ci_instance_var('config', $config); - } - - // -------------------------------------------------------------------- - - private function _create_content($dir, $file, $content, $sub = NULL, $base = FALSE) - { - // Create structure containing directory - $tree = array($dir => array()); - - // Check for subdirectory - if ($sub) { - // Add subdirectory to tree and get reference - $tree[$dir][$sub] = array(); - $leaf =& $tree[$dir][$sub]; - } - else { - // Get reference to main directory - $leaf =& $tree[$dir]; - } - - // Check for multiple files - if (is_array($file)) { - // Add multiple files to directory - foreach ($file as $name => $data) { - $leaf[$name.'.php'] = $data; - } - } - else { - // Add single file with content - $leaf[$file.'.php'] = $content; - } - - // Create structure under app or base path - vfsStream::create($tree, $base ? $this->load->base_root : $this->load->app_root); - } - -} +} \ No newline at end of file diff --git a/tests/codeigniter/helpers/date_helper_test.php b/tests/codeigniter/helpers/date_helper_test.php index 9feade71c..1458acd3e 100644 --- a/tests/codeigniter/helpers/date_helper_test.php +++ b/tests/codeigniter/helpers/date_helper_test.php @@ -168,6 +168,8 @@ class Date_helper_test extends CI_TestCase { public function test_timespan() { + $this->ci_vfs_clone('system/language/english/date_lang.php'); + $loader_cls = $this->ci_core_class('load'); $this->ci_instance_var('load', new $loader_cls); diff --git a/tests/codeigniter/helpers/form_helper_test.php b/tests/codeigniter/helpers/form_helper_test.php index 1a30ed993..48628d2e5 100644 --- a/tests/codeigniter/helpers/form_helper_test.php +++ b/tests/codeigniter/helpers/form_helper_test.php @@ -1,10 +1,12 @@ helper('form'); + } + public function test_form_hidden() { $expected = <<getMock($lang_cls, array('load')); $lang->expects($this->once()) ->method('load') ->with($this->equalTo('number')); // Assign the proper language array - - $lang->language = $this->_get_lang('number'); + $lang->language = $this->lang('number'); // We don't have a controller, so just create // a cheap class to act as our super object. // Make sure it has a lang attribute. - - $obj = new stdClass; - $obj->lang = $lang; - $this->ci_instance($obj); - } - - // Quick helper to actually grab the language - // file. Consider moving this to ci_testcase? - public function _get_lang($name) - { - require BASEPATH.'language/english/'.$name.'_lang.php'; - return $lang; + $this->ci_instance_var('lang', $lang); } public function test_byte_format() diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php index f131469cb..d75d26208 100644 --- a/tests/codeigniter/helpers/text_helper_test.php +++ b/tests/codeigniter/helpers/text_helper_test.php @@ -64,6 +64,7 @@ class Text_helper_test extends CI_TestCase { public function test_convert_accented_characters() { + $this->ci_vfs_clone('application/config/foreign_chars.php'); $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ')); $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü')); } diff --git a/tests/codeigniter/libraries/Encrypt_test.php b/tests/codeigniter/libraries/Encrypt_test.php index 153a25e1d..998d806b3 100644 --- a/tests/codeigniter/libraries/Encrypt_test.php +++ b/tests/codeigniter/libraries/Encrypt_test.php @@ -4,11 +4,8 @@ class Encrypt_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->encrypt = new Mock_Libraries_Encrypt(); - - $this->ci_instance($obj); - $this->encrypt = $obj->encrypt; + $this->encrypt = new Mock_Libraries_Encrypt(); + $this->ci_instance_var('encrypt', $this->encrypt); $this->ci_set_config('encryption_key', "Encryptin'glike@boss!"); $this->msg = 'My secret message'; diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php index b68f44a33..394c22692 100644 --- a/tests/codeigniter/libraries/Parser_test.php +++ b/tests/codeigniter/libraries/Parser_test.php @@ -4,12 +4,8 @@ class Parser_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->parser = new Mock_Libraries_Parser(); - - $this->ci_instance($obj); - - $this->parser = $obj->parser; + $this->parser = new Mock_Libraries_Parser(); + $this->ci_instance_var('parser', $this->parser); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/libraries/Session_test.php b/tests/codeigniter/libraries/Session_test.php index 60d3a5b30..14469f7fa 100644 --- a/tests/codeigniter/libraries/Session_test.php +++ b/tests/codeigniter/libraries/Session_test.php @@ -29,13 +29,15 @@ class Session_test extends CI_TestCase { $_COOKIE = array(); // Establish necessary support classes - $obj = new stdClass; $cfg = $this->ci_core_class('cfg'); - $obj->config = new $cfg(); $ldr = $this->ci_core_class('load'); - $obj->load = new $ldr(); - $obj->input = new Mock_Core_Input(NULL, NULL); - $this->ci_instance($obj); + $ci = $this->ci_instance(); + $ci->config = new $cfg(); + $ci->load = new $ldr(); + $ci->input = new Mock_Core_Input(NULL, NULL); + + // Make sure string helper is available + $this->ci_vfs_clone('system/helpers/string_helper.php'); // Attach session instance locally $config = array( diff --git a/tests/codeigniter/libraries/Table_test.php b/tests/codeigniter/libraries/Table_test.php index edfc83dd0..ce04b6a6d 100644 --- a/tests/codeigniter/libraries/Table_test.php +++ b/tests/codeigniter/libraries/Table_test.php @@ -4,12 +4,8 @@ class Table_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->table = new Mock_Libraries_Table(); - - $this->ci_instance($obj); - - $this->table = $obj->table; + $this->table = new Mock_Libraries_Table(); + $this->ci_instance_var('table', $this->table); } // Setter Methods diff --git a/tests/codeigniter/libraries/Typography_test.php b/tests/codeigniter/libraries/Typography_test.php index eb6dacb73..5dba06243 100644 --- a/tests/codeigniter/libraries/Typography_test.php +++ b/tests/codeigniter/libraries/Typography_test.php @@ -4,12 +4,8 @@ class Typography_test extends CI_TestCase { public function set_up() { - $obj = new stdClass; - $obj->type = new Mock_Libraries_Typography(); - - $this->ci_instance($obj); - - $this->type = $obj->type; + $this->type = new Mock_Libraries_Typography(); + $this->ci_instance('type', $this->type); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php index d79a3ffc9..827942773 100644 --- a/tests/codeigniter/libraries/Upload_test.php +++ b/tests/codeigniter/libraries/Upload_test.php @@ -4,18 +4,11 @@ class Upload_test extends CI_TestCase { function set_up() { - $obj = new stdClass; - $obj->upload = new Mock_Libraries_Upload(); - $obj->security = new Mock_Core_Security(); - $obj->lang = new Mock_Core_Lang(); - - $this->ci_instance($obj); - $this->upload = $obj->upload; - - vfsStreamWrapper::register(); - vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); - - $this->_test_dir = vfsStreamWrapper::getRoot(); + $ci = $this->ci_instance(); + $ci->upload = new Mock_Libraries_Upload(); + $ci->security = new Mock_Core_Security(); + $ci->lang = new Mock_Core_Lang(); + $this->upload = $ci->upload; } function test_do_upload() @@ -64,11 +57,15 @@ class Upload_test extends CI_TestCase { function test_set_filename() { - $file1 = vfsStream::newFile('hello-world.txt')->withContent('Hello world.')->at($this->_test_dir); + $dir = 'uploads'; + $isnew = 'helloworld.txt'; + $exists = 'hello-world.txt'; + $this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir); + $path = $this->ci_vfs_path($dir.'/', APPPATH); $this->upload->file_ext = '.txt'; - $this->assertEquals('helloworld.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'helloworld.txt')); - $this->assertEquals('hello-world1.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'hello-world.txt')); + $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew)); + $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists)); } function test_set_max_filesize() @@ -107,7 +104,7 @@ class Upload_test extends CI_TestCase { function test_set_image_properties() { $this->upload->file_type = 'image/gif'; - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $props = array( 'image_width' => 170, @@ -156,7 +153,7 @@ class Upload_test extends CI_TestCase { $this->assertTrue($this->upload->is_allowed_filetype(FALSE)); $this->assertTrue($this->upload->is_allowed_filetype(TRUE)); - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $this->upload->file_ext = '.gif'; $this->upload->file_type = 'image/gif'; $this->assertTrue($this->upload->is_allowed_filetype()); @@ -179,7 +176,7 @@ class Upload_test extends CI_TestCase { $this->assertTrue($this->upload->is_allowed_dimensions()); $this->upload->file_type = 'image/gif'; - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $this->upload->max_width = 10; $this->assertFalse($this->upload->is_allowed_dimensions()); @@ -197,7 +194,9 @@ class Upload_test extends CI_TestCase { $this->upload->upload_path = ''; $this->assertFalse($this->upload->validate_upload_path()); - $this->upload->upload_path = vfsStream::url('testDir'); + $dir = 'uploads'; + $this->ci_vfs_mkdir($dir); + $this->upload->upload_path = $this->ci_vfs_path($dir); $this->assertTrue($this->upload->validate_upload_path()); } @@ -222,20 +221,24 @@ class Upload_test extends CI_TestCase { function test_do_xss_clean() { - $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir); - $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir); - $file3 = vfsStream::newFile('file3.txt')->withContent('')->at($this->_test_dir); - - $this->upload->file_temp = vfsStream::url('file1.txt'); + $dir = 'uploads'; + $file1 = 'file1.txt'; + $file2 = 'file2.txt'; + $file3 = 'file3.txt'; + $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir); + $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir); + $this->ci_vfs_create($file3, '', $this->ci_vfs_root, $dir); + + $this->upload->file_temp = $this->ci_vfs_path($file1, $dir); $this->assertTrue($this->upload->do_xss_clean()); - $this->upload->file_temp = vfsStream::url('file2.txt'); + $this->upload->file_temp = $this->ci_vfs_path($file2, $dir); $this->assertFalse($this->upload->do_xss_clean()); - $this->upload->file_temp = vfsStream::url('file3.txt'); + $this->upload->file_temp = $this->ci_vfs_path($file3, $dir); $this->assertFalse($this->upload->do_xss_clean()); - $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif'; + $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); $this->assertTrue($this->upload->do_xss_clean()); } diff --git a/tests/codeigniter/libraries/Useragent_test.php b/tests/codeigniter/libraries/Useragent_test.php index 89383f807..e3726554e 100644 --- a/tests/codeigniter/libraries/Useragent_test.php +++ b/tests/codeigniter/libraries/Useragent_test.php @@ -10,12 +10,11 @@ class UserAgent_test extends CI_TestCase { // set a baseline user agent $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; - $obj = new stdClass; - $obj->agent = new Mock_Libraries_UserAgent(); + $this->ci_vfs_clone('application/config/user_agents.php'); - $this->ci_instance($obj); + $this->agent = new Mock_Libraries_UserAgent(); - $this->agent = $obj->agent; + $this->ci_instance_var('agent', $this->agent); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From e3621cc79fa4b4658768fea0694cc0ae52835d85 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 14 Oct 2012 18:23:52 -0400 Subject: Adapted Config load test to VFS APPPATH Signed-off-by: dchill42 --- tests/codeigniter/core/Config_test.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 7782a7898..80e0862ff 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -94,7 +94,7 @@ class Config_test extends CI_TestCase { public function test_load() { - // Create VFS tree of application config files + // Create config files in VFS $file1 = 'test.php'; $file2 = 'secttest'; $key1 = 'testconfig'; @@ -107,18 +107,10 @@ class Config_test extends CI_TestCase { 'two' => 2, 'three' => true ); - $tree = array( - 'application' => array( - 'config' => array( - $file1 => ' 'config->_config_paths = array(vfsStream::url('application').'/'); + $this->ci_vfs_create(array( + $file1 => ' 'ci_app_root, 'config'); // Test regular load $this->assertTrue($this->config->load($file1)); @@ -140,4 +132,4 @@ class Config_test extends CI_TestCase { $this->assertNull($this->config->load($file3)); } -} +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 3a43f130a02be38b7244ffe4507c6cefd874f285 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sat, 20 Oct 2012 22:44:16 -0400 Subject: Raised CI_Config test coverage to 100% Signed-off-by: dchill42 --- tests/codeigniter/core/Config_test.php | 197 ++++++++++++++++++++++++++------- 1 file changed, 158 insertions(+), 39 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 80e0862ff..d652a625e 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -7,11 +7,12 @@ class Config_test extends CI_TestCase { $cls =& $this->ci_core_class('cfg'); // set predictable config values - $this->ci_set_config(array( + $this->cfg = array( 'index_page' => 'index.php', 'base_url' => 'http://example.com/', 'subclass_prefix' => 'MY_' - )); + ); + $this->ci_set_config($this->cfg); $this->config = new $cls; } @@ -20,7 +21,7 @@ class Config_test extends CI_TestCase { public function test_item() { - $this->assertEquals('http://example.com/', $this->config->item('base_url')); + $this->assertEquals($this->cfg['base_url'], $this->config->item('base_url')); // Bad Config value $this->assertFalse($this->config->item('no_good_item')); @@ -48,36 +49,103 @@ class Config_test extends CI_TestCase { // Bad Config value $this->assertFalse($this->config->slash_item('no_good_item')); - $this->assertEquals('http://example.com/', $this->config->slash_item('base_url')); + $this->assertEquals($this->cfg['base_url'], $this->config->slash_item('base_url')); - $this->assertEquals('MY_/', $this->config->slash_item('subclass_prefix')); + $this->assertEquals($this->cfg['subclass_prefix'].'/', $this->config->slash_item('subclass_prefix')); } // -------------------------------------------------------------------- - public function test_site_url() + public function test_base_url() { - $this->assertEquals('http://example.com/index.php', $this->config->site_url()); + // Test regular base URL + $base_url = $this->cfg['base_url']; + $this->assertEquals($base_url, $this->config->base_url()); + + // Test with URI + $uri = 'test'; + $this->assertEquals($base_url.$uri, $this->config->base_url($uri)); + + // Clear base_url + $this->ci_set_config('base_url', ''); + + // Rerun constructor + $cls =& $this->ci_core_class('cfg'); + $this->config = new $cls; - $base_url = $this->config->item('base_url'); + // Test default base + $this->assertEquals('http://localhost/', $this->config->base_url()); + // Capture server vars + $old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL; + $old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL; + $old_https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : NULL; + + // Setup server vars for detection + $host = 'test.com'; + $path = '/path/'; + $script = 'base_test.php'; + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SCRIPT_NAME'] = $path.$script; + + // Rerun constructor + $this->config = new $cls; + + // Test plain detected + $this->assertEquals('http://'.$host.$path, $this->config->base_url()); + + // Rerun constructor + $_SERVER['HTTPS'] = 'on'; + $this->config = new $cls; + + // Test secure detected + $this->assertEquals('https://'.$host.$path, $this->config->base_url()); + + // Restore server vars + if ($old_host === NULL) unset($_SERVER['HTTP_HOST']); + else $_SERVER['HTTP_HOST'] = $old_host; + if ($old_script === NULL) unset($_SERVER['SCRIPT_NAME']); + else $_SERVER['SCRIPT_NAME'] = $old_script; + if ($old_https === NULL) unset($_SERVER['HTTPS']); + else $_SERVER['HTTPS'] = $old_https; + } + + // -------------------------------------------------------------------- + + public function test_site_url() + { + $base_url = $this->cfg['base_url']; + $index_page = $this->cfg['index_page']; + $this->assertEquals($base_url.$index_page, $this->config->site_url()); + + $old_base = $this->config->item('base_url'); $this->config->set_item('base_url', ''); $q_string = $this->config->item('enable_query_strings'); - $this->config->set_item('enable_query_strings', FALSE); - $this->assertEquals('index.php/test', $this->config->site_url('test')); - $this->assertEquals('index.php/test/1', $this->config->site_url(array('test', '1'))); + $uri= 'test'; + $uri2 = '1'; + $this->assertEquals($index_page.'/'.$uri, $this->config->site_url($uri)); + $this->assertEquals($index_page.'/'.$uri.'/'.$uri2, $this->config->site_url(array($uri, $uri2))); + + $suffix = 'ing'; + $this->config->set_item('url_suffix', $suffix); + + $arg = 'pass'; + $this->assertEquals($index_page.'/'.$uri.$suffix, $this->config->site_url($uri)); + $this->assertEquals($index_page.'/'.$uri.$suffix.'?'.$arg, $this->config->site_url($uri.'?'.$arg)); + + $this->config->set_item('url_suffix', FALSE); $this->config->set_item('enable_query_strings', TRUE); - $this->assertEquals('index.php?test', $this->config->site_url('test')); - $this->assertEquals('index.php?0=test&1=1', $this->config->site_url(array('test', '1'))); + $this->assertEquals($index_page.'?'.$uri, $this->config->site_url($uri)); + $this->assertEquals($index_page.'?0='.$uri.'&1='.$uri2, $this->config->site_url(array($uri, $uri2))); - $this->config->set_item('base_url', $base_url); + $this->config->set_item('base_url', $old_base); - $this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test')); + $this->assertEquals($base_url.$index_page.'?'.$uri, $this->config->site_url($uri)); // back to home base $this->config->set_item('enable_query_strings', $q_string); @@ -87,49 +155,100 @@ class Config_test extends CI_TestCase { public function test_system_url() { - $this->assertEquals('http://example.com/system/', $this->config->system_url()); + $this->assertEquals($this->cfg['base_url'].'system/', $this->config->system_url()); } // -------------------------------------------------------------------- public function test_load() { - // Create config files in VFS - $file1 = 'test.php'; - $file2 = 'secttest'; - $key1 = 'testconfig'; - $val1 = 'my_value'; - $cfg1 = array( - $key1 => $val1 - ); - $cfg2 = array( + // Test regular load + $file = 'test.php'; + $key = 'testconfig'; + $val = 'my_value'; + $cfg = array($key => $val); + $this->ci_vfs_create($file, 'ci_app_root, 'config'); + $this->assertTrue($this->config->load($file)); + $this->assertEquals($val, $this->config->item($key)); + + // Test reload - value should not change + $val2 = 'new_value'; + $cfg = array($key => $val2); + $this->ci_vfs_create($file, 'ci_app_root, 'config'); + $this->assertTrue($this->config->load($file)); + $this->assertEquals($val, $this->config->item($key)); + + // Test section load + $file = 'secttest'; + $cfg = array( 'one' => 'prime', 'two' => 2, 'three' => true ); - $this->ci_vfs_create(array( - $file1 => ' 'ci_app_root, 'config'); + $this->ci_vfs_create($file.'.php', 'ci_app_root, 'config'); + $this->assertTrue($this->config->load($file, TRUE)); + $this->assertEquals($cfg, $this->config->item($file)); - // Test regular load - $this->assertTrue($this->config->load($file1)); - $this->assertEquals($val1, $this->config->item($key1)); + // Test section merge + $cfg2 = array( + 'three' => 'tres', + 'number' => 42, + 'letter' => 'Z' + ); + $pkg_dir = 'package'; + $this->ci_vfs_create($file.'.php', 'ci_app_root, + array($pkg_dir, 'config')); + array_push($this->config->_config_paths, $this->ci_vfs_path($pkg_dir.'/', APPPATH)); + $this->assertTrue($this->config->load($file, TRUE)); + $this->assertEquals(array_merge($cfg, $cfg2), $this->config->item($file)); + array_pop($this->config->_config_paths); + + // Test graceful fail of invalid file + $file = 'badfile'; + $this->ci_vfs_create($file, '', $this->ci_app_root, 'config'); + $this->assertFalse($this->config->load($file, FALSE, TRUE)); + + // Test regular fail of invalid file + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Your '.$this->ci_vfs_path('config/'.$file.'.php', APPPATH). + ' file does not appear to contain a valid configuration array.' + ); + $this->assertNull($this->config->load($file)); + } - // Test section load - $this->assertTrue($this->config->load($file2, TRUE)); - $this->assertEquals($cfg2, $this->config->item($file2)); + // -------------------------------------------------------------------- - // Test graceful fail + public function test_load_nonexistent() + { + // Test graceful fail of nonexistent file $this->assertFalse($this->config->load('not_config_file', FALSE, TRUE)); // Test regular fail - $file3 = 'absentia'; + $file = 'absentia'; $this->setExpectedException( 'RuntimeException', - 'CI Error: The configuration file '.$file3.'.php does not exist.' + 'CI Error: The configuration file '.$file.'.php does not exist.' + ); + $this->assertNull($this->config->load($file)); + } + + // -------------------------------------------------------------------- + + public function test_assign_to_config() + { + $key1 = 'test'; + $key2 = '1'; + $val1 = 'foo'; + $val2 = 'bar'; + $cfg = array( + $key1 => $val1, + $key2 => $val2 ); - $this->assertNull($this->config->load($file3)); + + $this->assertNull($this->config->_assign_to_config($cfg)); + $this->assertEquals($val1, $this->config->item($key1)); + $this->assertEquals($val2, $this->config->item($key2)); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 4f42be55dc5ca3141f3d93f7f59989d3e52bc5a3 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 21 Oct 2012 21:31:19 -0400 Subject: Raised CI_Loader test coverage to 93% Signed-off-by: dchill42 --- tests/codeigniter/core/Loader_test.php | 343 ++++++++++++++++++++++----------- 1 file changed, 226 insertions(+), 117 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index 69b2afb63..a8a2de78f 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -7,27 +7,29 @@ class Loader_test extends CI_TestCase { public function set_up() { // Instantiate a new loader - $this->load = new Mock_Core_Loader(); + $loader = $this->ci_core_class('loader'); + $this->load = new $loader(); // Get CI instance $this->ci_obj = $this->ci_instance(); // Set subclass prefix - $this->ci_set_config('subclass_prefix', 'MY_'); + $this->prefix = 'MY_'; + $this->ci_set_config('subclass_prefix', $this->prefix); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::library - */ public function test_library() { - // Create libraries directory with test library + // Create library in VFS $lib = 'unit_test_lib'; $class = 'CI_'.ucfirst($lib); $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); + // Test is_loaded fail + $this->assertFalse($this->load->is_loaded($lib)); + // Test loading as an array. $this->assertNull($this->load->library(array($lib))); $this->assertTrue(class_exists($class), $class.' does not exist'); @@ -38,16 +40,68 @@ class Loader_test extends CI_TestCase { // Test a string given to params $this->assertNull($this->load->library($lib, ' ')); + + // Create library w/o class + $lib = 'bad_test_lib'; + $this->ci_vfs_create($lib, '', $this->ci_base_root, 'libraries'); + + // Test non-existent class + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Non-existent class: '.$lib + ); + $this->assertNull($this->load->library($lib)); + } + + // -------------------------------------------------------------------- + + public function test_library_extension() + { + // Create library and extension in VFS + $name = 'ext_test_lib'; + $lib = ucfirst($name); + $class = 'CI_'.$lib; + $ext = $this->prefix.$lib; + $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); + $this->ci_vfs_create($ext, 'ci_app_root, 'libraries'); + + // Test loading with extension + $this->assertNull($this->load->library($lib)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertTrue(class_exists($ext), $ext.' does not exist'); + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); + $this->assertAttributeInstanceOf($ext, $name, $this->ci_obj); + + // Test reloading with object name + $obj = 'exttest'; + $this->assertNull($this->load->library($lib, NULL, $obj)); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + $this->assertAttributeInstanceOf($ext, $obj, $this->ci_obj); + + // Test reloading + unset($this->ci_obj->$name); + $this->assertNull($this->load->library($lib)); + $this->assertObjectNotHasAttribute($name, $this->ci_obj); + + // Create baseless library + $name = 'ext_baseless_lib'; + $lib = ucfirst($name); + $class = $this->prefix.$lib; + $this->ci_vfs_create($class, 'ci_app_root, 'libraries'); + + // Test missing base class + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->assertNull($this->load->library($lib)); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::library - */ public function test_library_config() { - // Create libraries directory with test library + // Create library in VFS $lib = 'unit_test_config_lib'; $class = 'CI_'.ucfirst($lib); $content = 'config = $params; } }'; @@ -67,16 +121,16 @@ class Loader_test extends CI_TestCase { $this->assertTrue(class_exists($class), $class.' does not exist'); $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); $this->assertEquals($cfg, $this->ci_obj->$obj->config); + + // Test is_loaded + $this->assertEquals($obj, $this->load->is_loaded($lib)); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::library - */ public function test_load_library_in_application_dir() { - // Create libraries directory in app path with test library + // Create library in VFS $lib = 'super_test_library'; $class = ucfirst($lib); $this->ci_vfs_create($lib, 'ci_app_root, 'libraries'); @@ -91,12 +145,9 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::driver - */ public function test_driver() { - // Create libraries directory with test driver + // Create driver in VFS $driver = 'unit_test_driver'; $dir = ucfirst($driver); $class = 'CI_'.$dir; @@ -122,29 +173,11 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::model - */ - public function test_non_existent_model() - { - $this->setExpectedException( - 'RuntimeException', - 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' - ); - - $this->load->model('ci_test_nonexistent_model.php'); - } - - // -------------------------------------------------------------------- - - /** - * @covers CI_Loader::model - */ public function test_models() { $this->ci_set_core_class('model', 'CI_Model'); - // Create models directory with test model + // Create model in VFS $model = 'unit_test_model'; $class = ucfirst($model); $content = 'ci_core_class('model'); + + // Create modelin VFS + $model = 'test_sub_model'; + $base = 'CI_Model'; + $class = ucfirst($model); + $subdir = 'cars'; + $this->ci_vfs_create($model, 'ci_app_root, + array('models', $subdir)); + + // Load model + $name = 'testors'; + $this->assertNull($this->load->model($subdir.'/'.$model, $name)); + + // Was the model class instantiated? + $this->assertTrue(class_exists($class)); + $this->assertObjectHasAttribute($name, $this->ci_obj); + $this->assertAttributeInstanceOf($base, $name, $this->ci_obj); + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); + + // Test name conflict + $obj = 'conflict'; + $this->ci_obj->$obj = new StdClass(); + $this->setExpectedException( + 'RuntimeException', + 'CI Error: The model name you are loading is the name of a resource that is already being used: '.$obj + ); + $this->load->model('not_real', $obj); + } + + // -------------------------------------------------------------------- + + public function test_non_existent_model() + { + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' + ); + + $this->load->model('ci_test_nonexistent_model.php'); + } + + // -------------------------------------------------------------------- + // public function testDatabase() // { - // $this->assertEquals(NULL, $this->load->database()); - // $this->assertEquals(NULL, $this->load->dbutil()); + // $this->assertNull($this->load->database()); + // $this->assertNull($this->load->dbutil()); // } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::view - */ public function test_load_view() { - $this->ci_set_core_class('output', 'CI_Output'); - - // Create views directory with test view + // Create view in VFS $view = 'unit_test_view'; - $content = 'This is my test page. '; - $this->ci_vfs_create($view, $content, $this->ci_app_root, 'views'); - - // Use the optional return parameter in this test, so the view is not - // run through the output class. - $out = $this->load->view($view, array('hello' => "World!"), TRUE); - $this->assertEquals('This is my test page. World!', $out); + $var = 'hello'; + $value = 'World!'; + $content = 'This is my test page. '; + $this->ci_vfs_create($view, $content.'ci_app_root, 'views'); + + // Test returning view + $out = $this->load->view($view, array($var => $value), TRUE); + $this->assertEquals($content.$value, $out); + + // Mock output class + $class = 'Mock_Load_Output'; + $prop = 'output'; + eval('class '.$class.' { public function append_output($out) { $this->'.$prop.' = $out; } }'); + $this->ci_instance_var('output', new $class()); + + // Test view output + $this->load->view($view, array($var => $value)); + $this->assertObjectHasAttribute($prop, $this->ci_obj->output); + $this->assertEquals($content.$value, $this->ci_obj->output->$prop); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::view - */ public function test_non_existent_view() { $this->setExpectedException( @@ -205,12 +289,9 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::file - */ public function test_file() { - // Create views directory with test file + // Create view in VFS $dir = 'views'; $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; @@ -231,49 +312,66 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - /** - * @covers CI_Loader::vars - */ public function test_vars() { - $this->assertNull($this->load->vars(array('foo' => 'bar'))); - $this->assertNull($this->load->vars('foo', 'bar')); + $key1 = 'foo'; + $val1 = 'bar'; + $key2 = 'boo'; + $val2 = 'hoo'; + $this->assertNull($this->load->vars(array($key1 => $val1))); + $this->assertNull($this->load->vars($key2, $val2)); + $this->assertEquals($val1, $this->load->get_var($key1)); + $this->assertEquals(array($key1 => $val1, $key2 => $val2), $this->load->get_vars()); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::helper - */ public function test_helper() { - // Create helper directory in app path with test helper + // Create helper in VFS $helper = 'test'; $func = '_my_helper_test_func'; $content = 'ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_base_root, 'helpers'); + + // Create helper extension + $exfunc = '_my_extension_func'; + $content = 'ci_vfs_create($this->prefix.$helper.'_helper', $content, $this->ci_app_root, 'helpers'); // Load helper - $this->assertEquals(NULL, $this->load->helper($helper)); + $this->assertNull($this->load->helper($helper)); $this->assertTrue(function_exists($func), $func.' does not exist'); + $this->assertTrue(function_exists($exfunc), $exfunc.' does not exist'); + + // Create baseless extension + $ext = 'bad_ext'; + $this->ci_vfs_create($this->prefix.$ext.'_helper', '', $this->ci_app_root, 'helpers'); - // Test non-existent helper + // Test bad extension $this->setExpectedException( 'RuntimeException', - 'CI Error: Unable to load the requested file: helpers/bad_helper.php' + 'CI Error: Unable to load the requested file: helpers/'.$ext.'_helper.php' ); + $this->load->helper($ext); + } + + // -------------------------------------------------------------------- + public function test_non_existent_helper() + { + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested file: helpers/bad_helper.php' + ); $this->load->helper('bad'); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::helper - */ public function test_loading_multiple_helpers() { - // Create helper directory in base path with test helpers + // Create helpers in VFS $helpers = array(); $funcs = array(); $files = array(); @@ -287,7 +385,7 @@ class Loader_test extends CI_TestCase { $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers'); // Load helpers - $this->assertEquals(NULL, $this->load->helpers($helpers)); + $this->assertNull($this->load->helpers($helpers)); // Verify helper existence foreach ($funcs as $func) { @@ -297,40 +395,36 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - // public function testLanguage() - // { - // $this->assertEquals(NULL, $this->load->language('test')); - // } + public function test_language() + { + // Create mock Lang class with load stub + $class = 'Mock_Load_Lang'; + $prop = '_file'; + eval('class '.$class.' { public function load($file, $lang) { $this->'.$prop.' = $file; } }'); + $this->ci_instance_var('lang', new $class()); + + // Does the right file get loaded? + $file = 'test'; + $this->assertNull($this->load->language($file)); + $this->assertObjectHasAttribute($prop, $this->ci_obj->lang); + $this->assertEquals($file, $this->ci_obj->lang->$prop); + } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::add_package_path - * @covers CI_Loader::get_package_paths - * @covers CI_Loader::remove_package_path - */ public function test_packages() { - // Create third-party directory in app path with model + // Create model in VFS package path $dir = 'third-party'; $lib = 'unit_test_package'; $class = 'CI_'.ucfirst($lib); - $content = 'ci_vfs_create($lib, $content, $this->ci_app_root, $dir); - - // Test failed load without path - $this->setExpectedException( - 'RuntimeException', - 'CI Error: Unable to load the requested class: '.$lib - ); - $this->load->library($lib); + $this->ci_vfs_create($lib, 'ci_app_root, array($dir, 'libraries')); - // Clear exception and get paths - $this->setExpectedException(NULL); + // Get paths $paths = $this->load->get_package_paths(TRUE); // Add path and verify - $path = APPPATH.$dir; + $path = APPPATH.$dir.'/'; $this->assertNull($this->load->add_package_path($path)); $this->assertContains($path, $this->load->get_package_paths(TRUE)); @@ -338,51 +432,63 @@ class Loader_test extends CI_TestCase { $this->assertNull($this->load->library($lib)); $this->assertTrue(class_exists($class), $class.' does not exist'); + // Add another path + $path2 = APPPATH.'another/'; + $this->assertNull($this->load->add_package_path($path2)); + $this->assertContains($path2, $this->load->get_package_paths(TRUE)); + + // Remove last path + $this->assertNull($this->load->remove_package_path()); + $this->assertNotContains($path2, $this->load->get_package_paths(TRUE)); + // Remove path and verify restored paths $this->assertNull($this->load->remove_package_path($path)); $this->assertEquals($paths, $this->load->get_package_paths(TRUE)); + + // Test failed load without path + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->load->library($lib); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::config - */ public function test_load_config() { - $this->assertNull($this->load->config('config', FALSE)); + $cfg = 'someconfig'; + $this->assertNull($this->load->config($cfg, FALSE)); + $this->assertContains($cfg, $this->ci_obj->config->loaded); } // -------------------------------------------------------------------- - /** - * @covers CI_Loader::_ci_autoloader - */ - public function test_autoloader() + public function test_initialize() { - // Create helper directory in app path with test helper + // Create helper in VFS $helper = 'autohelp'; $hlp_func = '_autohelp_test_func'; - $content = 'ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); - // Create libraries directory in base path with test library + // Create library in VFS $lib = 'autolib'; $lib_class = 'CI_'.ucfirst($lib); $this->ci_vfs_create($lib, 'ci_base_root, 'libraries'); - // Create test driver + // Create driver in VFS $drv = 'autodrv'; $subdir = ucfirst($drv); $drv_class = 'CI_'.$subdir; - $this->ci_vfs_create($drv, 'ci_base_root, 'libraries/'.$subdir); + $this->ci_vfs_create($drv, 'ci_base_root, array('libraries', $subdir)); - // Create package directory in app path with model + // Create model in VFS package path $dir = 'testdir'; $path = APPPATH.$dir.'/'; $model = 'automod'; $mod_class = ucfirst($model); - $this->ci_vfs_create($model, 'ci_app_root, $dir.'/models'); + $this->ci_vfs_create($model, 'ci_app_root, array($dir, 'models')); // Create autoloader config $cfg = array( @@ -391,12 +497,12 @@ class Loader_test extends CI_TestCase { 'libraries' => array($lib), 'drivers' => array($drv), 'model' => array($model), - 'config' => array() + 'config' => array('config1', 'config2') ); $this->ci_vfs_create('autoload', 'ci_app_root, 'config'); - // Run autoloader - $this->load->autoload(); + // Run initialize and autoloader + $this->load->initialize(); // Verify path $this->assertContains($path, $this->load->get_package_paths()); @@ -415,6 +521,9 @@ class Loader_test extends CI_TestCase { // Verify model $this->assertTrue(class_exists($mod_class), $mod_class.' does not exist'); $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); + + // Verify config calls + $this->assertEquals($cfg['config'], $this->ci_obj->config->loaded); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 8889db7e1b1768ecfb76e9e73598541042a9edc1 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Mon, 22 Oct 2012 23:16:26 -0400 Subject: Replaced evals with getMock usage in Loader tests Signed-off-by: dchill42 --- tests/codeigniter/core/Loader_test.php | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index a8a2de78f..f7c338a20 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -264,15 +264,12 @@ class Loader_test extends CI_TestCase { $this->assertEquals($content.$value, $out); // Mock output class - $class = 'Mock_Load_Output'; - $prop = 'output'; - eval('class '.$class.' { public function append_output($out) { $this->'.$prop.' = $out; } }'); - $this->ci_instance_var('output', new $class()); + $output = $this->getMock('CI_Output', array('append_output')); + $output->expects($this->once())->method('append_output')->with($content.$value); + $this->ci_instance_var('output', $output); // Test view output - $this->load->view($view, array($var => $value)); - $this->assertObjectHasAttribute($prop, $this->ci_obj->output); - $this->assertEquals($content.$value, $this->ci_obj->output->$prop); + $this->assertNull($this->load->view($view, array($var => $value))); } // -------------------------------------------------------------------- @@ -397,17 +394,12 @@ class Loader_test extends CI_TestCase { public function test_language() { - // Create mock Lang class with load stub - $class = 'Mock_Load_Lang'; - $prop = '_file'; - eval('class '.$class.' { public function load($file, $lang) { $this->'.$prop.' = $file; } }'); - $this->ci_instance_var('lang', new $class()); - - // Does the right file get loaded? + // Mock lang class and test load call $file = 'test'; + $lang = $this->getMock('CI_Lang', array('load')); + $lang->expects($this->once())->method('load')->with($file); + $this->ci_instance_var('lang', $lang); $this->assertNull($this->load->language($file)); - $this->assertObjectHasAttribute($prop, $this->ci_obj->lang); - $this->assertEquals($file, $this->ci_obj->lang->$prop); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b