Source for file phpunit.php
Documentation is available at phpunit.php
// PHP framework for testing, based on the design of "JUnit".
// Written by Fred Yankowski <fred@ontosys.com>
// OntoSys, Inc <http://www.OntoSys.com>
// $Id: phpunit.php,v 1.2 2006/03/15 14:32:13 pcool Exp $
// Copyright (c) 2000 Fred Yankowski
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
E_CORE_ERROR | E_CORE_WARNING);
function run(&$aTestResult);
function countTestCases();
/* Emulate a Java exception, sort of... */
function assert($boolean, $message= 0) {
if ($expected != $actual) {
function failNotEquals($expected, $actual, $expected_label, $message= 0) {
// Private function for reporting failure to match.
$str = $message ? ($message . ' ') : '';
$str .= "($expected_label/actual)<br>";
$str .= sprintf("<pre>%s\n--------\n%s</pre>",
$htmlExpected, $htmlActual);
/* Defines context for running tests. Specific context -- such as
instance variables, global variables, global state -- is defined
by creating a subclass that specializes the setUp() and
tearDown() methods. A specific test is defined by a subclass
that specializes the runTest() method. */
function run($testResult= 0) {
/* Run this single test, by calling the run() method of the
TestResult object which will in turn call the runBare() method
of this object. That complication allows the TestResult object
to do various kinds of progress reporting as it invokes each
test. Create/obtain a TestResult object if none was passed in.
Note that if a TestResult object was passed in, it must be by
$testResult->run(&$this);
// Since isset($this->$name) is false, no way to run defensive checks
function setUp() /* expect override */ {
//print("TestCase::setUp()<br>\n");
function tearDown() /* possible override */ {
//print("TestCase::tearDown()<br>\n");
////////////////////////////////////////////////////////////////
/* override this to use specialized subclass of TestResult */
function fail($message= 0) {
//printf("TestCase::fail(%s)<br>\n", ($message) ? $message : '');
/* JUnit throws AssertionFailedError here. We just record the
function error($message) {
/* report error that requires correction in the test script
itself, or (heaven forbid) in this testing infrastructure */
printf('<b>ERROR: ' . $message . '</b><br>');
/* Compose a set of Tests (instances of TestCase or TestSuite), and
// Find all methods of the given class whose name starts with
// "test" and add them to the test suite. We are just _barely_
// able to do this with PHP's limited introspection... Note
// that PHP seems to store method names in lower case, and we
// have to avoid the constructor function for the TestCase class
// superclass. This will fail when $classname starts with
// "Test" since that will have a constructor method that will
// get matched below and then treated (incorrectly) as a test
// method. So don't name any TestCase subclasses as "Test..."!
// PHP4 introspection, submitted by Dylan Kuhn
while (list ($key, $method) = each($names)) {
if (preg_match('/^test/', $method) && $method != "testcase") {
$this->addTest(new $classname($method));
$dummy = new $classname("dummy");
while (list ($key, $value) = each($names)) {
if ($type == "user function" && preg_match('/^test/', $key)
$this->addTest(new $classname($key));
/* Add TestCase or TestSuite to this TestSuite */
function run(&$testResult) {
/* Run all TestCases and TestSuites comprising this TestSuite,
accumulating results in the given TestResult object. */
if ($testResult->shouldStop())
$test->run(&$testResult);
/* Number of TestCases comprising this TestSuite (including those
in any constituent TestSuites) */
while (list ($na, $test_case) = each($this->fTests)) {
$count += $test_case->countTestCases();
/* Record failure of a single TestCase, associating it with the
exception(s) that occurred */
/* Collect the results of running a set of TestCases. */
function _endTest($test) /* protected */ {
/* specialize this for end-of-test action, such as progress
/* Run a single TestCase in the context of this TestResult */
/* this is where JUnit would catch AssertionFailedError */
$exceptions = $test->getExceptions();
/* specialize this for start-of-test actions */
/* set indication that the test sequence should halt */
/* Specialize TestResult to produce text/html report */
$this->TestResult(); // call superclass constructor
/* report result of test run */
printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's');
printf("%s failure%s.<br>\n", $nFailures, ($nFailures == 1) ? '' : 's');
while (list ($i, $failure) = each($failures)) {
$failedTestName = $failure->getTestName();
printf("<li>%s\n", $failedTestName);
$exceptions = $failure->getExceptions();
while (list ($na, $exception) = each($exceptions))
printf("<li>%s\n", $exception->getMessage());
$outcome = $test->failed()
? "<font color=\"red\">FAIL</font>"
: "<font color=\"green\">ok</font>";
/* Run a suite of tests and report results. */
|