Skip to content
This repository
Browse code

Merge pull request #1744 from dchill42/load_config_units

Loader and Config Unit Test Improvements
  • Loading branch information...
commit bf93f9397fbae4535fba661f561bf545c903ca8a 2 parents f5f898f + 8889db7
Andrey Andreev narfbg authored

Showing 23 changed files with 999 additions and 234 deletions. Show diff stats Hide diff stats

  1. +10 4 tests/Bootstrap.php
  2. +72 5 tests/README.md
  3. +177 16 tests/codeigniter/core/Config_test.php
  4. +2 0  tests/codeigniter/core/Lang_test.php
  5. +381 81 tests/codeigniter/core/Loader_test.php
  6. +2 0  tests/codeigniter/helpers/date_helper_test.php
  7. +5 3 tests/codeigniter/helpers/form_helper_test.php
  8. +2 15 tests/codeigniter/helpers/number_helper_test.php
  9. +1 0  tests/codeigniter/helpers/text_helper_test.php
  10. +2 5 tests/codeigniter/libraries/Encrypt_test.php
  11. +2 6 tests/codeigniter/libraries/Parser_test.php
  12. +7 5 tests/codeigniter/libraries/Session_test.php
  13. +2 6 tests/codeigniter/libraries/Table_test.php
  14. +2 6 tests/codeigniter/libraries/Typography_test.php
  15. +30 27 tests/codeigniter/libraries/Upload_test.php
  16. +3 4 tests/codeigniter/libraries/Useragent_test.php
  17. +13 7 tests/mocks/autoloader.php
  18. +195 8 tests/mocks/ci_testcase.php
  19. +20 0 tests/mocks/ci_testconfig.php
  20. +27 2 tests/mocks/core/common.php
  21. +0 31 tests/mocks/core/loader.php
  22. +43 2 tests/mocks/database/db.php
  23. +1 1  tests/mocks/database/schema/skeleton.php
14 tests/Bootstrap.php
@@ -8,10 +8,7 @@
8 8
9 9 // Path constants
10 10 defined('PROJECT_BASE') OR define('PROJECT_BASE', realpath($dir.'/../').'/');
11   -defined('BASEPATH') OR define('BASEPATH', PROJECT_BASE.'system/');
12   -defined('APPPATH') OR define('APPPATH', PROJECT_BASE.'application/');
13   -defined('VIEWPATH') OR define('VIEWPATH', PROJECT_BASE.'');
14   -isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  11 +defined('SYSTEM_PATH') OR define('SYSTEM_PATH', PROJECT_BASE.'system/');
15 12
16 13 // Get vfsStream either via PEAR or composer
17 14 foreach (explode(PATH_SEPARATOR, get_include_path()) as $path)
@@ -31,8 +28,17 @@ class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory');
31 28 class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper');
32 29 }
33 30
  31 +// Define CI path constants to VFS (filesystem setup in CI_TestCase::setUp)
  32 +defined('BASEPATH') OR define('BASEPATH', vfsStream::url('system/'));
  33 +defined('APPPATH') OR define('APPPATH', vfsStream::url('application/'));
  34 +defined('VIEWPATH') OR define('VIEWPATH', APPPATH.'views/');
  35 +
  36 +// Set localhost "remote" IP
  37 +isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  38 +
34 39 // Prep our test environment
35 40 include_once $dir.'/mocks/core/common.php';
  41 +include_once SYSTEM_PATH.'core/Common.php';
36 42 include_once $dir.'/mocks/autoloader.php';
37 43 spl_autoload_register('autoload');
38 44
77 tests/README.md
Source Rendered
@@ -64,6 +64,30 @@ with a base for application and package tests. That gives us:
64 64 3. Application Test - bootstrapping for application/tests [not started]
65 65 4. Package Test - bootstrapping for <package>/tests [not started]
66 66
  67 +### Test Environment:
  68 +
  69 +The test/Bootstrap.php file establishes global constants such as BASEPATH,
  70 +APPPATH, and VIEWPATH, initializing them to point to VFS locations. The
  71 +test case class employs vfsStream to make a clean virtual filesystem with
  72 +the necessary paths for every individual test.
  73 +
  74 +Within each test case, VFS directory objects are available to use as arguments
  75 +to the VFS convenience functions (see below):
  76 +
  77 +- ci_vfs_root: VFS filesystem root
  78 +- ci_app_root: Application directory
  79 +- ci_base_root: System directory
  80 +- ci_view_root: Views directory
  81 +
  82 +Classes being instantiated for testing are read from the actual filesystem
  83 +by the unit test autoloader, as are mockups created in tests/mocks. If you
  84 +need access to the real system directory, the SYSTEM_PATH constant always
  85 +points to it.
  86 +
  87 +Any other resources which need to be read from the path constants must be
  88 +created or cloned within your test. Functions for doing so are outlined
  89 +below.
  90 +
67 91 ### CI_TestCase Documentation
68 92
69 93 Test cases should extend CI_TestCase. This internally extends
@@ -78,8 +102,14 @@ Current API is *not stable*. Names and implementations will change.
78 102
79 103 $this->ci_set_config($key, $val)
80 104
81   -Set the global config variables. If key is an array, it will
82   -replace the entire config array. They are _not_ merged.
  105 +Set the global config variables in a mock Config object. If key is an array,
  106 +it will replace the entire config array. They are _not_ merged. If called
  107 +without any parameters, it will create the mock object but not set any values.
  108 +The mock Config object also provides rudimentary item() and load() stubs for
  109 +delivering configured values to classes being tested and handling config load
  110 +calls, respectively. The load() stub does _not_ actually load any files, it
  111 +only records the filename provided. Check the config->loaded array to verify
  112 +calls made.
83 113
84 114 $this->ci_instance($obj)
85 115
@@ -103,11 +133,48 @@ $GLOBALS key. For example:
103 133 $cfg = new $cfg; // instantiates config and overwrites the CFG global
104 134
105 135 $this->ci_set_core_class($name, $obj)
106   -
  136 +
