How to use PHPUnit (CIUnit) with CodeIgniter 2.1.0

STOP SOPA

(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

CodeIgniter/
  application/
    third_party/
      CIUnit/ ... CIUnit itself
  tests/
    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
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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()
	{
		parent::__construct();
		$this->load->database();
	}
	
	function getCarriers(array $attributes)
	{
		foreach ($attributes as $field)
		{
			$this->db->select($field)->from('phone_carrier');
			$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