Welcome to Abdul Malik Ikhsan's Blog

Testing CodeIgniter application with Kahlan 3.0

Posted in testing, Tutorial PHP by samsonasik on October 19, 2016

Really? Yes, it is testable with kahlan – The PHP Test Framework for Freedom, Truth, and Justice -. Let’s give a try, I am using CodeIgniter 3.1.0 for this example. You can download CodeIgniter from codeigniter.com . For example, we are going to test its Welcome controller.

Setup:
a. require kahlan/kahlan

composer require --dev kahlan/kahlan:^3.0

b. setup minimal autoloading in kahlan-config.php in root CodeIgniter project with Kahlan\Filter\Filter::register() to register its autoloader:

<?php
// ./kahlan.config.php
use Kahlan\Filter\Filter;

define('CI_VERSION', '3.1.0');
define('ENVIRONMENT', 'development');
define('APPPATH', 'application/');
define('VIEWPATH', 'application/views/');
define('BASEPATH', 'system/');

require_once BASEPATH . 'core/Common.php';
function &get_instance()
{
    return CI_Controller::get_instance();
}

Filter::register('ci.autoloader', function($chain) {
    $this->autoloader()->addClassMap([
        // core
        'CI_Controller' =>  BASEPATH . 'core/Controller.php',
        
        // controllers
        'Welcome' => APPPATH . 'controllers/Welcome.php',
    ]);
    return $chain->next();
});
Filter::apply($this, 'namespaces', 'ci.autoloader');

c. Define the spec, we can create spec/controllers directory for placing controller spec:

application/
spec/
└── controllers
    └── WelcomeSpec.php
system/
kahlan-config.php

d. Write the spec:

<?php

describe('Welcome', function () {
    
    describe('->index()', function () {
        
        it('contains welcome message', function() {
            
            $controller = new Welcome();
            
            ob_start();
            $controller->index();
            $actual = ob_get_clean();
            
            expect($actual)->toContain('Welcome to CodeIgniter!');
            
        });
        
    });
    
});

e. run the kahlan command

vendor/bin/kahlan  --coverage=4 --src=application/

and you will get the following output:
kahlan-ci-output

What If we load model into controller ? How to test ?
We can also, For example, you have a model named Welcome_model which check what passed name that will be used in controller:

<?php
// application/models/Welcome_model.php
class Welcome_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function greeting($name)
    {
        if (trim($name) === '') {
            return 'Hello Guest';
        }
        
        return 'Hello ' . $name;
    }
}

As we need to check uri segment, we need to register new route in application/config/routes.php:

$route['welcome/:name'] = 'welcome/index';

And now, we load in controller:

<?php
// application/controllers/Welcome.php

class Welcome extends CI_Controller
{	
    public function __construct()
    {
         parent::__construct();
		
	    $this->load->model('Welcome_model', 'welcome');
    }
	
    public function index()
    {
	    $greeting = $this->welcome->greeting($this->uri->segment(3));	
	    $this->load->view('welcome_message', ['greeting' => $greeting]);
    }
}

On view ( application/views/welcome_message.php ), we modify the greeting:

<?php // application/views/welcome_message.php ?>
<h1><?php echo $greeting; ?>, Welcome to CodeIgniter!</h1>

At this case, we need a CI_URI::segment() and Welcome_model::greeting() to be stubbed in the spec, so, we need to modify our kahlan-config.php to register CI_URI, CI_Model and its Welcome_model classes:

<?php
// ./kahlan-config.php
use Kahlan\Filter\Filter;

define('CI_VERSION', '3.1.0');
define('ENVIRONMENT', 'development');
define('APPPATH', 'application/');
define('VIEWPATH', 'application/views/');
define('BASEPATH', 'system/');

require_once BASEPATH . 'core/Common.php';
function &get_instance()
{
    return CI_Controller::get_instance();
}

Filter::register('ci.autoloader', function($chain) {
    $this->autoloader()->addClassMap([
        // core
        'CI_Controller' =>  BASEPATH . 'core/Controller.php',
        'CI_URI' =>  BASEPATH . 'core/URI.php',
        'CI_Model' => BASEPATH . 'core/Model.php',
        
        // controllers
        'Welcome' => APPPATH . 'controllers/Welcome.php',
        
        // models
        'Welcome_model' => APPPATH . 'models/Welcome_model.php',
    ]);
    return $chain->next();
});
Filter::apply($this, 'namespaces', 'ci.autoloader');

Then, here is the spec we will need to have:

<?php

use Kahlan\Plugin\Double;

describe('Welcome', function () {
     
    describe('->index()', function () {
         
        it('contains welcome message to specific passed name parameter', function() {
            
            define('UTF8_ENABLED', TRUE); // used by CI_Uri
            
            allow('is_cli')->toBeCalled()->andReturn(false); // to disable _parse_argv call
            
            //  stubs CI_Uri::segment() 
            $uri = Double::instance(['extends' => 'CI_URI']);
            allow($uri)->toReceive('segment')->with(3)->andReturn('samsonasik');
            
            // stubs Welcome_model::greeting()              
            $welcome_model = Double::instance(['extends' => 'Welcome_model']);
            allow($welcome_model)->toReceive('greeting')
                                 ->with('samsonasik')
                                 ->andReturn('Hello samsonasik');
                         
            $controller = new Welcome();
            $controller->uri = $uri;
            $controller->welcome = $welcome_model;
             
            ob_start();
            $controller->index();
            $actual = ob_get_clean();
             
            expect($actual)->toContain('Hello samsonasik, Welcome to CodeIgniter!');
             
        });
         
    });
     
});

As we are stubbing Welcome_model::greeting(), here is the expected output that will be shown on run test:
kahlan-ci-output-with-model-load-in-controller-test.
If we want to make Welcome_model::greeting() coverable, we can create a new spec for testing real Welcome_model::greeting() call.

You wanna grab full sample? I created a repository for it so you can try: https://github.com/samsonasik/ci_310_with_kahlan 😉

Done 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: