PHPUnit on Central

For those of you working on the 2nd year group project, you may have been asked to think about testing of the server side portion of your system.  A number of groups have chosen PHP as the language for this portion.

In year 1, we looked at the use of JUnit for unit testing of Java systems. We talked about unit testing as one part of the process. We also mentioned that unit testing frameworks are available for a lot of languages. PHP is no exception, which has the PHPUnit framework.

PHPUnit can be used on your own development machines. Some groups have asked if it is also available on central, the IS machine you have access to for accessing your public_html directory. PHPUnit isn’t installed, however it is fairly easy to setup.


Go to PHPUnit’s homepage and download the latest stable release of the phar file. This is version 3.7.29 at the time of writing this post.

Running a test

In the PHPUnit documentation for installation, you are told that you can type the following to check the version of the system.

php phpunit.phar -v

However, that will generate an error, shown below; scroll right for the full message. Actually, the full message is repeated, but it contains HTML markup.

PHP Catchable fatal error: Argument 1 passed to PHPUnit_TextUI_Command::run() must be of the type array, null given, called in phar:///longpath/public_html/cs22120/phpunit.phar/phpunit/TextUI/Command.php on line 129 and defined in phar:///longpath/public_html/cs22120/phpunit.phar/phpunit/TextUI/Command.php on line 136

Hmm. That was a little unusual. After some help from the PHPUnit user email list, it was evident that I was using the wrong mode to run PHP. The default version of PHP that is found on central is designed to be run in CGI mode. However, we need to run PHP in a command line mode, referred to as CLI.

Thankfully, we can access the correct version on central. If you look at the binary that is found for php, you will see it is found in /usr/local/bin/php. If you run php -v on that version, you can see that it is a (cgi-fcgi) version. After a bit of looking around, it seems that there is another version available in /usr/bin/php. If you run that version with the -v option, you will see that it is the (cli) build.

Therefore, if you had a test class called StackTest (stored in the file StackTest.php) then you can run it on central by running:

/usr/bin/php phpunit.phar StackTest

The above information is particular to one of our IS servers. If you are trying to test with PHP on the CompSci teaching servers, you will see that the version of php in your path is the CLI version.  If you are running on a different machine and you see the same issue, then find out if you have a compiled version of the CLI version of php.

Just to note, PHPUnit isn’t a formal requirement of the group project. However, it could be a good addition to your test strategy. Something to consider as you enter Integration and Testing Week.