Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Router unit test

Signed-off-by: dchill42 <dchill42@gmail.com>
  • Loading branch information...
commit bcc58654dec7424e899b9c3f994f7965097fad4d 1 parent 6f7b0b6
@dchill42 authored
View
104 system/core/Router.php
@@ -9,7 +9,7 @@
* Licensed under the Open Software License version 3.0
*
* This source file is subject to the Open Software License (OSL 3.0) that is
- * bundled with this package in the files license.txt / license.rst. It is
+ * bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
* http://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to obtain it
@@ -103,70 +103,52 @@ public function _set_routing()
// Load the routes.php file.
$route = $this->CI->config->get('routes.php', 'route');
- // Set routes
+ // Set route remapping
$this->routes = is_array($route) ? $route : array();
unset($route);
// Set the default controller so we can display it in the event
// the URI doesn't correlate to a valid controller.
$this->default_controller = empty($this->routes['default_controller']) ? FALSE :
- strtolower($this->routes['default_controller']);
+ strtolower($this->routes['default_controller']);
// Are query strings enabled in the config file? Normally CI doesn't utilize query strings
// since URI segments are more search-engine friendly, but they can optionally be used.
// If this feature is enabled, we will gather the directory/class/method a little differently
- $uri =& $this->CI->uri;
- $config =& $this->CI->config;
- $ctl_trigger = $config->item('controller_trigger');
- if ($config->item('enable_query_strings') === TRUE && isset($_GET[$ctl_trigger]))
+ $ctl_trigger = $this->CI->config->item('controller_trigger');
+ if ($this->CI->config->item('enable_query_strings') === TRUE && isset($_GET[$ctl_trigger]))
{
$segments = array();
// Add directory segment if provided
- $dir_trigger = $config->item('directory_trigger');
+ $dir_trigger = $this->CI->config->item('directory_trigger');
if (isset($_GET[$dir_trigger]))
{
- $segments[] = trim($uri->_filter_uri($_GET[$dir_trigger]));
+ $segments[] = trim($this->CI->uri->_filter_uri($_GET[$dir_trigger]));
}
// Add controller segment - this was qualified above
- $class = trim($uri->_filter_uri($_GET[$ctl_trigger]));
+ $class = trim($this->CI->uri->_filter_uri($_GET[$ctl_trigger]));
$segments[] = $class;
// Add function segment if provided
- $fun_trigger = $config->item('function_trigger');
+ $fun_trigger = $this->CI->config->item('function_trigger');
if (isset($_GET[$fun_trigger]))
{
- $segments[] = trim($uri->_filter_uri($_GET[$fun_trigger]));
+ $segments[] = trim($this->CI->uri->_filter_uri($_GET[$fun_trigger]));
}
-
- // Determine if segments point to a valid route
- $route = $this->validate_route($segments);
- if ($route === FALSE)
- {
- // Invalid request - show a 404
- show_404($class);
- }
-
- // Set route stack and clean directory and class
- $this->route_stack = $route;
- $this->set_directory($route[self::SEG_SUBDIR]);
- $this->set_class($route[self::SEG_CLASS]);
- return;
+ }
+ else
+ {
+ // Fetch the complete URI string, remove the suffix, and explode
+ $this->CI->uri->_fetch_uri_string();
+ $this->CI->uri->_remove_url_suffix();
+ $this->CI->uri->_explode_segments();
+ $segments = $this->CI->uri->segments;
}
- // Fetch the complete URI string
- $uri->_fetch_uri_string();
-
- // Do we need to remove the URL suffix?
- $uri->_remove_url_suffix();
-
- // Compile the segments into an array
- $uri->_explode_segments();
-
- // Parse any custom routing that may exist
- // The default route will be applied if valid and necessary
- $this->_parse_routes();
+ // Set the route stack
+ $this->_set_request($segments);
}
// --------------------------------------------------------------------
@@ -177,10 +159,10 @@ public function _set_routing()
* This function takes an array of URI segments as
* input, and sets the current class/method
*
- * @param array
+ * @param array Route segments
* @return void
*/
- protected function _set_request($segments = array())
+ protected function _set_request(array $segments)
{
// Determine if segments point to a valid route
$route = $this->validate_route($segments);
@@ -227,20 +209,25 @@ protected function _set_request($segments = array())
public function validate_route($route)
{
// If we don't have any segments, the default will have to do
- if (count($route) == 0)
+ if (empty($route))
{
$route = $this->_default_segments();
- if (empty($route)) {
+ if (empty($route))
+ {
// No default - fail
return FALSE;
}
}
// Explode route if not already segmented
- if (!is_array($route)) {
+ if ( ! is_array($route))
+ {
$route = explode('/', $route);
}
+ // Parse any custom routing that may exist
+ $route = $this->_parse_routes($route);
+
// Search paths for controller
foreach ($this->CI->load->get_package_paths() as $path)
{
@@ -279,12 +266,12 @@ public function validate_route($route)
}
// Get class and method
- $class = array_unshift($default);
- $method = array_unshift($default);
+ $class = array_shift($default);
+ $method = array_shift($default);
}
// Does the requested controller exist in the sub-folder?
- if (file_exists($path.'controllers/'.$route[0].$class.'.php'))
+ if (file_exists($path.'controllers/'.$route[0].'/'.$class.'.php'))
{
// Found it - assemble segments
if ( ! isset($route[1]))
@@ -320,18 +307,18 @@ public function validate_route($route)
* the config/routes.php file against the URI to
* determine if the class/method need to be remapped.
*
- * @return void
+ * @param array Route segments
+ * @return array Remapped segments
*/
- protected function _parse_routes()
+ protected function _parse_routes(array $segments)
{
// Turn the segment array into a URI string
- $segments = $this->CI->uri->segments;
$uri = implode('/', $segments);
// Is there a literal match? If so we're done
if (isset($this->routes[$uri]))
{
- return $this->_set_request(explode('/', $this->routes[$uri]));
+ return explode('/', $this->routes[$uri]);
}
// Loop through the route array looking for wild-cards
@@ -349,13 +336,13 @@ protected function _parse_routes()
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
- return $this->_set_request(explode('/', $val));
+ return explode('/', $val);
}
}
// If we got this far it means we didn't encounter a
- // matching route so we'll set the site default route
- $this->_set_request($segments);
+ // matching route so we'll return the segments unchanged
+ return $segments;
}
// --------------------------------------------------------------------
@@ -482,11 +469,11 @@ public function fetch_route()
* @return mixed FALSE if route doesn't exist, otherwise array of 4+ segments
*/
public function get_error_route($is404 = FALSE)
- {
+ {
// Select route
$route = ($is404 ? '404' : 'error').'_override';
- // See if 404_override is defined
+ // See if error or 404 override is defined
if (empty($this->routes[$route])) {
// No override to apply
return FALSE;
@@ -511,6 +498,11 @@ public function _set_overrides($routing)
return;
}
+ if (isset($routing['path']))
+ {
+ $this->set_path($routing['path']);
+ }
+
if (isset($routing['directory']))
{
$this->set_directory($routing['directory']);
@@ -539,7 +531,7 @@ public function _set_overrides($routing)
protected function _default_segments()
{
// Check for default controller
- if ($this->default_controller === FALSE)
+ if (empty($this->default_controller))
{
// Return empty array
return array();
View
9 tests/codeigniter/core/CodeIgniter_test.php
@@ -174,7 +174,7 @@ public function test_sub_instance()
$class = 'Mock_Core_CodeIgniter';
$subclass = $pre.$class;
$content = '<?php class '.$subclass.' extends '.$class.' { }';
- $this->ci_vfs_create($subclass, $content, $this->ci_vfs_root, $path, 'core');
+ $this->ci_vfs_create($subclass, $content, $this->ci_vfs_root, array($path, 'core'));
// Create instance
$CI = Mock_Core_CodeIgniter::instance($this->ci_base_path, $this->ci_app_path);
@@ -737,7 +737,12 @@ private function _create_config(array $config, $name = 'config', $sub = NULL)
$content = '<?php $'.$name.' = '.var_export($config, TRUE).';';
// Create file under subdirectory of app config dir
- $this->ci_vfs_create($name, $content, $this->ci_app_root, 'config', $sub);
+ $path = array('config');
+ if ($sub)
+ {
+ $path[] = $sub;
+ }
+ $this->ci_vfs_create($name, $content, $this->ci_app_root, $path);
}
/**
View
2  tests/codeigniter/core/Config_test.php
@@ -245,7 +245,7 @@ public function test_get()
);
$dir = 'package';
$content = '<?php $'.$name.' = '.var_export($cfg2, TRUE).';';
- $this->ci_vfs_create($file, $content, $this->ci_vfs_root, $dir, 'config');
+ $this->ci_vfs_create($file, $content, $this->ci_vfs_root, array($dir, 'config'));
// Add config path
array_push($this->config->_config_paths, $this->ci_vfs_path($dir.'/'));
View
13 tests/codeigniter/core/Loader_test.php
@@ -116,7 +116,7 @@ public function test_library_config()
$class = 'CI_'.$lib;
$content = '<?php class '.$class.
' { public function __construct($params = NULL) { $this->config = $params; } } ';
- $this->ci_vfs_create($lib, $content, $this->ci_base_root, 'libraries', $sub);
+ $this->ci_vfs_create($lib, $content, $this->ci_base_root, array('libraries', $sub));
// Create config to be loaded
// For isolation, we just set the contents in CI_TestConfig to be retrieved
@@ -200,7 +200,7 @@ public function test_driver()
$driver = 'unit_test_driver';
$dir = ucfirst($driver);
$class = 'CI_'.$dir;
- $this->ci_vfs_create($driver, $this->_empty($class), $this->ci_base_root, 'libraries', $dir);
+ $this->ci_vfs_create($driver, $this->_empty($class), $this->ci_base_root, array('libraries', $dir));
// Test loading as an array.
$this->assertNull($this->load->driver(array($driver)));
@@ -265,7 +265,7 @@ public function test_model_subdir()
$base = 'CI_Model';
$class = ucfirst($model);
$subdir = 'cars';
- $this->ci_vfs_create($model, $this->_empty($class, $base), $this->ci_app_root, 'models', $subdir);
+ $this->ci_vfs_create($model, $this->_empty($class, $base), $this->ci_app_root, array('models', $subdir));
// Load model
$name = 'testors';
@@ -401,7 +401,8 @@ public function test_controller_result()
$subdir = 'special';
$class = 'TestResultCtlr';
$method = 'result_handler';
- $this->ci_vfs_create(strtolower($class), $this->_empty($class), $this->ci_app_root, 'controllers', $subdir);
+ $this->ci_vfs_create(strtolower($class), $this->_empty($class), $this->ci_app_root,
+ array('controllers', $subdir));
// Create route stack to pass
$route = array(
@@ -660,14 +661,14 @@ public function test_autoloader()
$drv = 'autodrv';
$subdir = ucfirst($drv);
$drv_class = 'CI_'.$subdir;
- $this->ci_vfs_create($drv, $this->_empty($drv_class), $this->ci_base_root, 'libraries', $subdir);
+ $this->ci_vfs_create($drv, $this->_empty($drv_class), $this->ci_base_root, array('libraries', $subdir));
// Create package directory in app path with model
$dir = 'testdir';
$path = $this->ci_app_path.$dir.'/';
$model = 'automod';
$mod_class = ucfirst($model);
- $this->ci_vfs_create($model, $this->_empty($mod_class), $this->ci_app_root, $dir, 'models');
+ $this->ci_vfs_create($model, $this->_empty($mod_class), $this->ci_app_root, array($dir, 'models'));
// Autoload path since autoloaded packages are handled during bootstrapping
$this->load->add_package_path($path);
View
411 tests/codeigniter/core/Router_test.php
@@ -0,0 +1,411 @@
+<?php
+
+class Router_test extends CI_TestCase {
+
+ private $ci;
+
+ /**
+ * Set up before each test
+ */
+ public function set_up()
+ {
+ // Get instance object
+ $this->ci = $this->ci_instance();
+
+ // Create router
+ $this->router = new Mock_Core_Router();
+ }
+
+ /**
+ * Test validating route
+ *
+ * @covers CI_Router::validate_route
+ */
+ public function test_validate_route()
+ {
+ // Mock loader with package paths
+ $this->_mock_loader();
+ $alt_dir = 'alternative';
+ $alt_root = $this->ci_vfs_mkdir($alt_dir);
+ $alt_path = $this->ci_vfs_path($alt_dir);
+ $this->ci->load->paths = array($alt_path, $this->ci_app_path);
+
+ // Create controller in app path
+ $dir = 'controllers';
+ $ctlr = 'app_ctlr';
+ $this->ci_vfs_create($ctlr, '', $this->ci_app_root, $dir);
+
+ // Is the default function routed?
+ $expect = array($this->ci_app_path, '', $ctlr, 'index');
+ $this->assertEquals($expect, $this->router->validate_route($ctlr));
+
+ // Create a controller in a subdirectory of the alternate path
+ $sub = 'bass';
+ $ctlr2 = 'sub_ctlr';
+ $func2 = 'drop';
+ $this->ci_vfs_create($ctlr2, '', $alt_root, array($dir, $sub));
+
+ // Do the alternate path and subdirectory work?
+ $expect = array($alt_path, $sub, $ctlr2, $func2);
+ $this->assertEquals($expect, $this->router->validate_route(array($sub, $ctlr2, $func2)));
+
+ // Set default controller with argument
+ $def_ctlr = 'foo';
+ $def_func = 'bar';
+ $def_arg = 'baz';
+ $this->ci_vfs_create($def_ctlr, '', $this->ci_app_root, $dir);
+ $this->router->default_ctlr($def_ctlr.'/'.$def_func.'/'.$def_arg);
+
+ // Do the default and argument work?
+ $expect = array($this->ci_app_path, '', $def_ctlr, $def_func, $def_arg);
+ $this->assertEquals($expect, $this->router->validate_route(''));
+
+ // Set route
+ $route3 = 'remap/request';
+ $ctlr3 = 'real_ctlr';
+ $func3 = 'action';
+ $this->router->routes = array($route3 => $ctlr3.'/'.$func3);
+
+ // Create actual controller
+ $this->ci_vfs_create($ctlr3, '', $this->ci_app_root, $dir);
+
+ // Does the request get remapped?
+ $expect = array($this->ci_app_path, '', $ctlr3, $func3);
+ $this->assertEquals($expect, $this->router->validate_route($route3));
+
+ // Set route with number arg
+ $rctlr = 'remap';
+ $rfunc = 'request';
+ $route4 = $rctlr.'/'.$rfunc;
+ $ctlr4 = 'real_ctlr';
+ $func4 = 'action';
+ $this->router->routes = array($route4.'/(:num)' => $ctlr4.'/'.$func4.'/$1');
+
+ // Create actual controller
+ $this->ci_vfs_create($ctlr4, '', $this->ci_app_root, $dir);
+
+ // Does the argument get passed?
+ $arg4 = '42';
+ $expect = array($this->ci_app_path, '', $ctlr4, $func4, $arg4);
+ $this->assertEquals($expect, $this->router->validate_route($route4.'/'.$arg4));
+
+ // Create remap controller
+ $this->ci_vfs_create($rctlr, '', $this->ci_app_root, $dir);
+
+ // Is the route ignored for a non-number?
+ $arg = 'foo';
+ $expect = array($this->ci_app_path, '', $rctlr, $rfunc, $arg);
+ $this->assertEquals($expect, $this->router->validate_route($route4.'/'.$arg));
+
+ // Does a non-existent route fail?
+ $this->assertFalse($this->router->validate_route('non_ctlr'));
+ }
+
+ /**
+ * Test default routing
+ *
+ * @covers CI_Router::_set_routing
+ */
+ public function test_default_routing()
+ {
+ // Load mock Config
+ $this->ci_set_config();
+
+ // Mock up URI
+ $this->_mock_uri();
+
+ // Mock loader with package paths
+ $this->_mock_loader();
+ $this->ci->load->paths = array($this->ci_app_path);
+
+ // Set up routes config with default
+ $stack = array('main', 'handler');
+ $default = implode('/', $stack);
+ $routes = array('default_controller' => $default);
+ $this->ci->config->to_get($routes);
+
+ // Create default controller
+ $this->ci_vfs_create($stack[0], '', $this->ci_app_root, 'controllers');
+
+ // Set routing
+ $this->router->_set_routing();
+
+ // Did the default get set?
+ $this->assertEquals($default, $this->router->default_ctlr());
+
+ // Did the right URI methods get called?
+ $expect = array('_fetch_uri_string', '_remove_url_suffix', '_explode_segments', '_reindex_segments');
+ $this->assertObjectHasAttribute('called', $this->ci->uri);
+ $this->assertEquals($expect, $this->ci->uri->called);
+
+ // Did the route stack get set correctly?
+ $expect = array($this->ci_app_path, '', $stack[0], $stack[1]);
+ $this->assertEquals($expect, $this->router->fetch_route());
+
+ // Are the routed segments correct?
+ $this->assertEquals($stack, $this->ci->uri->rsegments);
+ }
+
+ /**
+ * Test query string routing
+ *
+ * @covers CI_Router::_set_routing
+ */
+ public function test_query_routing()
+ {
+ // Load mock Config with triggers
+ $ctrigger = 'ctlr';
+ $dtrigger = 'dir';
+ $ftrigger = 'func';
+ $config = array(
+ 'enable_query_strings' => TRUE,
+ 'controller_trigger' => $ctrigger,
+ 'directory_trigger' => $dtrigger,
+ 'function_trigger' => $ftrigger
+ );
+ $this->ci_set_config($config);
+
+ // Mock up URI
+ $this->_mock_uri();
+
+ // Mock loader with package paths
+ $this->_mock_loader();
+ $this->ci->load->paths = array($this->ci_app_path);
+
+ // Set up routes config
+ $routes = array('default_controller' => '');
+ $this->ci->config->to_get($routes);
+
+ // Set query args
+ $dir = 'qdir';
+ $ctlr = 'query_ctlr';
+ $func = 'get';
+ $oldget = $_GET;
+ $_GET = array($dtrigger => $dir, $ctrigger => $ctlr, $ftrigger => $func);
+
+ // Create controller
+ $this->ci_vfs_create($ctlr, '', $this->ci_app_root, array('controllers', $dir));
+
+ // Set routing
+ $this->router->_set_routing();
+
+ // Did the route stack get set?
+ $expect = array($this->ci_app_path, $dir.'/', $ctlr, $func);
+ $this->assertEquals($expect, $this->router->fetch_route());
+
+ // Reset _GET
+ $_GET = $oldget;
+ }
+
+ /**
+ * Test route remapping
+ *
+ * @covers CI_Router::_set_routing
+ */
+ public function test_remap_routing()
+ {
+ // Load mock Config
+ $this->ci_set_config();
+
+ // Mock up URI
+ $this->_mock_uri();
+
+ // Mock loader with package paths
+ $this->_mock_loader();
+ $this->ci->load->paths = array($this->ci_app_path);
+
+ // Set up routes config
+ $route = 'main/path';
+ $arg = 'switch';
+ $ctlr = 'other';
+ $func = 'handler';
+ $routes = array(
+ 'default_controller' => '',
+ $route.'/(:any)' => $ctlr.'/'.$func.'/$1',
+ );
+ $this->ci->config->to_get($routes);
+
+ // Create controller and set segments
+ $this->ci_vfs_create($ctlr, '', $this->ci_app_root, 'controllers');
+ $this->ci->uri->segments = array($ctlr, $func, $arg);
+
+ // Set routing
+ $this->router->_set_routing();
+
+ // Did the route stack get set correctly?
+ $expect = array($this->ci_app_path, '', $ctlr, $func, $arg);
+ $this->assertEquals($expect, $this->router->fetch_route());
+ }
+
+ /**
+ * Test getting error route
+ *
+ * @covers CI_Router::get_error_route
+ */
+ public function test_error_route()
+ {
+ // Mock loader with package paths
+ $this->_mock_loader();
+ $this->ci->load->paths = array($this->ci_app_path);
+
+ // Do we get FALSE with no error route?
+ $this->assertFalse($this->router->get_error_route());
+
+ // Set up error override
+ $dir = 'controllers';
+ $ectlr = 'error_ctlr';
+ $efunc = 'error_func';
+ $this->ci_vfs_create($ectlr, '', $this->ci_app_root, $dir);
+ $this->router->routes = array('error_override' => $ectlr.'/'.$efunc);
+
+ // Do we get the error route when set?
+ $expect = array($this->ci_app_path, '', $ectlr, $efunc);
+ $this->assertEquals($expect, $this->router->get_error_route());
+
+ // Set up 404 override
+ $ctlr = 'e404_ctlr';
+ $func = 'e404_func';
+ $this->ci_vfs_create($ctlr, '', $this->ci_app_root, $dir);
+ $this->router->routes = array('404_override' => $ctlr.'/'.$func);
+
+ // Do we get the error route when set?
+ $expect = array($this->ci_app_path, '', $ctlr, $func);
+ $this->assertEquals($expect, $this->router->get_error_route(TRUE));
+ }
+
+ /**
+ * Test set/fetch methods
+ *
+ * @covers CI_Router::set_path
+ * @covers CI_Router::fetch_path
+ * @covers CI_Router::set_directory
+ * @covers CI_Router::fetch_directory
+ * @covers CI_Router::set_class
+ * @covers CI_Router::fetch_class
+ * @covers CI_Router::set_method
+ * @covers CI_Router::fetch_method
+ * @covers CI_Router::fetch_route
+ */
+ public function test_set_fetch()
+ {
+ // Set/fetch path
+ $path = $this->ci_vfs_path('application/');
+ $this->router->set_path($path);
+ $this->assertEquals($path, $this->router->fetch_path());
+
+ // Set/fetch directory - should add slash
+ $directory = 'custom';
+ $this->router->set_directory($directory);
+ $this->assertEquals($directory.'/', $this->router->fetch_directory());
+
+ // Set/fetch class
+ $class = 'my_ctlr';
+ $this->router->set_class($class);
+ $this->assertEquals($class, $this->router->fetch_class());
+
+ // Set/fetch method
+ $method = 'unusual';
+ $this->router->set_method($method);
+ $this->assertEquals($method, $this->router->fetch_method());
+
+ // Do we get all the parts in the stack?
+ $expect = array($path, $directory.'/', $class, $method);
+ $this->assertEquals($expect, $this->router->fetch_route());
+ }
+
+ /**
+ * Test setting overrides
+ *
+ * @covers CI_Router::_set_overrides
+ */
+ public function test_set_overrides()
+ {
+ // Load mock Config
+ $this->ci_set_config();
+
+ // Mock up URI
+ $this->_mock_uri();
+
+ // Mock loader with package paths
+ $this->_mock_loader();
+ $alt_dir = 'alternative';
+ $alt_root = $this->ci_vfs_mkdir($alt_dir);
+ $alt_path = $this->ci_vfs_path($alt_dir);
+ $this->ci->load->paths = array($alt_path, $this->ci_app_path);
+
+ // Set up routes config
+ $routes = array('default_controller' => '');
+ $this->ci->config->to_get($routes);
+
+ // Create controller and set segments
+ $ctlr = 'orig_ctlr';
+ $func = 'orig_func';
+ $this->ci_vfs_create($ctlr, '', $this->ci_app_root, 'controllers');
+ $this->ci->uri->segments = array($ctlr, $func);
+
+ // Set routing
+ $this->router->_set_routing();
+
+ // Did the route stack get set correctly?
+ $expect = array($this->ci_app_path, '', $ctlr, $func);
+ $this->assertEquals($expect, $this->router->fetch_route());
+
+ // Set overrides
+ $opath = $alt_path;
+ $odir = 'new_dir';
+ $octlr = 'new_ctlr';
+ $ofunc = 'new_func';
+ $override = array(
+ 'path' => $opath,
+ 'directory' => $odir,
+ 'controller' => $octlr,
+ 'function' => $ofunc
+ );
+ $this->router->_set_overrides($override);
+
+ // Did the route stack get updated?
+ $expect = array($opath, $odir.'/', $octlr, $ofunc);
+ $this->assertEquals($expect, $this->router->fetch_route());
+ }
+
+ /**
+ * Mock up URI
+ */
+ private function _mock_uri()
+ {
+ // Create mock URI class
+ $class = 'Router_URI';
+ if ( ! class_exists($class))
+ {
+ $code = 'class '.$class.' { public $segments = array(); public $rsegments = array(); '.
+ 'public function _filter_uri($arg1) { return $arg1; } '.
+ 'public function _fetch_uri_string() { $this->called[] = __FUNCTION__; } '.
+ 'public function _remove_url_suffix() { $this->called[] = __FUNCTION__; } '.
+ 'public function _explode_segments() { $this->called[] = __FUNCTION__; } '.
+ 'public function _reindex_segments() { $this->called[] = __FUNCTION__; } '.
+ '}';
+ eval($code);
+ }
+
+ // Attach mock URI
+ $this->ci->uri = new $class();
+ }
+
+ /**
+ * Mock up Loader
+ */
+ private function _mock_loader()
+ {
+ // Create VFS tree
+ $this->ci_vfs_setup();
+
+ // Create mock Loader class
+ $class = 'Router_Loader';
+ if ( ! class_exists($class))
+ {
+ eval('class '.$class.' { public $paths; public function get_package_paths() { return $this->paths; } }');
+ }
+ $this->ci->load = new $class();
+ }
+}
+
View
97 tests/mocks/ci_testcase.php
@@ -188,23 +188,43 @@ public function ci_vfs_setup()
// --------------------------------------------------------------------
/**
+ * Create VFS directory
+ *
+ * @param string Directory name
+ * @param object Optional root to create in
+ * @param object New directory object
+ */
+ public function ci_vfs_mkdir($name, $root = NULL)
+ {
+ // Check for root
+ if ( ! $root)
+ {
+ $root = $this->ci_vfs_root;
+ }
+
+ // Return new directory object
+ return vfsStream::newDirectory($name)->at($root);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Create VFS content
*
- * @param string File name
- * @param string File content
- * @param object VFS directory object
- * @param string Optional directory name
- * @param string Optional subdirectory name
- * @return void
+ * @param string File name
+ * @param string File content
+ * @param object VFS directory object
+ * @param mixed Optional subdirectory path or array of subs
+ * @return void
*/
- public function ci_vfs_create($file, $content, $root = NULL, $dir = NULL, $sub = NULL)
+ public function ci_vfs_create($file, $content, $root = NULL, $path = NULL)
{
// Check for array
if (is_array($file))
{
foreach ($file as $name => $content)
{
- $this->ci_vfs_create($name, $content, $root, $dir, $sub);
+ $this->ci_vfs_create($name, $content, $root, $path);
}
return;
}
@@ -218,45 +238,38 @@ public function ci_vfs_create($file, $content, $root = NULL, $dir = NULL, $sub =
// Build content
$tree = array($file => $content);
- // Check for directory
- if ($dir)
+ // Check for path
+ $subs = array();
+ if ($path)
+ {
+ // Explode if not array
+ $subs = is_array($path) ? $path : explode('/', trim($path, '/'));
+ }
+
+ // Handle subdirectories
+ while (($dir = array_shift($subs)))
{
+ // See if subdir exists under current root
$dir_root = $root->getChild($dir);
if ($dir_root)
{
- // Directory exists - have sub?
- if ($sub)
- {
- // Check for sub
- $sub_root = $dir_root->getChild($sub);
- if ($sub_root)
- {
- // Exists - build under sub
- $root = $sub_root;
- }
- else
- {
- // None - build sub under dir
- $root = $dir_root;
- $tree = array($sub => $tree);
- }
- }
- else
- {
- // Build under dir
- $root = $dir_root;
- }
+ // Yes - recurse into subdir
+ $root = $dir_root;
}
else
{
- // Directory doesn't exist - have sub?
- if ($sub)
- {
- // Build content in sub
- $tree = array($sub => $tree);
- }
-
- // Build dir with content
+ // No - put subdirectory back and quit
+ array_unshift($subs, $dir);
+ break;
+ }
+ }
+
+ // Create any remaining subdirectories
+ if ($subs)
+ {
+ foreach (array_reverse($subs) as $dir)
+ {
+ // Wrap content in subdirectory for creation
$tree = array($dir => $tree);
}
}
@@ -274,8 +287,8 @@ public function ci_vfs_create($file, $content, $root = NULL, $dir = NULL, $sub =
*/
public function ci_vfs_path($path)
{
- // Remove leading slashes and return URL
- return vfsStream::url(ltrim($path, '/'));
+ // Trim slashes, add trailing slash, and return URL
+ return vfsStream::url(trim($path, '/').'/');
}
// --------------------------------------------------------------------
View
18 tests/mocks/core/router.php
@@ -0,0 +1,18 @@
+<?php
+
+class Mock_Core_Router extends CI_Router {
+
+ /**
+ * Get the default controller
+ */
+ public function default_ctlr($default = NULL)
+ {
+ if ($default)
+ {
+ $this->default_controller = $default;
+ return;
+ }
+ return $this->default_controller;
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.