107 137 An alternative way to set one of the core globals.
108 138
  139 + $this->ci_vfs_mkdir($name, $root)
  140 +
  141 +Creates a new directory in the test VFS. Pass a directory object to be the
  142 +parent directory or none to create a root-level directory. Returns the new
  143 +directory object.
  144 +
  145 + $this->ci_vfs_create($file, $content, $root, $path)
  146 +
  147 +Creates a new VFS file. '.php' is automatically appended to the filename if
  148 +it has no extension. Pass a directory object as the root, and an optional path
  149 +to recurse and/or create for containing the file. Path may be a string (such
  150 +as 'models/subdir') or an array (e.g. - array('models', 'subdir') ). Existing
  151 +directories in the VFS root will be recursed until a new directory is
  152 +identified - all others in the path will be created, so you can mix-and-match
  153 +old and new directories. If $file is an array (key = name, value = content),
  154 +multiple files will be created in the same path.
  155 +
  156 + $this->ci_vfs_clone($path)
  157 +
  158 +Clones an existing file from the real filesystem to exist in the same path of
  159 +the VFS. Path must be relative to the project root (i.e. - starting with
  160 +'system' or 'application').
  161 +
  162 + $this->ci_vfs_path($path, $base)
  163 +
  164 +Creates a VFS file path string suitable for use with PHP file operations. Path
  165 +may be absolute from the VFS root, or relative to a base path. It is often
  166 +useful to use APPPATH or BASEPATH as the base.
  167 +
  168 + $this->helper($name)
  169 +
  170 +Loads a helper from the real filesystem.
  171 +
  172 + $this->lang($name)
  173 +
  174 +Loads a language file from the real filesystem and returns the $lang array.
  175 +
109 176 $this->ci_get_config() __internal__
110   -
  177 +
111 178 Returns the global config array. Internal as you shouldn't need to
112 179 call this (you're setting it, after all). Used internally to make
113 180 CI's get_config() work.
@@ -155,4 +222,4 @@ I don't have a clue how this will work.
155 222
156 223 Needs to be able to handle packages
157 224 that are used multiple times within the application (i.e. EE/Pyro modules)
158   -as well as packages that are used by multiple applications (library distributions)
  225 +as well as packages that are used by multiple applications (library distributions)
193 tests/codeigniter/core/Config_test.php
@@ -7,11 +7,12 @@ public function set_up()
7 7 $cls =& $this->ci_core_class('cfg');
8 8
9 9 // set predictable config values
10   - $this->ci_set_config(array(
  10 + $this->cfg = array(
11 11 'index_page' => 'index.php',
12 12 'base_url' => 'http://example.com/',
13 13 'subclass_prefix' => 'MY_'
14   - ));
  14 + );
  15 + $this->ci_set_config($this->cfg);
15 16
16 17 $this->config = new $cls;
17 18 }
@@ -20,7 +21,7 @@ public function set_up()
20 21
21 22 public function test_item()
22 23 {
23   - $this->assertEquals('http://example.com/', $this->config->item('base_url'));
  24 + $this->assertEquals($this->cfg['base_url'], $this->config->item('base_url'));
24 25
25 26 // Bad Config value
26 27 $this->assertFalse($this->config->item('no_good_item'));
@@ -48,36 +49,103 @@ public function test_slash_item()
48 49 // Bad Config value
49 50 $this->assertFalse($this->config->slash_item('no_good_item'));
50 51
51   - $this->assertEquals('http://example.com/', $this->config->slash_item('base_url'));
  52 + $this->assertEquals($this->cfg['base_url'], $this->config->slash_item('base_url'));
52 53
53   - $this->assertEquals('MY_/', $this->config->slash_item('subclass_prefix'));
  54 + $this->assertEquals($this->cfg['subclass_prefix'].'/', $this->config->slash_item('subclass_prefix'));
54 55 }
55 56
56 57 // --------------------------------------------------------------------
57 58
58   - public function test_site_url()
  59 + public function test_base_url()
