Skip to content
Integration of PHPUnit 3.5.x with CodeIgniter 2.x using a clean way.
PHP JavaScript
Find file
Pull request Compare This branch is 3 commits ahead, 6305 commits behind bcit-ci:develop.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
application
schema
system
tests Added missing PHPUnit bootstrap class and configuration file. Sep 4, 2011
user_guide
.gitignore
index.php Ability to move the view folder out of the Application directory Aug 21, 2011
license.txt
readme.md

readme.md

PHPUnit integration with CodeIgniter

This is an attempt of integrating PHPUnit with CodeIgniter. This provides a clean way of bootstrapping the framework and then performing tests on the Model layer. The use is very simple as it does not require any change in how a regular CodeIgniter application is built.

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.

  • A 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

Example

<?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));
    }
}
Something went wrong with that request. Please try again.