Permalink
Browse files

Added many more Loader unit tests

Signed-off-by: dchill42 <dchill42@gmail.com>
  • Loading branch information...
1 parent c8f39eb commit 6f7b0b6b6ab319476c002ea69aa7d451bc08edeb @dchill42 committed Sep 9, 2012
Showing with 436 additions and 187 deletions.
  1. +30 −24 system/core/CodeIgniter.php
  2. +110 −113 system/core/Loader.php
  3. +293 −48 tests/codeigniter/core/Loader_test.php
  4. +1 −1 tests/mocks/autoloader.php
  5. +2 −1 tests/mocks/ci_testcase.php
View
54 system/core/CodeIgniter.php
@@ -99,13 +99,6 @@ class CodeIgniter {
protected static $instance = NULL;
/**
- * call_controller return scheme constants
- */
- const RET_SUCCESS = 0;
- const RET_RESULT = 1;
- const RET_OUTPUT = 2;
-
- /**
* Constructor
*
* This constructor is protected in order to force instantiation
@@ -495,12 +488,12 @@ public function is_callable($class, $method)
*
* @param string Class name
* @param string Method
- * @param array Arguments
+ * @param array Optional arguments
* @param string Optional object name
- * @param int Return scheme (RET_SUCCESS|RET_RESULT|RET_OUTPUT)
- * @return mixed Success (TRUE|FALSE), call result, or output
+ * @param bool TRUE to return call result (or NULL if failed)
+ * @return bool TRUE or call result on success, otherwise FALSE or NULL (if $return == TRUE)
*/
- public function call_controller($class, $method, array $args = array(), $name = '', $return = self::RET_SUCCESS)
+ public function call_controller($class, $method, array $args = array(), $name = '', $return = FALSE)
{
// Default name if not provided
if (empty($name))
@@ -512,12 +505,6 @@ public function call_controller($class, $method, array $args = array(), $name =
$ran = FALSE;
$result = NULL;
- // Capture output if requested
- if ($return === self::RET_OUTPUT)
- {
- $this->output->stack_push();
- }
-
// Class must be loaded, and method cannot start with underscore, nor be a member of the base class
if (isset($this->$name) && strncmp($method, '_', 1) != 0 && ! $this->is_callable('CI_Controller', $method))
{
@@ -536,13 +523,32 @@ public function call_controller($class, $method, array $args = array(), $name =
}
}
- // Return according to scheme
- switch ($return)
- {
- case self::RET_RESULT: return $result;
- case self::RET_OUTPUT: return $this->output->stack_pop();
- default: return $ran;
- }
+ // Return result or success status
+ return $return ? $result : $ran;
+ }
+
+ /**
+ * Call a controller method and get its output
+ *
+ * Requires that controller already be loaded, validates method name, and calls
+ * _remap if available.
+ *
+ * @param string Reference to output string
+ * @param string Class name
+ * @param string Method
+ * @param array Optional arguments
+ * @param string Optional object name
+ * @return bool TRUE on success, otherwise FALSE
+ */
+ public function get_controller_output(&$out, $class, $method, array $args = array(), $name = '')
+ {
+ // Capture output and call controller
+ $this->output->stack_push();
+ $result = $this->call_controller($class, $method, $args, $name);
+ $out = $this->output->stack_pop();
+
+ // Return success or failure
+ return $result;
}
/**
View
223 system/core/Loader.php
@@ -241,19 +241,36 @@ public function library($library = '', $params = NULL, $object_name = NULL)
*
* This function lets users load and instantiate (sub)controllers.
*
- * @access public
- * @param string the name of the class
- * @param string name for the controller
- * @param bool FALSE to skip calling controller method
- * @param int Return scheme (RET_SUCCESS|RET_RESULT|RET_OUTPUT)
- * @return mixed Output if $return, TRUE on success, otherwise FALSE
+ * @param mixed Route to controller/method
+ * @param string Optional controller object name
+ * @param bool TRUE to return method result, FALSE to skip calling method
+ * @return bool TRUE or call result on success, otherwise FALSE or NULL (if $call == TRUE)
*/
- public function controller($route, $name = NULL, $call = TRUE, $return = CodeIgniter::RET_SUCCESS)
+ public function controller($route, $name = NULL, $call = NULL)
+ {
+ // Set output flag to be passed
+ $out = FALSE;
+ return $this->controller_output($out, $route, $name, $call);
+ }
+
+ /**
+ * Controller Loader with output capture
+ *
+ * This function lets users load and instantiate (sub)controllers and
+ * return their output as a string.
+ *
+ * @param string Reference to output string
+ * @param mixed Route to controller/method
+ * @param string Optional controller object name
+ * @param bool TRUE to return method result, FALSE to skip calling method
+ * @return bool TRUE or call result on success, otherwise FALSE or NULL (if $call == TRUE)
+ */
+ public function controller_output(&$out, $route, $name = NULL, $call = NULL)
{
// Check for missing class
if (empty($route))
{
- return FALSE;
+ return $call === TRUE ? NULL : FALSE;
}
// Get instance and establish segment stack
@@ -262,7 +279,7 @@ public function controller($route, $name = NULL, $call = TRUE, $return = CodeIgn
// Assume segments have been pre-parsed by CI_Router::validate_route() - make sure there's 4
if (count($route) <= CI_Router::SEG_METHOD)
{
- return FALSE;
+ return $call === TRUE ? NULL : FALSE;
}
}
else
@@ -271,7 +288,7 @@ public function controller($route, $name = NULL, $call = TRUE, $return = CodeIgn
$route = $this->CI->router->validate_route(explode('/', $route));
if ($route === FALSE)
{
- return FALSE;
+ return $call === TRUE ? NULL : FALSE;
}
}
@@ -306,37 +323,11 @@ public function controller($route, $name = NULL, $call = TRUE, $return = CodeIgn
// Load base class(es) if not already done
if ( ! class_exists('CI_Controller'))
{
- // Locate base class
- foreach ($this->_ci_library_paths as $lib_path)
- {
- $file = $lib_path.'core/Controller.php';
- if (file_exists($file))
- {
- // Include class source
- include $file;
- break;
- }
- }
-
- // Check for subclass
- $pre = $this->CI->config->item('subclass_prefix');
- if ( ! empty($pre))
- {
- // Locate subclass
- foreach ($this->_ci_mvc_paths as $mvc_path => $cascade)
- {
- $file = $mvc_path.'core/'.$pre.'Controller.php';
- if (file_exists($file))
- {
- // Include class source
- include($file);
- break;
- }
- }
- }
+ $this->_ci_include('Controller', 'core');
}
// Include source and instantiate object
+ // The Router is responsible for providing a valid path in the route stack
include($path.'controllers/'.$subdir.strtolower($class).'.php');
$classnm = ucfirst($class);
$this->CI->$name = new $classnm();
@@ -345,13 +336,22 @@ public function controller($route, $name = NULL, $call = TRUE, $return = CodeIgn
$this->_ci_controllers[] = $name;
}
- // Call method unless disabled
- if ($call)
+ // Check call and output flags
+ if ($call === FALSE)
{
- return $this->CI->call_controller($class, $method, $route, $name, $return);
+ // Call disabled - return success
+ return TRUE;
+ }
+ else if ($out === FALSE)
+ {
+ // No output - just return result or success status
+ return $this->CI->call_controller($class, $method, $route, $name, (bool)$call);
+ }
+ else
+ {
+ // Get output and return success status
+ return $this->CI->get_controller_output($out, $class, $method, $route, $name);
}
-
- return TRUE;
}
// --------------------------------------------------------------------
@@ -428,34 +428,7 @@ public function model($model, $name = '', $db_conn = FALSE)
// Load base class(es) if not already done
if ( ! class_exists('CI_Model'))
{
- // Locate base class
- foreach ($this->_ci_library_paths as $lib)
- {
- $file = $lib.'core/Model.php';
- if (file_exists($file))
- {
- // Include class source
- include($file);
- break;
- }
- }
-
- // Check for subclass
- $pre = $this->CI->config->item('subclass_prefix');
- if (!empty($pre))
- {
- // Locate subclass
- foreach ($this->_ci_mvc_paths as $lib => $cascade)
- {
- $file = $lib.'core/'.$pre.'Model.php';
- if (file_exists($file))
- {
- // Include class source
- include($file);
- break;
- }
- }
- }
+ $this->_ci_include('Model', 'core');
}
// Search MVC paths for model
@@ -700,47 +673,13 @@ public function helper($helpers = array())
return;
}
- // Is this a helper extension request?
- $file = 'helpers/'.$this->CI->config->item('subclass_prefix').$helper.'.php';
- foreach ($this->_ci_library_paths as $path)
- {
- // Check each path for extension
- $ext_helper = $path.$file;
- if (file_exists($ext_helper))
- {
- // Extension found - require base class
- $base_helper = $this->_ci_base_path.'helpers/'.$helper.'.php';
- if ( ! file_exists($base_helper))
- {
- show_error('Unable to load the requested file: helpers/'.$helper.'.php');
- }
-
- // Include extension followed by base, so extension overrides base functions
- include_once($ext_helper);
- include_once($base_helper);
-
- // Mark as loaded and return
- $this->_ci_helpers[$helper] = TRUE;
- log_message('debug', 'Helper loaded: '.$helper);
- return;
- }
- }
-
- // Try to load the helper
- $file = 'helpers/'.$helper.'.php';
- foreach ($this->_ci_library_paths as $path)
+ // Include helper with any subclass extension
+ if ($this->_ci_include($helper, 'helpers'))
{
- // Check each path for helper
- if (file_exists($path.$file))
- {
- // Include helper
- include_once($path.$file);
-
- // Mark as loaded and return
- $this->_ci_helpers[$helper] = TRUE;
- log_message('debug', 'Helper loaded: '.$helper);
- return;
- }
+ // Mark as loaded and return
+ $this->_ci_helpers[$helper] = TRUE;
+ log_message('debug', 'Helper loaded: '.$helper);
+ return;
}
// Unable to load the helper
@@ -937,7 +876,65 @@ public function remove_package_path($path = '', $remove_config_path = TRUE)
// --------------------------------------------------------------------
/**
- * Loader
+ * Include a file from package paths
+ *
+ * This function includes a prefixed subclass file if found, and its base file
+ *
+ * @param string File name
+ * @param string Search directory
+ * @return void
+ */
+ protected function _ci_include($name, $dir)
+ {
+ // Get subclass prefix and build relative file name
+ $pre = $this->CI->config->item('subclass_prefix');
+ $file = $dir.'/'.$pre.$name.'.php';
+
+ // Search all paths for subclass extension
+ foreach ($this->_ci_library_paths as $path)
+ {
+ // Check each path for extension
+ $path .= $file;
+ if (file_exists($path))
+ {
+ // Extension found - require base file
+ $base = $this->_ci_base_path.$dir.'/'.$name.'.php';
+ if ( ! file_exists($base))
+ {
+ // No base for extension found
+ return FALSE;
+ }
+
+ // Include extension followed by base, so extension overrides base functions
+ // If this is for a base class, the order won't matter
+ include_once($path);
+ include_once($base);
+ return TRUE;
+ }
+ }
+
+ // Search all paths for the regular file
+ $file = $dir.'/'.$name.'.php';
+ foreach ($this->_ci_library_paths as $path)
+ {
+ // Check each path for base
+ $path .= $file;
+ if (file_exists($path))
+ {
+ // Include file
+ include_once($path);
+ return TRUE;
+ }
+ }
+
+ // File not found
+ return FALSE;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * File/View Loader
*
* This function is used to load views and files.
* Variables are prefixed with _ci_ to avoid symbol collision with
@@ -1168,7 +1165,7 @@ protected function _ci_load_class($class, $params = NULL, $object_name = NULL)
foreach ($this->_ci_library_paths as $path)
{
// Try both upper- and lower-class in path subdirectory
- $path .= 'libraries/'.$subdir;
+ $path .= 'libraries/'.$subdir;
foreach (array(ucfirst($class), strtolower($class)) as $class)
{
// Does the file exist? No? Bummer...
View
341 tests/codeigniter/core/Loader_test.php
@@ -13,15 +13,54 @@ public function set_up()
$this->ci_view_root = vfsStream::newDirectory('views')->at($this->ci_vfs_root);
$this->ci_view_path = vfsStream::url('views/');
- // Set up config
- $this->subclass = 'MY_';
- $this->ci_set_config('subclass_prefix', $this->subclass);
+ // Determine if we need core support methods
+ if (strpos($this->getName(), 'controller') !== FALSE) {
+ // Create CI mockup
+ $class = 'Mock_Loader_CI';
+ if ( ! class_exists($class))
+ {
+ $code = 'class '.$class.' { '.
+ 'public function call_controller($class, $method, $args, $name, $return) '.
+ '{ $this->_ctlr_ran = $method; return $return ? $this->_ctlr_result : TRUE; } '.
+ 'public function get_controller_output(&$out, $class, $method, $args, $name) '.
+ '{ $this->_ctlr_ran = $method; $out = $this->_ctlr_output; return TRUE; } '.
+ '}';
+ eval($code);
+ }
+ $this->ci_obj = new $class();
+
+ // Initialize support flags
+ $this->_ctlr_ran = FALSE;
+ $this->_ctlr_result = TRUE;
+ $this->_ctlr_output = '';
+
+ // Create router mockup
+ $class = 'Mock_Loader_Router';
+ if ( ! class_exists($class))
+ {
+ $code = 'class '.$class.' { const SEG_METHOD = 4; '.
+ 'public function validate_route($route) { return $this->_valid_route; } }';
+ eval($code);
+ }
+ $this->ci_obj->router = new $class();
+
+ // Initialize route to return
+ $this->ci_obj->router->_valid_route = array();
+ }
+ else {
+ // Just get the default instance
+ $this->ci_obj = $this->ci_instance();
+ }
- // Get CI instance and set empty autoload.php contents and path sources
- $this->ci_obj = $this->ci_instance();
+ // Set up CI instance with empty autoload.php contents and path sources
$this->ci_obj->_autoload = array();
$this->ci_obj->base_paths = array($this->ci_app_path, $this->ci_base_path);
$this->ci_obj->app_paths = array($this->ci_app_path);
+ $this->ci_instance($this->ci_obj);
+
+ // Set up config
+ $this->subclass = 'MY_';
+ $this->ci_set_config('subclass_prefix', $this->subclass);
// Instantiate a new loader
$this->load = new Mock_Core_Loader();
@@ -30,7 +69,20 @@ public function set_up()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::library
+ * @covers CI_Loader::__construct
+ * @covers CI_Loader::get_package_paths
+ */
+ public function test_ctor()
+ {
+ // Did we get paths from the core?
+ $this->assertEquals($this->ci_obj->base_paths, $this->load->get_package_paths(TRUE));
+ $this->assertEquals($this->ci_obj->app_paths, $this->load->get_package_paths());
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * @covers CI_Loader::library
*/
public function test_library()
{
@@ -54,7 +106,7 @@ public function test_library()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::library
+ * @covers CI_Loader::library
*/
public function test_library_config()
{
@@ -92,7 +144,7 @@ public function test_library_config()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::library
+ * @covers CI_Loader::library
*/
public function test_load_library_in_application_dir()
{
@@ -123,7 +175,7 @@ public function test_library_subclass()
$this->ci_vfs_create($class, $this->_empty($class), $this->ci_base_root, 'libraries');
// Create library subclass in app path libraries directory
- $content = '<?php class '.$this->subclass.$class.' extends '.$class.' { } ';
+ $content = $this->_empty($this->subclass.$class, $class);
$this->ci_vfs_create($this->subclass.$lib, $content, $this->ci_app_root, 'libraries');
// Load library
@@ -140,7 +192,7 @@ public function test_library_subclass()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::driver
+ * @covers CI_Loader::driver
*/
public function test_driver()
{
@@ -172,43 +224,85 @@ public function test_driver()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::model
+ * @covers CI_Loader::model
*/
- public function test_non_existent_model()
+ public function test_model()
{
- $model = 'ci_test_nonexistent_model.php';
+ // Make sure base class is loaded - we'll test _ci_include later
+ $this->ci_core_class('model');
- $this->setExpectedException(
- 'RuntimeException',
- 'CI Error: Unable to locate the model you have specified: '.$model
- );
- $this->load->model($model);
+ // Create models directory with test model
+ $model = 'unit_test_model';
+ $base = 'CI_Model';
+ $class = ucfirst($model);
+ $this->ci_vfs_create($model, $this->_empty($class, $base), $this->ci_app_root, 'models');
+
+ // Load model as array
+ $this->assertNull($this->load->model(array($model)));
+
+ // Was the model class instantiated?
+ $this->assertTrue(class_exists($class));
+ $this->assertObjectHasAttribute($model, $this->ci_obj);
+ $this->assertAttributeInstanceOf($base, $model, $this->ci_obj);
+ $this->assertAttributeInstanceOf($class, $model, $this->ci_obj);
+
+ // Test no model given
+ $this->assertNull($this->load->model(''));
}
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::model
+ * @covers CI_Loader::model
*/
- public function test_models()
+ public function test_model_subdir()
{
- // Create models directory with test model
- $model = 'unit_test_model';
+ // Make sure base class is loaded - we'll test _ci_include later
+ $this->ci_core_class('model');
+
+ // Create models subdirectory with test model
+ $model = 'test_sub_model';
$base = 'CI_Model';
$class = ucfirst($model);
- $this->ci_vfs_create($model, '<?php class '.$class.' extends '.$base.' {} ', $this->ci_app_root, 'models');
+ $subdir = 'cars';
+ $this->ci_vfs_create($model, $this->_empty($class, $base), $this->ci_app_root, 'models', $subdir);
// Load model
- $this->assertNull($this->load->model($model));
+ $name = 'testors';
+ $this->assertNull($this->load->model($subdir.'/'.$model, $name));
// Was the model class instantiated?
$this->assertTrue(class_exists($class));
- $this->assertObjectHasAttribute($model, $this->ci_obj);
- $this->assertAttributeInstanceOf($base, $model, $this->ci_obj);
- $this->assertAttributeInstanceOf($class, $model, $this->ci_obj);
+ $this->assertObjectHasAttribute($name, $this->ci_obj);
+ $this->assertAttributeInstanceOf($base, $name, $this->ci_obj);
+ $this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
- // Test no model given
- $this->assertNull($this->load->model(''));
+ // Test name conflict
+ $obj = 'conflict';
+ $this->ci_obj->$obj = new StdClass();
+ $this->setExpectedException(
+ 'RuntimeException',
+ 'CI Error: The model name you are loading is the name of a resource that is already being used: '.$obj
+ );
+ $this->load->model('not_real', $obj);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * @covers CI_Loader::model
+ */
+ public function test_non_existent_model()
+ {
+ // Set expected exception
+ $model = 'ci_test_nonexistent_model.php';
+ $this->setExpectedException(
+ 'RuntimeException',
+ 'CI Error: Unable to locate the model you have specified: '.$model
+ );
+
+ // Try to load model
+ $this->load->model($model);
}
// --------------------------------------------------------------------
@@ -222,7 +316,7 @@ public function test_models()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::view
+ * @covers CI_Loader::view
*/
public function test_load_view()
{
@@ -239,28 +333,160 @@ public function test_load_view()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::view
+ * @covers CI_Loader::view
*/
public function test_non_existent_view()
{
+ // Set expected exception
$view = 'ci_test_nonexistent_view';
-
$this->setExpectedException(
'RuntimeException',
'CI Error: Unable to load the requested file: '.$view.'.php'
);
+ // Try to load the view
$this->load->view($view, array('foo' => 'bar'));
}
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::file
+ * @covers CI_Loader::controller
+ */
+ public function test_controller()
+ {
+ // Make sure base class is loaded - we'll test _ci_include later
+ $this->ci_core_class('ctlr');
+
+ // Create controller in VFS
+ $class = 'TestCtlr';
+ $method = 'test_handler';
+ $name = strtolower($class);
+ $this->ci_vfs_create($name, $this->_empty($class), $this->ci_app_root, 'controllers');
+
+ // Create route stack to return
+ $this->ci_obj->router->_valid_route = array(
+ $this->ci_app_path,
+ '',
+ $class,
+ $method
+ );
+
+ // Did the controller get loaded?
+ $this->assertTrue($this->load->controller($class.'/'.$method));
+ $this->assertObjectHasAttribute($name, $this->ci_obj);
+ $this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
+
+ // Did the correct method get called?
+ $this->assertEquals($method, $this->ci_obj->_ctlr_ran);
+
+ // Do we get FALSE for an empty route?
+ $this->assertFalse($this->load->controller(''));
+
+ // Do we get FALSE for an invalid stack?
+ $this->assertFalse($this->load->controller(array('foo', 'bar', 'baz')));
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * @covers CI_Loader::controller
+ */
+ public function test_controller_result()
+ {
+ // Make sure base class is loaded - we'll test _ci_include later
+ $this->ci_core_class('ctlr');
+
+ // Create controller in subdirectory of VFS
+ $subdir = 'special';
+ $class = 'TestResultCtlr';
+ $method = 'result_handler';
+ $this->ci_vfs_create(strtolower($class), $this->_empty($class), $this->ci_app_root, 'controllers', $subdir);
+
+ // Create route stack to pass
+ $route = array(
+ $this->ci_app_path,
+ $subdir.'/',
+ $class,
+ $method
+ );
+
+ // Set object name and method result
+ $name = 'flock';
+ $result = 'I ran (so far away)';
+ $this->ci_obj->_ctlr_result = $result;
+
+ // Was the result returned?
+ $this->assertEquals($result, $this->load->controller($route, $name, TRUE));
+
+ // Did the controller get loaded?
+ $this->assertObjectHasAttribute($name, $this->ci_obj);
+ $this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
+
+ // Did the correct method get called?
+ $this->assertEquals($method, $this->ci_obj->_ctlr_ran);
+
+ // Does it skip the call if we tell it to?
+ $route[3] = 'not_a_handler';
+ $this->ci_obj->_ctlr_ran = FALSE;
+ $this->assertTrue($this->load->controller($route, $name, FALSE));
+ $this->assertFalse($this->ci_obj->_ctlr_ran);
+
+ // Do we get NULL as a result for an empty route?
+ $this->assertNull($this->load->controller('', NULL, TRUE));
+
+ // Do we get NULL as a result for an invalid stack?
+ $this->assertNull($this->load->controller(array('foo', 'bar', 'baz'), NULL, TRUE));
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * @covers CI_Loader::controller
+ */
+ public function test_controller_output()
+ {
+ // Make sure base class is loaded - we'll test _ci_include later
+ $this->ci_core_class('ctlr');
+
+ // Create controller in VFS
+ $class = 'TestOutCtlr';
+ $method = 'output_handler';
+ $name = strtolower($class);
+ $this->ci_vfs_create($name, $this->_empty($class), $this->ci_app_root, 'controllers');
+
+ // Create route stack to pass
+ $route = array(
+ $this->ci_app_path,
+ '',
+ $class,
+ $method
+ );
+
+ // Create output to generate
+ $expected = 'Don\'t panic.';
+ $this->ci_obj->_ctlr_output = $expected;
+
+ // Did the controller get loaded?
+ $this->assertTrue($this->load->controller_output($out, $route));
+ $this->assertObjectHasAttribute($name, $this->ci_obj);
+ $this->assertAttributeInstanceOf($class, $name, $this->ci_obj);
+
+ // Did the correct method get called?
+ $this->assertEquals($method, $this->ci_obj->_ctlr_ran);
+
+ // Was the output returned?
+ $this->assertEquals($expected, $out);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * @covers CI_Loader::file
*/
public function test_file()
{
- // Create views directory with test file
+ // Create subdirectory with test file
$dir = 'views';
$file = 'ci_test_mock_file';
$content = 'Here is a test file, which we will load now.';
@@ -282,7 +508,7 @@ public function test_file()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::vars
+ * @covers CI_Loader::vars
*/
public function test_vars()
{
@@ -293,7 +519,7 @@ public function test_vars()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::helper
+ * @covers CI_Loader::helper
*/
public function test_helper()
{
@@ -319,7 +545,7 @@ public function test_helper()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::helper
+ * @covers CI_Loader::helper
*/
public function test_loading_multiple_helpers()
{
@@ -347,17 +573,30 @@ public function test_loading_multiple_helpers()
// --------------------------------------------------------------------
- // public function testLanguage()
- // {
- // $this->assertEquals(NULL, $this->load->language('test'));
- // }
+ /**
+ * @covers CI_Loader::language
+ */
+ public function test_language()
+ {
+ // Create mock Lang class with load stub
+ $class = 'Mock_Load_Lang';
+ $prop = '_file';
+ eval('class '.$class.' { public function load($file, $lang) { $this->'.$prop.' = $file; } }');
+ $this->ci_instance_var('lang', new $class());
+
+ // Does the right file get loaded?
+ $file = 'test';
+ $this->assertNull($this->load->language($file));
+ $this->assertObjectHasAttribute($prop, $this->ci_obj->lang);
+ $this->assertEquals($file, $this->ci_obj->lang->$prop);
+ }
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::add_package_path
- * @covers CI_Loader::get_package_paths
- * @covers CI_Loader::remove_package_path
+ * @covers CI_Loader::add_package_path
+ * @covers CI_Loader::get_package_paths
+ * @covers CI_Loader::remove_package_path
*/
public function test_packages()
{
@@ -392,7 +631,7 @@ public function test_packages()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::config
+ * @covers CI_Loader::config
*/
public function test_load_config()
{
@@ -402,7 +641,7 @@ public function test_load_config()
// --------------------------------------------------------------------
/**
- * @covers CI_Loader::_ci_autoloader
+ * @covers CI_Loader::_ci_autoloader
*/
public function test_autoloader()
{
@@ -429,7 +668,6 @@ public function test_autoloader()
$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, $dir, 'models');
// Autoload path since autoloaded packages are handled during bootstrapping
$this->load->add_package_path($path);
@@ -454,21 +692,28 @@ public function test_autoloader()
// Verify library
$this->assertTrue(class_exists($lib_class), $lib_class.' does not exist');
+ $this->assertObjectHasAttribute($lib, $this->ci_obj);
$this->assertAttributeInstanceOf($lib_class, $lib, $this->ci_obj);
// Verify driver
$this->assertTrue(class_exists($drv_class), $drv_class.' does not exist');
+ $this->assertObjectHasAttribute($drv, $this->ci_obj);
$this->assertAttributeInstanceOf($drv_class, $drv, $this->ci_obj);
// Verify model
$this->assertTrue(class_exists($mod_class), $mod_class.' does not exist');
+ $this->assertObjectHasAttribute($model, $this->ci_obj);
$this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj);
}
// --------------------------------------------------------------------
- private function _empty($class)
+ private function _empty($class, $base = '')
{
+ if ( ! empty($base))
+ {
+ $class .= ' extends '.$base;
+ }
return '<?php class '.$class.' { } ';
}
View
2 tests/mocks/autoloader.php
@@ -16,7 +16,7 @@ function autoload($class)
$ci_core = array(
'Benchmark', 'Config', 'Controller',
'Exceptions', 'Hooks', 'Input',
- 'Lang', 'Loader', 'Model',
+ 'Lang', 'Loader', 'Log', 'Model',
'Output', 'Router', 'Security',
'URI', 'Utf8',
);
View
3 tests/mocks/ci_testcase.php
@@ -22,7 +22,8 @@ class CI_TestCase extends PHPUnit_Framework_TestCase {
'input' => 'in',
'lang' => 'lang',
'loader' => 'load',
- 'model' => 'model'
+ 'model' => 'model',
+ 'controller'=> 'ctlr'
);
// --------------------------------------------------------------------

0 comments on commit 6f7b0b6

Please sign in to comment.