59 60 {
60   - $this->assertEquals('http://example.com/index.php', $this->config->site_url());
  61 + // Test regular base URL
  62 + $base_url = $this->cfg['base_url'];
  63 + $this->assertEquals($base_url, $this->config->base_url());
  64 +
  65 + // Test with URI
  66 + $uri = 'test';
  67 + $this->assertEquals($base_url.$uri, $this->config->base_url($uri));
  68 +
  69 + // Clear base_url
  70 + $this->ci_set_config('base_url', '');
  71 +
  72 + // Rerun constructor
  73 + $cls =& $this->ci_core_class('cfg');
  74 + $this->config = new $cls;
  75 +
  76 + // Test default base
  77 + $this->assertEquals('http://localhost/', $this->config->base_url());
  78 +
  79 + // Capture server vars
  80 + $old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL;
  81 + $old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL;
  82 + $old_https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : NULL;
  83 +
  84 + // Setup server vars for detection
  85 + $host = 'test.com';
  86 + $path = '/path/';
  87 + $script = 'base_test.php';
  88 + $_SERVER['HTTP_HOST'] = $host;
  89 + $_SERVER['SCRIPT_NAME'] = $path.$script;
  90 +
  91 + // Rerun constructor
  92 + $this->config = new $cls;
  93 +
  94 + // Test plain detected
  95 + $this->assertEquals('http://'.$host.$path, $this->config->base_url());
  96 +
  97 + // Rerun constructor
  98 + $_SERVER['HTTPS'] = 'on';
  99 + $this->config = new $cls;
  100 +
  101 + // Test secure detected
  102 + $this->assertEquals('https://'.$host.$path, $this->config->base_url());
61 103
62   - $base_url = $this->config->item('base_url');
  104 + // Restore server vars
  105 + if ($old_host === NULL) unset($_SERVER['HTTP_HOST']);
  106 + else $_SERVER['HTTP_HOST'] = $old_host;
  107 + if ($old_script === NULL) unset($_SERVER['SCRIPT_NAME']);
  108 + else $_SERVER['SCRIPT_NAME'] = $old_script;
  109 + if ($old_https === NULL) unset($_SERVER['HTTPS']);
  110 + else $_SERVER['HTTPS'] = $old_https;
  111 + }
  112 +
  113 + // --------------------------------------------------------------------
  114 +
  115 + public function test_site_url()
  116 + {
  117 + $base_url = $this->cfg['base_url'];
  118 + $index_page = $this->cfg['index_page'];
  119 + $this->assertEquals($base_url.$index_page, $this->config->site_url());
63 120
  121 + $old_base = $this->config->item('base_url');
64 122 $this->config->set_item('base_url', '');
65 123
66 124 $q_string = $this->config->item('enable_query_strings');
67   -
68 125 $this->config->set_item('enable_query_strings', FALSE);
69 126
70   - $this->assertEquals('index.php/test', $this->config->site_url('test'));
71   - $this->assertEquals('index.php/test/1', $this->config->site_url(array('test', '1')));
  127 + $uri= 'test';
  128 + $uri2 = '1';
  129 + $this->assertEquals($index_page.'/'.$uri, $this->config->site_url($uri));
  130 + $this->assertEquals($index_page.'/'.$uri.'/'.$uri2, $this->config->site_url(array($uri, $uri2)));
  131 +
  132 + $suffix = 'ing';
  133 + $this->config->set_item('url_suffix', $suffix);
  134 +
  135 + $arg = 'pass';
  136 + $this->assertEquals($index_page.'/'.$uri.$suffix, $this->config->site_url($uri));
  137 + $this->assertEquals($index_page.'/'.$uri.$suffix.'?'.$arg, $this->config->site_url($uri.'?'.$arg));
  138 +
  139 + $this->config->set_item('url_suffix', FALSE);
72 140
73 141 $this->config->set_item('enable_query_strings', TRUE);
74 142
75   - $this->assertEquals('index.php?test', $this->config->site_url('test'));
76   - $this->assertEquals('index.php?0=test&1=1', $this->config->site_url(array('test', '1')));
  143 + $this->assertEquals($index_page.'?'.$uri, $this->config->site_url($uri));
  144 + $this->assertEquals($index_page.'?0='.$uri.'&1='.$uri2, $this->config->site_url(array($uri, $uri2)));
77 145
78   - $this->config->set_item('base_url', $base_url);
  146 + $this->config->set_item('base_url', $old_base);
79 147
80   - $this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test'));
  148 + $this->assertEquals($base_url.$index_page.'?'.$uri, $this->config->site_url($uri));
81 149
82 150 // back to home base
83 151 $this->config->set_item('enable_query_strings', $q_string);
@@ -87,7 +155,100 @@ public function test_site_url()
87 155
88 156 public function test_system_url()
89 157 {
90   - $this->assertEquals('http://example.com/system/', $this->config->system_url());
  158 + $this->assertEquals($this->cfg['base_url'].'system/', $this->config->system_url());
  159 + }
  160 +
  161 + // --------------------------------------------------------------------
  162 +
  163 + public function test_load()
  164 + {
  165 + // Test regular load
  166 + $file = 'test.php';
  167 + $key = 'testconfig';
  168 + $val = 'my_value';
  169 + $cfg = array($key => $val);
  170 + $this->ci_vfs_create($file, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
  171 + $this->assertTrue($this->config->load($file));
  172 + $this->assertEquals($val, $this->config->item($key));
  173 +
  174 + // Test reload - value should not change
  175 + $val2 = 'new_value';
  176 + $cfg = array($key => $val2);
  177 + $this->ci_vfs_create($file, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
  178 + $this->assertTrue($this->config->load($file));
  179 + $this->assertEquals($val, $this->config->item($key));
  180 +
  181 + // Test section load
  182 + $file = 'secttest';
  183 + $cfg = array(
  184 + 'one' => 'prime',
  185 + 'two' => 2,
  186 + 'three' => true
  187 + );
  188 + $this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
  189 + $this->assertTrue($this->config->load($file, TRUE));
  190 + $this->assertEquals($cfg, $this->config->item($file));
  191 +
  192 + // Test section merge
  193 + $cfg2 = array(
  194 + 'three' => 'tres',
  195 + 'number' => 42,
  196 + 'letter' => 'Z'
  197 + );
  198 + $pkg_dir = 'package';
  199 + $this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg2, TRUE).';', $this->ci_app_root,
  200 + array($pkg_dir, 'config'));
  201 + array_push($this->config->_config_paths, $this->ci_vfs_path($pkg_dir.'/', APPPATH));
  202 + $this->assertTrue($this->config->load($file, TRUE));
  203 + $this->assertEquals(array_merge($cfg, $cfg2), $this->config->item($file));
  204 + array_pop($this->config->_config_paths);
  205 +
  206 + // Test graceful fail of invalid file
  207 + $file = 'badfile';
  208 + $this->ci_vfs_create($file, '', $this->ci_app_root, 'config');
  209 + $this->assertFalse($this->config->load($file, FALSE, TRUE));
  210 +
  211 + // Test regular fail of invalid file
  212 + $this->setExpectedException(
  213 + 'RuntimeException',
  214 + 'CI Error: Your '.$this->ci_vfs_path('config/'.$file.'.php', APPPATH).
  215 + ' file does not appear to contain a valid configuration array.'
  216 + );
  217 + $this->assertNull($this->config->load($file));
  218 + }
  219 +
  220 + // --------------------------------------------------------------------
  221 +
  222 + public function test_load_nonexistent()
  223 + {
  224 + // Test graceful fail of nonexistent file
  225 + $this->assertFalse($this->config->load('not_config_file', FALSE, TRUE));
  226 +
  227 + // Test regular fail
  228 + $file = 'absentia';
  229 + $this->setExpectedException(
  230 + 'RuntimeException',
  231 + 'CI Error: The configuration file '.$file.'.php does not exist.'
  232 + );
  233 + $this->assertNull($this->config->load($file));
  234 + }
  235 +
  236 + // --------------------------------------------------------------------
  237 +
  238 + public function test_assign_to_config()
  239 + {
  240 + $key1 = 'test';
  241 + $key2 = '1';
  242 + $val1 = 'foo';
  243 + $val2 = 'bar';
  244 + $cfg = array(
  245 + $key1 => $val1,
  246 + $key2 => $val2
  247 + );
  248 +
  249 + $this->assertNull($this->config->_assign_to_config($cfg));
  250 + $this->assertEquals($val1, $this->config->item($key1));
  251 + $this->assertEquals($val2, $this->config->item($key2));
91 252 }
92 253
93 254 }
2  tests/codeigniter/core/Lang_test.php
@@ -17,6 +17,7 @@ public function set_up()
17 17
18 18 public function test_load()
19 19 {
  20 + $this->ci_vfs_clone('system/language/english/profiler_lang.php');
20 21 $this->assertTrue($this->lang->load('profiler', 'english'));
21 22 $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string'));
22 23 }
@@ -25,6 +26,7 @@ public function test_load()
25 26
26 27 public function test_load_with_unspecified_language()
27 28 {
  29 + $this->ci_vfs_clone('system/language/english/profiler_lang.php');
28 30 $this->assertTrue($this->lang->load('profiler'));
29 31 $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string'));
30 32 }
462 tests/codeigniter/core/Loader_test.php
@@ -7,92 +7,187 @@ class Loader_test extends CI_TestCase {
7 7 public function set_up()
8 8 {
9 9 // Instantiate a new loader
10   - $this->load = new Mock_Core_Loader();
  10 + $loader = $this->ci_core_class('loader');
  11 + $this->load = new $loader();
11 12
12   - // mock up a ci instance
13   - $this->ci_obj = new stdClass;
  13 + // Get CI instance
  14 + $this->ci_obj = $this->ci_instance();
14 15
15   - // Fix get_instance()
16   - $this->ci_instance($this->ci_obj);
  16 + // Set subclass prefix
  17 + $this->prefix = 'MY_';
  18 + $this->ci_set_config('subclass_prefix', $this->prefix);
17 19 }
18 20
19 21 // --------------------------------------------------------------------
20 22
21 23 public function test_library()
22 24 {
23   - $this->_setup_config_mock();
  25 + // Create library in VFS
  26 + $lib = 'unit_test_lib';
  27 + $class = 'CI_'.ucfirst($lib);
  28 + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries');
  29 +
  30 + // Test is_loaded fail
  31 + $this->assertFalse($this->load->is_loaded($lib));
24 32
25 33 // Test loading as an array.
26   - $this->assertNull($this->load->library(array('table')));
27   - $this->assertTrue(class_exists('CI_Table'), 'Table class exists');
28   - $this->assertAttributeInstanceOf('CI_Table', 'table', $this->ci_obj);
  34 + $this->assertNull($this->load->library(array($lib)));
  35 + $this->assertTrue(class_exists($class), $class.' does not exist');
  36 + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj);
29 37
30 38 // Test no lib given
31   - $this->assertEquals(FALSE, $this->load->library());
  39 + $this->assertFalse($this->load->library());
32 40
33 41 // Test a string given to params
34   - $this->assertEquals(NULL, $this->load->library('table', ' '));
  42 + $this->assertNull($this->load->library($lib, ' '));
  43 +
  44 + // Create library w/o class
  45 + $lib = 'bad_test_lib';
  46 + $this->ci_vfs_create($lib, '', $this->ci_base_root, 'libraries');
  47 +
  48 + // Test non-existent class
  49 + $this->setExpectedException(
  50 + 'RuntimeException',
  51 + 'CI Error: Non-existent class: '.$lib
  52 + );
  53 + $this->assertNull($this->load->library($lib));
35 54 }
36 55
37 56 // --------------------------------------------------------------------
38 57
39   - public function test_load_library_in_application_dir()
  58 + public function test_library_extension()
