Testing Codeigniter applications with PHPUnit08 Nov 2013
Codeigniter had its glory days but becomes useless with time. On the other hand, there are some legacy systems it would be really nice if I could test’em (avoiding errors in production with upgrades and all). Codeigniter has its own unit testing library but I prefer PHPUnit.
With the unpacked and working Codeigniter, create a new composer.json file with the following content:
Install PHPUnit on the project with the command:
It’ll create a vendor folder with all the necessary packages for PHPUnit, including its binary under /vendor/bin/. Now if you run:
you’ll see that PHPUnit works and shows its default help message.
Here we’ll create the PHPUnit config file for the project. Under the main folder, create a new phpunit.xml.dist file containing:
With PHPUnit you can write a boostrap file to set the test execution environment. For that, we’ll have almost the very same Codeigniter’s index.php saved in /tests/Bootstrap.php:
PHPUnit’s result comes os the terminal with a very pragmatic output. With that in mind, we’ll have to suppress the Codeigniter’s code output so when tests run we can understand what PHPUnit tests will tell us. To achieve it, we’ll use Codeigniter hooks.
First, change the /application/config/config.php file turning hooks on for the application:
Later in /application/config/hooks.php refer to the hook we’ll use:
Lastly we’ll create the /application/hooks/DisplayHook.php hook:
PHPUnit has some incompatibilities with Codeigniter that we will have to handle. Change the framework’s core libraries is a very bad practice, so we’ll need to overwrite some Codeigniter libraries by copying to the /application/core/ folder with the _MY__ prefix on the file name. The first class will be the Utf8.php one.
cp /system/core/Utf8.php /application/core/MY_Utf8.php
Change the class name and the $CFG variable as follows:
The same thing must be done with the Output.php class:
cp /system/core/Output.php /application/core/MY_Output.php
Change the class name, the $CFG and $BM variables as follows:
Now you’ll be able to run PHPUnit inside your project without errors:
taiar@guestxor:~/dev/ci$ ./vendor/bin/phpunit PHPUnit 4.1.3 by Sebastian Bergmann. Configuration read from /home/taiar/dev/ci/phpunit.xml.dist Time: 100 ms, Memory: 2.25Mb No tests executed!
So let’s write a test using Codeigniter! As stated before, PHPUnit will run tests from /tests/ folder. So we create the /tests/CITest.php file.
Run for a successful test:
taiar@guestxor:~/dev/ci$ ./vendor/bin/phpunit PHPUnit 4.1.3 by Sebastian Bergmann. Configuration read from /home/taiar/dev/ci/phpunit.xml.dist . Time: 130 ms, Memory: 3.50Mb OK (1 test, 1 assertion)
In this article I’ll not look into tests. Only the configuration of this environment given the initial motivation. Improvements on the tests execution would be done by mapping the Codeigniter error output to exceptions instead of the default html output and other issues. I don’t try any of this suggestions yet (it’s a little hard to be motivated when dealing with legacies).