CodeIgniter Forums

Full Version: Here's a patch allowing to subclass CI_Loader to be able to create mock loader for unit testing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum


I hope this is the appropriate place to post patches to CodeIgniter code. If not, please redirect me.

I'm writing an application using CI, and I started adding unit tests. Since CI unit test support is very rudimentary, I'm using Toast (, which seems a very nice addition and I hope it will be included in mainstream CI distribution package one day.

I have a lot of controllers to test. As expected, my controllers are stateless, everything is either stored into session, model or sent to a view. Creating a mock model or a session object for unit testing is trivial and that works very well. However, creating mock views is a problem. Why do I need the views? Well, I wish to see what is the result of controller's function, i.e. what variables are set for the view to display. It seems that easiest way to do this is to create a mock loader (derived from CI_Loader) and override the view() function to grasp the view name and data sent to it.

The problem I had is that Toast itself uses CI, so I cannot just load CI classes at will. In fact, I could, but load_class code in Common.php uses "require" instead of "require_once". Here is a patch to fix this issue:

diff --git a/system/codeigniter/Common.php b/system/codeigniter/Common.php
--- a/system/codeigniter/Common.php
+++ b/system/codeigniter/Common.php
@@ -100,20 +100,20 @@ function &load;_class($class, $instantiate = TRUE)
        // folder we'll load the native class from the system/libraries folder.
        if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
-               require(BASEPATH.'libraries/'.$class.EXT);
-               require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
+               require_once(BASEPATH.'libraries/'.$class.EXT);
+               require_once(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
                $is_subclass = TRUE;
                if (file_exists(APPPATH.'libraries/'.$class.EXT))
-                       require(APPPATH.'libraries/'.$class.EXT);
+                       require_once(APPPATH.'libraries/'.$class.EXT);
                        $is_subclass = FALSE;
-                       require(BASEPATH.'libraries/'.$class.EXT);
+                       require_once(BASEPATH.'libraries/'.$class.EXT);
                        $is_subclass = FALSE;

Thanks for a great framework and keep up the good work.