This blog - now archived - is read-only.
New blog: Mohammad Emran Hasan.

A clean way of integrating PHPUnit 3.5.x with CodeIgniter 2.x

Now a days, my default choice of framework is always Zend Framework. However, I have to maintain a couple live projects in CodeIgniter from early days. I feel its very important to have tests around critical applications, so I have attempted a couple times to integrate PHPUnit with CodeIgniter but failed every time – well, until now.

I’ve managed it this time with hooks. It provides a clean way of bootstrapping the framework and then performing tests on the Model layer – for me testing the model layer has been sufficient. The use is very simple as it does not require any change in how a regular CodeIgniter application is built.

Grab the code from github.

Example – /tests/PostTest.php

<?php

class PostTest extends PHPUnit_Framework_TestCase
{
    private $CI;

    public function setUp()
    {
        $this->CI = &get_instance();
        $this->CI->load->database('testing');
    }

    public function testGetsAllPosts()
    {
        $this->CI->load->model('post');
        $posts = $this->CI->post->getAll();
        $this->assertEquals(1, count($posts));
    }
}

How it works

  • The provided PHPUnit bootstrap file sets the CI environment as testing and then loads the framework normally – the code is taken directly from the index.php file.
  • display_override hook checks if the environment is set to testing or not and when it is, it refrains from outputting the rendered view file
  • The PHPUnit test case file now can get a reference of the CI object using the commonly used &get_instance() method and can load models and other libraries as needed.
  • It is good to have separate database configuration for testing and it might be useful to load the test database with fresh data every time test runs – it can be easily added in the setUp method using the Database Forge classes

Let me know what you think!

12 thoughts on “A clean way of integrating PHPUnit 3.5.x with CodeIgniter 2.x

  1. Stephan Jusypiw

    Hi,

    Thanks for your post. I am interested in integrating PHPUnit 3.5 with CI V2. Can you answer a few questions for me?

    1. Where do you load the PHPUnit library?
    2. What is the URL address to run the test in your example?

    Thanks

    Stephan

  2. Petr

    Can I somehow run a certain test file? I’ve tried to run it:
    phpunit PostTest.php
    but CI tells me about disallowed characters in the URI. Generally I want to use PHPUnit plugin for my IDE (NetBeans).

  3. shaded2

    Thanks for sharing, I’ve tried to get this working before but failed. Did you manage to get the xdebug code coverage working?

  4. shaded2

    OK so i have everything working from a clone of your repo, however when i add your changes to my repo and i run phpunit, nothing happens. No errors, no output at all. It just executes and ends.

    Any idea what to do here, I’m kinda stuck since there is nothing to go on? I, hoping you’ve run into this before.

    Thanks.

    1. Emran Hasan Post author

      Not sure why…can you try debugging it using Xdebug ? Or maybe run a debug_backtrace to see the execution path?

  5. Dish

    This is a very useful article you have published, as I have been searching a way to integrate PHPUnit with CI. I have copped you code from GitHub and try to run the unit test but it complained following error
    Fatal error: Class ‘CI_Model’ not found. Any idea to resolve this issue as you may have ran into this issue before.

    Thanks.