Pragmatism in the real world

Unit testing Zend Framework 1

As part of our release process for Zend Framework 1.12, I’ve been working through the unit tests and running them on PHP 5.2.4 as it seems that recent changes weren’t being tested with that version. This isn’t totally surprising as Open Source contributors are, almost by definition, interested in new things and so are much more likely to be running PHP 5.4 rather than 5.2! This is, of course, a compelling reason for using continuous integration and I’m quite excited with Travis-CI and we are using it with ZF2.

Installing PHPUnit 3.4

The first challenge that I encountered was that ZF1’s unit test are not compatible with PHPUnit 3.6. As there are over 14,000 ZF1 unit tests which have been written since 2006, there hasn’t been much enthusiasm for rewriting them to be PHPUnit 3.6 compatible. (However, if someone wants to volunteer, please contact me!)

As I have PHPUnit 3.6 installed for testing other projects, I needed to install PHPUnit 3.4 side-by-side with version 3.6 This turns out to be relatively easy and has been documented by Christer Edvartsen in his article Running Multiple Versions of PHPUnit.

I ran into one problem with his instructions though and needed this code to be added to the top of phpunit:

// For OS X 10.7
set_include_path(implode(PATH_SEPARATOR, array(
    __DIR__ . '/../share/php',
    '/usr/share/php',
    get_include_path()
)));

// Or for OS X 10.8
set_include_path(implode(PATH_SEPARATOR, array(
    __DIR__ . '/../lib/php/pear',
    '/usr/lib/php/pear',
    get_include_path()
)));

Having done this though, PHPUnit 3.4 works correctly and we can run ZF1 unit tests.

Running the ZF1 unit tests

To run ZF1’s unit tests, you first need to check out the code from the Subversion repository.

svn co http://framework.zend.com/svn/framework/standard/trunk/

Due to the number of tests and the memory that they take up, you should run tests for each component individually.

The command to use is:

phpunit34 --stderr -d memory_limit=-1 Zend/{Component}/AllTests.php

Note:

  • --stderr pipes PHPUnit’s output to stderr which means that any tests that rely on header() will work. (i,e. don’t test Zend_Session without it!)
  • -d memory_limit=-1 will turn off PHP’s memory_limit setting. The ZF1 unit tests use a lot of memory, so this is easiest.
  • The tests rely on being set up correctly. This is done using AllTests.php so don’t forget this. Tests will fail if you forget!

All that needs to happen now is that any failing tests are fixed!