Sunday, November 01, 2009

Unit testing with Zend Framework 1.8+

I recently gave a presentation at ZendCon 2009 Uncon about "php unit with Zend Framework", which many people liked a lot (see comments on http://joind.in/638). But since then I got a lot of questions how to set up a testing environment for Zend Framework applications that uses version Zend Framework 1.8 or newer.

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">
        <directory>./directory>
    testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">../library/directory>
            <directory suffix=".php">../application/directory>
            <exclude>
                <directory suffix=".phtml">../application/directory>
            exclude>
        whitelist>
    filter>

    <logging>
        <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" />
    logging>
phpunit>


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
ob_start();

// set our app paths and environments
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
define('APPLICATION_ENV', 'testing');

// Include path
set_include_path(
    '.'
    . PATH_SEPARATOR . BASE_PATH . '/library'
    . PATH_SEPARATOR . get_include_path()
);

// Set the default timezone !!!
date_default_timezone_set('Europe/Brussels');

// We wanna catch all errors en strict warnings
error_reporting(E_ALL|E_STRICT);

require_once 'ControllerTestCase.php';



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.



require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $application;

    public function setUp()
    {
        $this->application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }

    public function appBootstrap()
    {
        $this->application->bootstrap();
    }
}



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()
    {
        $this->dispatch('/');
        $this->assertController('index');
        $this->assertAction('index');
    }
  
    public function testCallingBogusTriggersError()
    {
        $this->dispatch('/bogus');
        $this->assertController('error');
        $this->assertAction('error');
        $this->assertResponseCode(404);
    }
}



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.


As you can see, with just a minimal effort you've set up your 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 !
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.