Getting started with PHPUnit, Codeigniter

Install PHPUnit with XAMPP

xampp_with_phpunit

1) To use PHPUnit you need PEAR package. So install latest PEAR package and run the PEAR installer.

Download the package from http://pear.php.net/go-pear.phar and keep it in your E:\xampp\php folder

cd \xampp\php
php go-pear.phar

2) Run the E:\xampp\php\PEAR_ENV.reg file to import your PEAR environment variable settings.

3) Restart apache server. This will load the new environment variables into memory. Confirm your settings by running:

 pear config-show

It should now read: C:\xampp\php\pear.ini

4) Install PHPUnit & Skeleton Generator through PEAR

pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
pear install phpunit/PHPUnit_SkeletonGenerator

5) Check your version of PHPUnit – It should be at least version

phpunit --version

Configure PHPUnit with Codeigniter

6) Download Codeignator framework and install it. Once installation is done, create a tests folder in Codeignator project root directory and copy & paste the index.php in there and rename it to bootstrap.php.

7) Also create a phpunit.xml file on following path, this file will have the PHPUnit configuration settings for Codeignator and test cases or test suite you want to run.

// tests/phpunit.xml
<phpunit bootstrap="bootstrap.php" 
colors="true" 
convertErrorsToExceptions="true"
convertNoticesToExceptions="true" 
convertWarningsToExceptions="true"
processIsolation="false" 
stopOnFailure="false" 
syntaxCheck="false" 
verbose="true"> 
</phpunit>

8) Add following variables in the bootstrap.php file.

$system_path = '../system';
$application_folder = '../application';

Write First Test Case

9) To write first test case, I am creating a test module “POST”:

<?php // application/models/post.php 
class Post extends CI_Model {     
   public function getAll()
    { 		
           return array( array('title'=>'post 1','content'=>'...'),
			array('title'=>'post 2','content'=>'...'),
			array('title'=>'post 3','content'=>'...'),
			array('title'=>'post 4','content'=>'...'),
			array('title'=>'post 5','content'=>'...'),
		);
    }
}

10) Now create a PostTest.php file in your test folder and add following code:

<?php // tests/PostTest.php 

class PostTest extends PHPUnit_Framework_TestCase { 	

        private $CI; 	
       public function setUp() { 	

	$this->CI = &get_instance();
	}

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

11) Let’s try to run our test from the command line. Navigate to the tests folder and execute following commands:

cd E:\xampplite\htdocs\CodeIgniter_2.1.3\tests
phpunit .

We should get the following error:

Fatal error: Call to a member function item() on a non-object in E:\xampplite\htdocs\CodeIgniter_2.1.3\system\core\Utf8.php on line 47

It’s because we still need to change few things in Codeigniter. Open up the system/core/Utf8.php file and goto line 41, the $CFG variable is marked as global and this is causing issues with PHPUnit. Replace that line by loading the class via CodeIgniter like so:

$CFG =& load_class('Config', 'core');

12) Run the test again. If you are still working in Codeigniter development or live environment you will face another error. To remove the error change your environment to development in config file:

//Change the environment on line 21
define('ENVIRONMENT', 'testing');

//Enable hooks in the application/config/config.php file on line 94
$config['enable_hooks'] = TRUE;

Add the following to the application/config/hooks.php file:

// application/config/hooks.php
$hook['display_override'] = array(
	'class' => 'DisplayHook',
	'function' => 'captureOutput',
	'filename' => 'DisplayHook.php',
	'filepath' => 'hooks'
);

13) Create a DisplayHook.php file in the application/hooks folder and enter the following code:

<?php // application/hooks/DisplayHook.php 

class DisplayHook { 	
public function captureOutput() { 	
  	        $this->CI =& get_instance();
		$output = $this->CI->output->get_output();
		if (ENVIRONMENT != 'testing') {
			echo $output;
		}
	}
}

This checks the current ENVIRONMENT and only outputs data if we’re not in testing, as we’ve specified this environment in our bootstrap file normal output wont be seen. Go back to the console and run the tests again and if everything has gone smoothly you should see the following:

C:\xampplite\htdocs\CodeIgniter_2.1.3\tests>phpunit .
PHPUnit 3.6.11 by Sebastian Bergmann.

Configuration read from C:\xampplite\htdocs\CodeIgniter_2.1.3\tests\phpunit.xml

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 1 assertion)