40 59 {
41   - $this->_setup_config_mock();
  60 + // Create library and extension in VFS
  61 + $name = 'ext_test_lib';
  62 + $lib = ucfirst($name);
  63 + $class = 'CI_'.$lib;
  64 + $ext = $this->prefix.$lib;
  65 + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries');
  66 + $this->ci_vfs_create($ext, '<?php class '.$ext.' extends '.$class.' { }', $this->ci_app_root, 'libraries');
  67 +
  68 + // Test loading with extension
  69 + $this->assertNull($this->load->library($lib));
  70 + $this->assertTrue(class_exists($class), $class.' does not exist');
  71 + $this->assertTrue(class_exists($ext), $ext.' does not exist');
  72 + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
  73 + $this->assertAttributeInstanceOf($ext, $name, $this->ci_obj);
  74 +
  75 + // Test reloading with object name
  76 + $obj = 'exttest';
  77 + $this->assertNull($this->load->library($lib, NULL, $obj));
  78 + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj);
  79 + $this->assertAttributeInstanceOf($ext, $obj, $this->ci_obj);
  80 +
  81 + // Test reloading
  82 + unset($this->ci_obj->$name);
  83 + $this->assertNull($this->load->library($lib));
  84 + $this->assertObjectNotHasAttribute($name, $this->ci_obj);
  85 +
  86 + // Create baseless library
  87 + $name = 'ext_baseless_lib';
  88 + $lib = ucfirst($name);
  89 + $class = $this->prefix.$lib;
  90 + $this->ci_vfs_create($class, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries');
  91 +
  92 + // Test missing base class
  93 + $this->setExpectedException(
  94 + 'RuntimeException',
  95 + 'CI Error: Unable to load the requested class: '.$lib
  96 + );
  97 + $this->assertNull($this->load->library($lib));
  98 + }
42 99
43   - $content = '<?php class Super_test_library {} ';
  100 + // --------------------------------------------------------------------
