Chapter 17. 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 PHP-Unit for PHP 5, such as code-coverage analysis.

The PHPUnit release series for PHP 4 has its own PEAR package named PHPUnit (instead of PHPUnit2). This is because incompatible branches of PEAR packages (such as PHPUnit 1.X for PHP 4 and PHPUnit 2.X for PHP 5) have to be maintained in separate packages.

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

$ pear install PHPUnit

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 a PHP4 test class extends PHPUnit_ TestCase (which itself extends PHPUnit_Assert, the class that provides the assertion methods).

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

Example 28. 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 ...

Get PHPUnit Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.