2012-01-17
How to use PHPUnit (CIUnit) with CodeIgniter 2.1.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.
- CIUnit Official Site http://www.foostack.com/foostack/
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
Related
購入: 1人 クリック: 1回
- 2415 http://codeigniter.com/forums/viewthread/208731/
- 1772 http://ellislab.com/forums/viewthread/208731/
- 98 https://www.google.com/
- 71 http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDYQFjAB&url=http://d.hatena.ne.jp/Kenji_s/20120117/1326763908&ei=6xF6T8WTJ5DAgQfFw7nwDg&usg=AFQjCNF7Bq3PDsCDe9t19YOGP4TBAmezMQ
- 54 http://codeigniter.org.cn/forums/thread-12205-1-1.html
- 50 http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CDwQFjAA&url=http://d.hatena.ne.jp/Kenji_s/20120117/1326763908&ei=qZuET5HzBcL_ggeP2sCrBw&usg=AFQjCNF7Bq3PDsCDe9t19YOGP4TBAmezMQ&sig2=ki1mjkNT-HD_SvreCfuXlw
- 40 http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CD0QFjAC&url=http://d.hatena.ne.jp/Kenji_s/20120117/1326763908&ei=SpuIT4zfFMqQ0QGKkoX7CQ&usg=AFQjCNF7Bq3PDsCDe9t19YOGP4TBAmezMQ&sig2=a0IPny3OC2rcbsRMJ0N0Wg
- 36 http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0CEsQFjAD&url=http://d.hatena.ne.jp/Kenji_s/20120117/1326763908&ei=-7uZT-nxHYu-2AWlnI23Bw&usg=AFQjCNF7Bq3PDsCDe9t19YOGP4TBAmezMQ
- 34 http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=5&ved=0CFsQFjAE&url=http://d.hatena.ne.jp/Kenji_s/20120117/1326763908&ei=FkaWT6KxJKediAfG1uCfCg&usg=AFQjCNF7Bq3PDsCDe9t19YOGP4TBAmezMQ&sig2=yrlNPgUQYlW-ZYMWE_hqYA
- 32 https://www.google.co.jp/



