Prev Next

Appendix C. 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 http://pear.phpunit.de/get/PHPUnit-1.3.3.tgz

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 C.1 shows a version of the ArrayTest test case that can be used with PHPUnit for PHP 4.

Example C.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 C.2.

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

Figure C.1. The PHP-GTK Test Runner

The PHP-GTK Test Runner

Prev Next
1. Automating Tests
2. PHPUnit's Goals
3. Installing PHPUnit
4. Writing Tests for PHPUnit
Data Providers
Testing Exceptions
Testing PHP Errors
5. The Command-Line Test Runner
6. Fixtures
More setUp() than tearDown()
Variations
Sharing Fixture
7. Organizing Test Suites
Suite-Level Setup
8. TestCase Extensions
Testing Output
Testing Performance
9. Database Testing
Datasets
Flat XML Data Set
XML Data Set
Operations
Database Testing Best Practices
10. Incomplete and Skipped Tests
Incomplete Tests
Skipping Tests
11. Mock Objects
Self-Shunting
Stubs
12. Testing Practices
During Development
During Debugging
13. Test-First Programming
BankAccount Example
14. Code Coverage Analysis
Specifying Covered Methods
Ignoring Code Blocks
Including and Excluding Files
15. Other Uses for Tests
Agile Documentation
Cross-Team Tests
16. Logging
XML Format
Code Coverage (XML)
JavaScript Object Notation (JSON)
Test Anything Protocol (TAP)
GraphViz Markup
Test Database
17. Skeleton Generator
Annotations
18. PHPUnit and Selenium
Selenium RC
PHPUnit_Extensions_SeleniumTestCase
19. Continuous Integration
CruiseControl
phpUnderControl
Apache Maven
20. PHPUnit's Implementation
21. PHPUnit API
Overview
PHPUnit_Framework_Assert
PHPUnit_Framework_Test
PHPUnit_Framework_TestCase
PHPUnit_Framework_TestSuite
PHPUnit_Framework_TestResult
Package Structure
22. Extending PHPUnit
Subclass PHPUnit_Framework_TestCase
Assert Classes
Subclass PHPUnit_Extensions_TestDecorator
Implement PHPUnit_Framework_Test
Subclass PHPUnit_Framework_TestResult
Implement PHPUnit_Framework_TestListener
New Test Runner
A. Assertions
B. The XML Configuration File
Test Suite
Groups
Including and Excluding Files for Code Coverage
Logging
PMD Rules
Setting PHP INI settings and Global Variables
C. PHPUnit for PHP 4
D. Index
E. Bibliography
F. Copyright