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