Chapter 17. PHPUnit for PHP 4There 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 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 29. Example 29. 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 8 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.
|