UPDATED: To get the nice reports from PHPUnit, the XDebug package has to be installed. I've modified the steps below to reflect this.
I'm diving into Test Driven Development. To do so with Zend Framework means making use of PHPUnit. However, the version of PHPUnit that is installable via "apt-get install phpunit" on Ubuntu Jaunty Jackolope (or version 9.04) is a little old (3.2.16, when the latest stable version seems to be 3.3.17). Here's how I resolved this.
A quick aside for any who do not know what unit testing is. As a developer, we write code, test to make sure it works, then move on to the next coding task. But sometimes a later change may be needed to a seemingly unrelated part of the code that ends up breaking this part that we assume to have been tested already. To resolve this, a quick, easy, and automated method to test parts of the code was needed. There have a been a few different techniques created for this, but Unit Testing has become popular, specifically via xUnit (which is just a fancy way to say language specific implementations of a common unit testing methodology). Unit testing allows us to issue a single command, and apply all the defined tests to the target code, with a output and/or a nice report showing what passed and what failed. The points that failed obviously need attention. Which means that we can run our unit tests many times throughout the development of an application to make sure new code doesn't break older code.
I know that explanation is not really sufficient, but if you follow the links, it'll become clearer. The topic is worthy of much longer posts, and books have been written to cover it.
Ok, onto the "fix".
Getting the latest version of PHPUnit installed takes a few steps. But these are mostly simple copy/paste type commands.
- First, fire up a command prompt. (I'm running the KUbuntu desktop, and prefer to run Konsole - usually via ALT-F2. But "xterm" works just as well.)
- Next take a look at The official install guide. If you already have PEAR installed, you can just follow the instructions there. The rest of the post isn't really pertinent in that case. But, if you did a typical install of PHP via "apt-get install php5", then you probably do not have PEAR installed. In that case, read on...
- Install PEAR (if needed). Issue the command sudo apt-get install php-pear.
- Make sure PEAR is up to date. Issue the command sudo pear channel-update pear.php.net. This updates some of the protocols PEAR uses.
- Upgrade the PEAR elements. Issue the command sudo pear upgrade-all.
- Tell PEAR where to find the PHPUnit code. Issue the command sudo pear channel-discover pear.phpunit.de
- Install PHPUnit. Issue the command sudo pear install -a phpunit/PHPUnit. The -a makes sure all dependency packages are also installed.
- Finally, confirm the version. Issue the command phpunit --version. You should have version 3.3.17 or newer installed.
- Install XDebug. Issue the command sudo apt-get install php5-xdebug. This is needed if you want the HTML based coverage reports. Remember to restart the web server after installing this package. (/etc/init.d/apache restart or similar on most Linux/Unix based systems)
If all goes well, you can now proceed with setting up your unit testing. There is a good video at ZendCasts to get you started. And I found a good guide at http://maff.ailoo.net detailing how to take the basic Zend Framework 1.8.x application generated by zftool and get it running WITH unit testing even. A Google Search will also reveal a number of tutorials for PHPUnit.
For those curious what triggered this post... When setting up PHPUnit, the method mentioned in the video linked to above is to make use of a phpunit.xml file to set the configuration options for your application's testing. The very first thing you do with the phpunit.xml file is to define a "phpunit" xml tag, with an attribute of bootstrap="something". i.e.
<phpunit bootstrap="./application/bootstrap.php">
This bootstrap reference is where you set up your include paths and anything else you may need for your code to work properly. Those familiar with Zend Framework will recognize the idea here - but it's specifically a command line thing, so the standard ZF bootstrap routines don't quite apply 100%.
The problem I ran into was that the bootstrap.php file was NEVER being executed. If the bootstrap file did not execute, all other code would fail. After digging around for an hour or so, I found that this was a known bug that was fixed with version 3.3.6. From there it was a matter of finding how to get the right version installed. And this post is the result of that effort.