44 101
45   - $model = vfsStream::newFile('Super_test_library.php')->withContent($content)->at($this->load->libs_dir);
46   - $this->assertNull($this->load->library('super_test_library'));
  102 + public function test_library_config()
  103 + {
  104 + // Create library in VFS
  105 + $lib = 'unit_test_config_lib';
  106 + $class = 'CI_'.ucfirst($lib);
  107 + $content = '<?php class '.$class.' { public function __construct($params) { $this->config = $params; } }';
  108 + $this->ci_vfs_create($lib, $content, $this->ci_base_root, 'libraries');
  109 +
  110 + // Create config file
  111 + $cfg = array(
  112 + 'foo' => 'bar',
  113 + 'bar' => 'baz',
  114 + 'baz' => false
  115 + );
  116 + $this->ci_vfs_create($lib, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
47 117
48   - // Was the model class instantiated.
49   - $this->assertTrue(class_exists('Super_test_library'));
  118 + // Test object name and config
  119 + $obj = 'testy';
  120 + $this->assertNull($this->load->library($lib, NULL, $obj));
  121 + $this->assertTrue(class_exists($class), $class.' does not exist');
  122 + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj);
  123 + $this->assertEquals($cfg, $this->ci_obj->$obj->config);
  124 +
  125 + // Test is_loaded
  126 + $this->assertEquals($obj, $this->load->is_loaded($lib));
50 127 }
51 128
52 129 // --------------------------------------------------------------------
53 130
54   - private function _setup_config_mock()
  131 + public function test_load_library_in_application_dir()
55 132 {
56   - // Mock up a config object until we
57   - // figure out how to test the library configs
58   - $config = $this->getMock('CI_Config', NULL, array(), '', FALSE);
59   - $config->expects($this->any())
60   - ->method('load')
61   - ->will($this->returnValue(TRUE));
  133 + // Create library in VFS
  134 + $lib = 'super_test_library';
  135 + $class = ucfirst($lib);
  136 + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries');
  137 +
  138 + // Load library
  139 + $this->assertNull($this->load->library($lib));
62 140
63   - // Add the mock to our stdClass
64   - $this->ci_instance_var('config', $config);
  141 + // Was the model class instantiated.
  142 + $this->assertTrue(class_exists($class), $class.' does not exist');
  143 + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj);
65 144 }
66 145
67 146 // --------------------------------------------------------------------
68 147
69   - public function test_non_existent_model()
  148 + public function test_driver()
70 149 {
71   - $this->setExpectedException(
72   - 'RuntimeException',
73   - 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php'
74   - );
  150 + // Create driver in VFS
  151 + $driver = 'unit_test_driver';
  152 + $dir = ucfirst($driver);
  153 + $class = 'CI_'.$dir;
  154 + $content = '<?php class '.$class.' { } ';
  155 + $this->ci_vfs_create($driver, $content, $this->ci_base_root, 'libraries/'.$dir);
75 156
76   - $this->load->model('ci_test_nonexistent_model.php');
  157 + // Test loading as an array.
  158 + $this->assertNull($this->load->driver(array($driver)));
  159 + $this->assertTrue(class_exists($class), $class.' does not exist');
  160 + $this->assertAttributeInstanceOf($class, $driver, $this->ci_obj);
  161 +
  162 + // Test loading as a library with a name
  163 + $obj = 'testdrive';
  164 + $this->assertNull($this->load->library($driver, NULL, $obj));
  165 + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj);
  166 +
  167 + // Test no driver given
  168 + $this->assertFalse($this->load->driver());
  169 +
  170 + // Test a string given to params
  171 + $this->assertNull($this->load->driver($driver, ' '));
