AppendixA.PHPUnit for PHP 4

There is a release series of PHPUnit that works with PHP 4 and does not require PHP 5. Due to PHP 4's limited object model, PHPUnit for PHP 4 is not a complete port of JUnit as PHPUnit for PHP 5 is. It also lacks certain features of PHPUnit for PHP 5, such as code-coverage analysis.

The following command line shows how to install PHPUnit for PHP 4 using the PEAR Installer:

pear install -f phpunit/PHPUnit-1.3.2

A test-case class that is used with PHPUnit for PHP 4 is similar to one that is used with PHPUnit for PHP 5. The essential difference is that such a class extends PHPUnit_TestCase (which itself extends PHPUnit_Assert, the class that provides the assertion methods).

Example A.1 shows a version of the ArrayTest test case that can be used with PHPUnit for PHP 4.

Example A.1: Writing a test case for PHPUnit 1.x

<?php
require_once 'PHPUnit/TestCase.php';
 
class ArrayTest extends PHPUnit_TestCase
{
    var $_fixture;
 
    function setUp()
    {
        $this->_fixture = array();
    }
 
    function testNewArrayIsEmpty()
    {
        $this->assertEquals(0, sizeof($this->_fixture));
    }
 
    function testArrayContainsAnElement()
    {
        $this->_fixture[] = 'Element';
        $this->assertEquals(1, sizeof($this->_fixture));
    }
}
?>


PHPUnit for PHP 4 does not provide a TextUI test runner. The most commonly used way to run tests with PHPUnit for PHP 4 is to write a test suite and run it manually, as shown in Example A.2.

Example A.2: Running a test case with PHPUnit 1.x

<?php
require_once 'ArrayTest.php';
require_once 'PHPUnit.php';
 
$suite  = new PHPUnit_TestSuite('ArrayTest');
$result = PHPUnit::run($suite);
 
print $result->toString();
?>
TestCase arraytest->testnewarrayisempty() passed
TestCase arraytest->testarraycontainsanelement() passed


Figure A.1 shows the one feature that PHPUnit for PHP 4 has that PHPUnit for PHP 5 does not yet have: a test runner with a graphical user interface based on PHP-GTK.

FigureA.1.The PHP-GTK Test Runner

The PHP-GTK Test Runner