Welcome to Abdul Malik Ikhsan's Blog

Testing CodeIgniter 4 application with kahlan ^3.0

Posted in CodeIgniter 4, Tutorial PHP by samsonasik on January 14, 2017

This is another post series about CodeIgniter and Kahlan, but now, it will uses CodeIgniter 4. CodeIgniter 4 requires some constants and required classes and functions to be included during application bootstrap to make front controller works.
For example, we need to do functional test App\Controllers\Home controller, this is the kahlan-config.php we need to have:

<?php

use CodeIgniter\CodeIgniter;
use CodeIgniter\Services;
use Config\App;
use Config\Autoload;
use Kahlan\Filter\Filter;

Filter::register('ci.start', function($chain) {
    $root = $this->suite();
    $root->beforeAll(function ($var) {

        define('ENVIRONMENT', 'testing');
        define('BASEPATH',    'system'      . DIRECTORY_SEPARATOR);
        define('APPPATH',     'application' . DIRECTORY_SEPARATOR);
        define('ROOTPATH',    'public'      . DIRECTORY_SEPARATOR);
        define('WRITEPATH',   'writable'    . DIRECTORY_SEPARATOR);
        define('CI_DEBUG',    1);

        require BASEPATH . 'Autoloader/Autoloader.php';
        require APPPATH  . 'Config/Constants.php';
        require APPPATH  . 'Config/Autoload.php';
        require APPPATH  . 'Config/Services.php';

        class_alias('Config\Services', 'CodeIgniter\Services');

        $loader = Services::autoloader();
        $loader->initialize(new Autoload());
        $loader->register();

        require BASEPATH . 'Common.php';
        $app = new App();
        Services::exceptions($app, true)->initialize();

        $var->codeIgniter = new CodeIgniter(
            $app
        );

    });
    return $chain->next();
});
Filter::apply($this, 'run', 'ci.start');

We now can call $this->codeIgniter property in all entire tests.

We can then place the spec under spec/ directory:

.
├── DCO.txt
├── README.md
├── application
│   ├── Controllers
│   │   └── Home.php
├── spec
│   └── Controllers
│       └── HomeDispatchSpec.php

We can apply $_SESSION['argv'] and $_SESSION['argc'] to assign URI routing data:

$_SERVER['argv'] = [
    __FILE__,
    '/', // path
];
$_SERVER['argc'] = 2;

ob_start();
$this->codeIgniter->run();
$actual = ob_get_clean();

expect($actual)->toContain('Welcome to CodeIgniter');

Here is the complete tests:

<?php

namespace AppSpec\Controllers;

describe('Home Dispatch', function () {

    describe('/', function () {

        it('contains "welcome" greeting', function () {

            $_SERVER['argv'] = [
                __FILE__,
                '/', // path
            ];
            $_SERVER['argc'] = 2;

            ob_start();
            $this->codeIgniter->run();
            $actual = ob_get_clean();

            expect($actual)->toContain('Welcome to CodeIgniter');

        });

    });

});

Time to run test:

$ vendor/bin/kahlan --coverage=4 --src=application/Controllers/Home.php
            _     _
  /\ /\__ _| |__ | | __ _ _ __
 / //_/ _` | '_ \| |/ _` | '_ \
/ __ \ (_| | | | | | (_| | | | |
\/  \/\__,_|_| |_|_|\__,_|_| | |

The PHP Test Framework for Freedom, Truth and Justice.

Working Directory: /Users/samsonasik/www/CodeIgniter4

.                                                                   1 / 1 (100%)



Expectations   : 1 Executed
Specifications : 0 Pending, 0 Excluded, 0 Skipped

Passed 1 of 1 PASS in 0.120 seconds (using 4Mo)

Coverage Summary
----------------
                               Lines           %

 \                             1 / 1     100.00%
└── App\                       1 / 1     100.00%
   └── Controllers\            1 / 1     100.00%
      └── Home                 1 / 1     100.00%
         └── Home::index()     1 / 1     100.00%

Total: 100.00% (1/1)

Coverage collected in 0.003 seconds (using an additionnal 16Ko)

Done 😉

Tagged with: ,

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: