We start off by setting our environment best fitted for our unit testing. I use a virtual linux system for this, using VMWare, but with some extra background research these global settings can be applied for your own (test) environment as well.
I'm not going to explain how to install PHP here, but I can tell you I'm using PHP 5.3.0 (build from source). With this build, I've installed PHPUnit 3.4.2 and Xdebug 2.0.5. The example application was made using Zend Framework 1.9.5.
First we're creating a new Zend Framework project using Zend_Tool on command line, by issuing the command zf create project zfunit in the workspace directory I use to share my VMWare instance with my workstation.
Ok, we now have a default Zend Framework application running.
We're not going to build a complete application here, but just showing how you can start unit testing your Zend Framework project, so you can implement unit tests on your own applications.
First we need to modify the phpunit.xml file so we can use it as our unit testing configuration.
<phpunit bootstrap="./TestHelper.php" colors="true">
<testsuite name="Zend Framework Unit Testing">
<log type="coverage-html" target="./log/report" charset="UTF-8" yui="true"
highlight="true" lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html" />
As you see in the root node of our phpunit.xml file, we include the TestHelper script to aid us setting up the right testing environment. Of course, you can modify this file to fit your own application setup. This is my TestHelper script to set up my test environment, include paths and setting timezones.
// start output buffering
// set our app paths and environments
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
// Include path
. PATH_SEPARATOR . BASE_PATH . '/library'
. PATH_SEPARATOR . get_include_path()
// Set the default timezone !!!
// We wanna catch all errors en strict warnings
We create a custom ControllerTestCase that we can extend in all our controller test cases. This proves to be the most convenient way to handle the setup and teardown of the Zend Framework architecture.
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
public function setUp()
$this->application = new Zend_Application(
APPLICATION_PATH . '/configs/application.ini'
$this->bootstrap = array($this, 'appBootstrap');
public function appBootstrap()
And finally we create our first test on the default IndexController.
require_once realpath(dirname(__FILE__) . '/../../ControllerTestCase.php');
class IndexControllerTest extends ControllerTestCase
public function testCallingRootTriggersIndex()
public function testCallingBogusTriggersError()
Running phpunit from commandline will tell you that your assertions succeeded and all is a go.
All green, assertions succeeded and report is generated.
I've setup my test environment that directory log/report is aliased by /report, so I can view the code coverage reports (requires XDebug).
The following code coverage report is the result.
Zend Framework application to be unit tested. So now you don't have any more excuses to leave your applications untested.
Since I don't have all the knowledge, I would like to thank Matthew Weier O' Phinney and Mathias Geat for sharing their knowledge on the subject. Thanks guys !