77 172 }
78 173
79 174 // --------------------------------------------------------------------
80 175
81   - /**
82   - * @coverts CI_Loader::model
83   - */
84 176 public function test_models()
85 177 {
86 178 $this->ci_set_core_class('model', 'CI_Model');
87 179
88   - $content = '<?php class Unit_test_model extends CI_Model {} ';
  180 + // Create model in VFS
  181 + $model = 'unit_test_model';
  182 + $class = ucfirst($model);
  183 + $content = '<?php class '.$class.' extends CI_Model {} ';
  184 + $this->ci_vfs_create($model, $content, $this->ci_app_root, 'models');
89 185
90   - $model = vfsStream::newFile('unit_test_model.php')->withContent($content)->at($this->load->models_dir);
91   -
92   - $this->assertNull($this->load->model('unit_test_model'));
  186 + // Load model
  187 + $this->assertNull($this->load->model($model));
93 188
94 189 // Was the model class instantiated.
95   - $this->assertTrue(class_exists('Unit_test_model'));
  190 + $this->assertTrue(class_exists($class));
96 191
97 192 // Test no model given
98 193 $this->assertNull($this->load->model(''));
@@ -100,36 +195,85 @@ public function test_models()
100 195
101 196 // --------------------------------------------------------------------
102 197
  198 + public function test_model_subdir()
  199 + {
  200 + // Make sure base class is loaded - we'll test _ci_include later
  201 + $this->ci_core_class('model');
  202 +
  203 + // Create modelin VFS
  204 + $model = 'test_sub_model';
  205 + $base = 'CI_Model';
  206 + $class = ucfirst($model);
  207 + $subdir = 'cars';
  208 + $this->ci_vfs_create($model, '<?php class '.$class.' extends '.$base.' { }', $this->ci_app_root,
  209 + array('models', $subdir));
  210 +
  211 + // Load model
  212 + $name = 'testors';
  213 + $this->assertNull($this->load->model($subdir.'/'.$model, $name));
  214 +
  215 + // Was the model class instantiated?
  216 + $this->assertTrue(class_exists($class));
  217 + $this->assertObjectHasAttribute($name, $this->ci_obj);
  218 + $this->assertAttributeInstanceOf($base, $name, $this->ci_obj);
  219 + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
  220 +
  221 + // Test name conflict
  222 + $obj = 'conflict';
  223 + $this->ci_obj->$obj = new StdClass();
  224 + $this->setExpectedException(
  225 + 'RuntimeException',
  226 + 'CI Error: The model name you are loading is the name of a resource that is already being used: '.$obj
  227 + );
  228 + $this->load->model('not_real', $obj);
  229 + }
  230 +
  231 + // --------------------------------------------------------------------
  232 +
  233 + public function test_non_existent_model()
  234 + {
  235 + $this->setExpectedException(
  236 + 'RuntimeException',
  237 + 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php'
  238 + );
  239 +
  240 + $this->load->model('ci_test_nonexistent_model.php');
  241 + }
  242 +
  243 + // --------------------------------------------------------------------
  244 +
103 245 // public function testDatabase()
104 246 // {
105   - // $this->assertEquals(NULL, $this->load->database());
106   - // $this->assertEquals(NULL, $this->load->dbutil());
  247 + // $this->assertNull($this->load->database());
  248 + // $this->assertNull($this->load->dbutil());
107 249 // }
108 250
109 251 // --------------------------------------------------------------------
110 252
111   - /**
112   - * @coverts CI_Loader::view
113   - */
114 253 public function test_load_view()
115 254 {
116   - $this->ci_set_core_class('output', 'CI_Output');
117   -
118   - $content = 'This is my test page. <?php echo $hello; ?>';
119   - $view = vfsStream::newFile('unit_test_view.php')->withContent($content)->at($this->load->views_dir);
120   -
121   - // Use the optional return parameter in this test, so the view is not
122   - // run through the output class.
123   - $this->assertEquals('This is my test page. World!',
124   - $this->load->view('unit_test_view', array('hello' => "World!"), TRUE));
125   -
  255 + // Create view in VFS
  256 + $view = 'unit_test_view';
  257 + $var = 'hello';
  258 + $value = 'World!';
  259 + $content = 'This is my test page. ';
  260 + $this->ci_vfs_create($view, $content.'<?php echo $'.$var.';', $this->ci_app_root, 'views');
  261 +
  262 + // Test returning view
  263 + $out = $this->load->view($view, array($var => $value), TRUE);
  264 + $this->assertEquals($content.$value, $out);
  265 +
  266 + // Mock output class
  267 + $output = $this->getMock('CI_Output', array('append_output'));
  268 + $output->expects($this->once())->method('append_output')->with($content.$value);
  269 + $this->ci_instance_var('output', $output);
  270 +
  271 + // Test view output
  272 + $this->assertNull($this->load->view($view, array($var => $value)));
126 273 }
127 274
128 275 // --------------------------------------------------------------------
129 276
130   - /**
131   - * @coverts CI_Loader::view
132   - */
133 277 public function test_non_existent_view()
134 278 {
135 279 $this->setExpectedException(
@@ -144,14 +288,17 @@ public function test_non_existent_view()
144 288
145 289 public function test_file()
146 290 {
  291 + // Create view in VFS
  292 + $dir = 'views';
  293 + $file = 'ci_test_mock_file';
147 294 $content = 'Here is a test file, which we will load now.';
148   - $file = vfsStream::newFile('ci_test_mock_file.php')->withContent($content)->at($this->load->views_dir);
  295 + $this->ci_vfs_create($file, $content, $this->ci_app_root, $dir);
149 296
150 297 // Just like load->view(), take the output class out of the mix here.
151   - $load = $this->load->file(vfsStream::url('application').'/views/ci_test_mock_file.php', TRUE);
152   -
153   - $this->assertEquals($content, $load);
  298 + $out = $this->load->file(APPPATH.$dir.'/'.$file.'.php', TRUE);
  299 + $this->assertEquals($content, $out);
154 300
  301 + // Test non-existent file
155 302 $this->setExpectedException(
156 303 'RuntimeException',
157 304 'CI Error: Unable to load the requested file: ci_test_file_not_exists'
@@ -164,21 +311,56 @@ public function test_file()
164 311
165 312 public function test_vars()
166 313 {
167   - $this->assertNull($this->load->vars(array('foo' => 'bar')));
168   - $this->assertNull($this->load->vars('foo', 'bar'));
  314 + $key1 = 'foo';
  315 + $val1 = 'bar';
  316 + $key2 = 'boo';
  317 + $val2 = 'hoo';
  318 + $this->assertNull($this->load->vars(array($key1 => $val1)));
  319 + $this->assertNull($this->load->vars($key2, $val2));
  320 + $this->assertEquals($val1, $this->load->get_var($key1));
  321 + $this->assertEquals(array($key1 => $val1, $key2 => $val2), $this->load->get_vars());
169 322 }
170 323
171 324 // --------------------------------------------------------------------
172 325
173 326 public function test_helper()
174 327 {
175   - $this->assertEquals(NULL, $this->load->helper('array'));
  328 + // Create helper in VFS
  329 + $helper = 'test';
  330 + $func = '_my_helper_test_func';
  331 + $content = '<?php function '.$func.'() { return true; } ';
  332 + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_base_root, 'helpers');
  333 +
  334 + // Create helper extension
  335 + $exfunc = '_my_extension_func';
  336 + $content = '<?php function '.$exfunc.'() { return true; } ';
  337 + $this->ci_vfs_create($this->prefix.$helper.'_helper', $content, $this->ci_app_root, 'helpers');
  338 +
  339 + // Load helper
  340 + $this->assertNull($this->load->helper($helper));
  341 + $this->assertTrue(function_exists($func), $func.' does not exist');
  342 + $this->assertTrue(function_exists($exfunc), $exfunc.' does not exist');
  343 +
  344 + // Create baseless extension
  345 + $ext = 'bad_ext';
  346 + $this->ci_vfs_create($this->prefix.$ext.'_helper', '', $this->ci_app_root, 'helpers');
  347 +
  348 + // Test bad extension
  349 + $this->setExpectedException(
  350 + 'RuntimeException',
  351 + 'CI Error: Unable to load the requested file: helpers/'.$ext.'_helper.php'
  352 + );
  353 + $this->load->helper($ext);
  354 + }
  355 +
  356 + // --------------------------------------------------------------------
176 357
  358 + public function test_non_existent_helper()
  359 + {
177 360 $this->setExpectedException(
178 361 'RuntimeException',
179 362 'CI Error: Unable to load the requested file: helpers/bad_helper.php'
180 363 );
181   -
182 364 $this->load->helper('bad');
183 365 }
184 366
@@ -186,36 +368,154 @@ public function test_helper()
186 368
187 369 public function test_loading_multiple_helpers()
188 370 {
189   - $this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string')));
  371 + // Create helpers in VFS
  372 + $helpers = array();
  373 + $funcs = array();
  374 + $files = array();
  375 + for ($i = 1; $i <= 3; ++$i) {
  376 + $helper = 'test'.$i;
  377 + $helpers[] = $helper;
  378 + $func = '_my_helper_test_func'.$i;
  379 + $funcs[] = $func;
  380 + $files[$helper.'_helper'] = '<?php function '.$func.'() { return true; } ';
  381 + }
  382 + $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers');
  383 +
  384 + // Load helpers
  385 + $this->assertNull($this->load->helpers($helpers));
  386 +
  387 + // Verify helper existence
  388 + foreach ($funcs as $func) {
  389 + $this->assertTrue(function_exists($func), $func.' does not exist');
  390 + }
190 391 }
191 392
192 393 // --------------------------------------------------------------------
193 394
194   - // public function testLanguage()
195   - // {
196   - // $this->assertEquals(NULL, $this->load->language('test'));
197   - // }
  395 + public function test_language()
  396 + {
  397 + // Mock lang class and test load call
  398 + $file = 'test';
  399 + $lang = $this->getMock('CI_Lang', array('load'));
  400 + $lang->expects($this->once())->method('load')->with($file);
  401 + $this->ci_instance_var('lang', $lang);
  402 + $this->assertNull($this->load->language($file));
  403 + }
198 404
199 405 // --------------------------------------------------------------------
200 406
201   - public function test_load_config()
  407 + public function test_packages()
202 408 {
203   - $this->_setup_config_mock();
204   - $this->assertNull($this->load->config('config', FALSE));
  409 + // Create model in VFS package path
  410 + $dir = 'third-party';
  411 + $lib = 'unit_test_package';
  412 + $class = 'CI_'.ucfirst($lib);
  413 + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, array($dir, 'libraries'));
  414 +
  415 + // Get paths
  416 + $paths = $this->load->get_package_paths(TRUE);
  417 +
  418 + // Add path and verify
  419 + $path = APPPATH.$dir.'/';
  420 + $this->assertNull($this->load->add_package_path($path));
  421 + $this->assertContains($path, $this->load->get_package_paths(TRUE));
  422 +
  423 + // Test successful load
  424 + $this->assertNull($this->load->library($lib));
  425 + $this->assertTrue(class_exists($class), $class.' does not exist');
  426 +
  427 + // Add another path
  428 + $path2 = APPPATH.'another/';
  429 + $this->assertNull($this->load->add_package_path($path2));
  430 + $this->assertContains($path2, $this->load->get_package_paths(TRUE));
  431 +
  432 + // Remove last path
  433 + $this->assertNull($this->load->remove_package_path());
  434 + $this->assertNotContains($path2, $this->load->get_package_paths(TRUE));
  435 +
  436 + // Remove path and verify restored paths
  437 + $this->assertNull($this->load->remove_package_path($path));
  438 + $this->assertEquals($paths, $this->load->get_package_paths(TRUE));
  439 +
  440 + // Test failed load without path
  441 + $this->setExpectedException(
  442 + 'RuntimeException',
  443 + 'CI Error: Unable to load the requested class: '.$lib
  444 + );
  445 + $this->load->library($lib);
205 446 }
206 447
207 448 // --------------------------------------------------------------------
208 449
209   - public function test_load_bad_config()
  450 + public function test_load_config()
210 451 {
211   - $this->_setup_config_mock();
  452 + $cfg = 'someconfig';
  453 + $this->assertNull($this->load->config($cfg, FALSE));
  454 + $this->assertContains($cfg, $this->ci_obj->config->loaded);
  455 + }
212 456
213   - $this->setExpectedException(
214   - 'RuntimeException',
215   - 'CI Error: The configuration file foobar.php does not exist.'
  457 + // --------------------------------------------------------------------
  458 +
  459 + public function test_initialize()
  460 + {
  461 + // Create helper in VFS
  462 + $helper = 'autohelp';
  463 + $hlp_func = '_autohelp_test_func';
  464 + $content = '<?php function '.$hlp_func.'() { return true; }';
  465 + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers');
  466 +
  467 + // Create library in VFS
  468 + $lib = 'autolib';
  469 + $lib_class = 'CI_'.ucfirst($lib);
  470 + $this->ci_vfs_create($lib, '<?php class '.$lib_class.' { }', $this->ci_base_root, 'libraries');
  471 +
  472 + // Create driver in VFS
  473 + $drv = 'autodrv';
  474 + $subdir = ucfirst($drv);
  475 + $drv_class = 'CI_'.$subdir;
  476 + $this->ci_vfs_create($drv, '<?php class '.$drv_class.' { }', $this->ci_base_root, array('libraries', $subdir));
  477 +
  478 + // Create model in VFS package path
  479 + $dir = 'testdir';
  480 + $path = APPPATH.$dir.'/';
  481 + $model = 'automod';
  482 + $mod_class = ucfirst($model);
  483 + $this->ci_vfs_create($model, '<?php class '.$mod_class.' { }', $this->ci_app_root, array($dir, 'models'));
  484 +
  485 + // Create autoloader config
  486 + $cfg = array(
  487 + 'packages' => array($path),
  488 + 'helper' => array($helper),
  489 + 'libraries' => array($lib),
  490 + 'drivers' => array($drv),
  491 + 'model' => array($model),
  492 + 'config' => array('config1', 'config2')
216 493 );
  494 + $this->ci_vfs_create('autoload', '<?php $autoload = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
  495 +
  496 + // Run initialize and autoloader
  497 + $this->load->initialize();
  498 +
  499 + // Verify path
  500 + $this->assertContains($path, $this->load->get_package_paths());
  501 +
  502 + // Verify helper
  503 + $this->assertTrue(function_exists($hlp_func), $hlp_func.' does not exist');
  504 +
  505 + // Verify library
  506 + $this->assertTrue(class_exists($lib_class), $lib_class.' does not exist');
  507 + $this->assertAttributeInstanceOf($lib_class, $lib, $this->ci_obj);
  508 +
  509 + // Verify driver
  510 + $this->assertTrue(class_exists($drv_class), $drv_class.' does not exist');
  511 + $this->assertAttributeInstanceOf($drv_class, $drv, $this->ci_obj);
  512 +
  513 + // Verify model
  514 + $this->assertTrue(class_exists($mod_class), $mod_class.' does not exist');
  515 + $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj);
217 516
218   - $this->load->config('foobar', FALSE);
  517 + // Verify config calls
  518 + $this->assertEquals($cfg['config'], $this->ci_obj->config->loaded);
219 519 }
220 520
221 521 }
2  tests/codeigniter/helpers/date_helper_test.php
@@ -168,6 +168,8 @@ public function test_standard_date_w3c()
168 168
169 169 public function test_timespan()
170 170 {
  171 + $this->ci_vfs_clone('system/language/english/date_lang.php');
  172 +
171 173 $loader_cls = $this->ci_core_class('load');
172 174 $this->ci_instance_var('load', new $loader_cls);
173 175
8 tests/codeigniter/helpers/form_helper_test.php
... ... @@ -1,10 +1,12 @@
1 1 <?php
2 2
3   -require BASEPATH . 'core/Common.php';
4   -require BASEPATH . 'helpers/form_helper.php';
5   -
6 3 class Form_helper_test extends CI_TestCase
7 4 {
  5 + public function set_up()
  6 + {
  7 + $this->helper('form');
  8 + }
  9 +
8 10 public function test_form_hidden()
9 11 {
10 12 $expected = <<<EOH
17 tests/codeigniter/helpers/number_helper_test.php
@@ -11,31 +11,18 @@ public function set_up()
11 11
12 12 // Mock away load, too much going on in there,
13 13 // we'll just check for the expected parameter
14   -
15 14 $lang = $this->getMock($lang_cls, array('load'));
16 15 $lang->expects($this->once())
17 16 ->method('load')
18 17 ->with($this->equalTo('number'));
19 18
20 19 // Assign the proper language array
21   -
22   - $lang->language = $this->_get_lang('number');
  20 + $lang->language = $this->lang('number');
23 21
24 22 // We don't have a controller, so just create
25 23 // a cheap class to act as our super object.
26 24 // Make sure it has a lang attribute.
27   -
28   - $obj = new stdClass;
29   - $obj->lang = $lang;
30   - $this->ci_instance($obj);
31   - }
32   -
33   - // Quick helper to actually grab the language
34   - // file. Consider moving this to ci_testcase?
35   - public function _get_lang($name)
36   - {
37   - require BASEPATH.'language/english/'.$name.'_lang.php';
38   - return $lang;
  25 + $this->ci_instance_var('lang', $lang);
39 26 }
40 27
41 28 public function test_byte_format()
1  tests/codeigniter/helpers/text_helper_test.php
@@ -64,6 +64,7 @@ public function test_entities_to_ascii()
64 64
65 65 public function test_convert_accented_characters()
66 66 {
  67 + $this->ci_vfs_clone('application/config/foreign_chars.php');
67 68 $this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ'));
68 69 $this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü'));
69 70 }
7 tests/codeigniter/libraries/Encrypt_test.php
@@ -4,11 +4,8 @@ class Encrypt_test extends CI_TestCase {
4 4
5 5 public function set_up()
6 6 {
7   - $obj = new stdClass;
8   - $obj->encrypt = new Mock_Libraries_Encrypt();
9   -
10   - $this->ci_instance($obj);
11   - $this->encrypt = $obj->encrypt;
  7 + $this->encrypt = new Mock_Libraries_Encrypt();
  8 + $this->ci_instance_var('encrypt', $this->encrypt);
12 9
13 10 $this->ci_set_config('encryption_key', "Encryptin'glike@boss!");
14 11 $this->msg = 'My secret message';
8 tests/codeigniter/libraries/Parser_test.php
@@ -4,12 +4,8 @@ class Parser_test extends CI_TestCase {
4 4
5 5 public function set_up()
6 6 {
7   - $obj = new stdClass;
8   - $obj->parser = new Mock_Libraries_Parser();
9   -
10   - $this->ci_instance($obj);
11   -
12   - $this->parser = $obj->parser;
  7 + $this->parser = new Mock_Libraries_Parser();
  8 + $this->ci_instance_var('parser', $this->parser);
13 9 }
14 10
15 11 // --------------------------------------------------------------------
12 tests/codeigniter/libraries/Session_test.php
@@ -29,13 +29,15 @@ public function set_up()
29 29 $_COOKIE = array();
30 30
31 31 // Establish necessary support classes
32   - $obj = new stdClass;
33 32 $cfg = $this->ci_core_class('cfg');
34   - $obj->config = new $cfg();
35 33 $ldr = $this->ci_core_class('load');
36   - $obj->load = new $ldr();
37   - $obj->input = new Mock_Core_Input(NULL, NULL);
38   - $this->ci_instance($obj);
  34 + $ci = $this->ci_instance();
  35 + $ci->config = new $cfg();
  36 + $ci->load = new $ldr();
  37 + $ci->input = new Mock_Core_Input(NULL, NULL);
  38 +
  39 + // Make sure string helper is available
  40 + $this->ci_vfs_clone('system/helpers/string_helper.php');
39 41
40 42 // Attach session instance locally
41 43 $config = array(
8 tests/codeigniter/libraries/Table_test.php
@@ -4,12 +4,8 @@ class Table_test extends CI_TestCase {