A Day in Serenity @ kenjis


How to use PHPUnit (CIUnit) with CodeIgniter 2.1.0


(2015/05/14) I'm building a new tool for CodeIgniter 3.0: CI PHPUnit Test for CodeIgniter 3.0

This article explains how to install and use PHPUnit/CIUnit with CodeIgniter 2.1.0.

What is CIUnit?

CIUnit is a bridge between your CodeIgniter application and PHPUnit.

But the official release supports CodeIgniter 1.7.2. So I use the fork of CIUnit (my-ciunit).

Install PHPUnit

$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover components.ez.no
$ sudo pear channel-discover pear.symfony-project.com
$ sudo pear install phpunit/PHPUnit

Install CodeIgniter

$ wget http://downloads.codeigniter.com/reactor/CodeIgniter_2.1.0.zip
$ unzip CodeIgniter_2.1.0.zip

Install CIUnit

Download my-ciunit

The default branch of my-ciunit is now for CodeIgniter 2.1.0.

$ wget https://bitbucket.org/kenjis/my-ciunit/get/default.zip
$ unzip default.zip

If you use CodeIgniter 2.0.3, get "CI 2.0.3" branch.

$ wget https://bitbucket.org/kenjis/my-ciunit/get/CI%202.0.3.zip
$ unzip "CI 2.0.3.zip"
Install my-ciunit by shell script

my-ciunit has a installer shell script.

How to use:

$ tools/install.sh /path/to/CodeIgniter/ [database_name [database_user [database_password [database_host]]]]

Note: The database name for testing must end with "_test".

For example:

$ cd kenjis-my-ciunit-*
$ tools/install.sh ../CodeIgniter_2.1.0/ ciunit_test root password

This script creates database config file for testing, "application/config/testing/database.php".

Install my-ciunit manually

Copy application folder and tests folder in my-ciunit to CodeIgniter top diretory.

$ cd kenjis-my-ciunit-*
$ cp -R application /path/to/CodeIgniter_2.1.0/
$ cp -R tests /path/to/CodeIgniter_2.1.0/

And create database config file for testing, "application/config/testing/database.php".

Note: The database name for testing must end with "_test".

If you use MY_Loader, MY_Output, MY_Session

Change the parent classes to MY_* in:

  • application/third_party/CIUnit/core/CIU_*.php
  • application/third_party/CIUnit/libraries/CIU_*.php

Directory structure of CIUnit

      CIUnit/ ... CIUnit itself
    controllers/ ... tests of controller
    fixtures/    ... fixtures
    helpers/     ... tests of helper
    libs/        ... tests of library
    models/      ... tests of model
    system/      ... tests of system (PHP)

Prepare Database for Testing

The database name for testing must end with "_test". This is the specification of CIUnit.

Create "ciunit_test" database, and create "phone_carrier" table for a sample model test code in CIUnit.

CREATE TABLE IF NOT EXISTS `phone_carrier` (
  `name` varchar(255) NOT NULL,
  `txt_address` varchar(255) NOT NULL,
  `txt_message_length` int(11) NOT NULL

Prepare Model for sample test code

CIUnit has a sample test code for "Phone_carrier_model" model, but has no "Phone_carrier_model" model code.

Create the model.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Phone_carrier_model extends CI_Model {
	function __construct()
	function getCarriers(array $attributes)
		foreach ($attributes as $field)
			$query = $this->db->get();
			foreach ($query->result_array() as $row)
				$data[] = array($field, $row[$field]);
		return $data;

/* End of file phone_carrier_model.php */
/* Location: ./application/models/phone_carrier_model.php */

Run tests

Before run phpunit, move to tests folder.

$ cd tests/

To run all tests,

$ phpunit

To run tests of model, specify a folder,

$ phpunit models

To run a specific test file,

$ phpunit models/PhoneCarrierModelTest.php

If all tests have passed, you'll see green OK below:


Next: Database Testing of CodeIgniter Application with PHPUnit (CIUnit) - A Day in Serenity @ kenjis


CasparCaspar 2012/04/28 13:09 Fantastic summary, and works perfectly, thanks a lot!

PalomaPaloma 2012/09/17 00:55 thank you from spain man!

AndyAndy 2012/11/28 22:00 I don't know who you are. But I will find you, and Thank you!

gallabgallab 2013/02/05 02:15 Thanks a lot for the tutorial. Followed your instructions, works like a charm. Though I need to catch up with phpunit :)

phopho 2013/12/17 07:43 素晴らしい!

oliverusselldevoliverusselldev 2017/03/15 22:32 Nice guide, but you should also mention that before starting Unit Testing you should have a proper directory structure of the file that needs to be tested (source: https://www.cloudways.com/blog/getting-started-with-unit-testing-php/ ). This is a requirement for unit testing PHP apps using PHPUnit.