【php开源代码栏目提醒】:网学会员鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“phpunit.php”一文,供大家参考学习
<?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.1 2002/03/30 19:32:17 bmatzelle 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 per
mission 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
// SOFTWARE.
//
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE |
E_CORE_ERROR | E_CORE_WARNING);
/*
interface Test {
function run(&$aTestResult);
function countTestCases();
}
*/
function trace($msg) {
return;
print($msg);
flush();
}
class Exception {
/* Emulate a
Java exception, sort of... */
var $message;
function Exception($message) {
$this->message = $message;
}
function getMessage() {
return $this->message;
}
}
class Assert {
function assert($boolean, $message=0) {
if (! $boolean)
$this->fail($message);
}
function assertEquals($expected, $actual, $message=0) {
if ($expected != $actual) {
$this->failNotEquals($expected, $actual, "expected", $message);
}
}
function assertRegexp($regexp, $actual, $message=false) {
if (! preg_match($regexp, $actual)) {
$this->failNotEquals($regexp, $actual, "pattern", $message);
}
}
function failNotEquals($expected, $actual, $expected_label, $message=0) {
// Private function for reporting failure to match.
$str = $message ? ($message . ' ') : '';
$str .= "($expected_label/actual)<br>";
$htmlExpected = htmlspecialchars($expected);
$htmlActual = htmlspecialchars($actual);
$str .= sprintf("<pre>%s\n--------\n%s</pre>",
$htmlExpected, $htmlActual);
$this->fail($str);
}
}
class TestCase extends Assert /* implements Test */ {
/* 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. */
var $fName;
var $fResult;
var $fExceptions = array();
function TestCase($name) {
$this->fName = $name;
}
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
reference. */
if (! $testResult)
$testResult = $this->_createResult();
$this->fResult = $testResult;
$testResult->run(&$this);
$this->fResult = 0;
return $testResult;
}
function countTestCases() {
return 1;
}
function runTest() {
$name = $this->name();
// Since isset($this->$name) is false, no way to run defensive checks
$this->$name();
}
function setUp() /* expect override */ {
//print("TestCase::setUp()<br>\n");
}
function tearDown() /* possible override */ {
//print("TestCase::tearDown()<br>\n");
}
////////////////////////////////////////////////////////////////
function _createResult() /* protected */ {
/* override this to use specialized subclass of TestResult */
return new TestResult;
}
function fail($message=0) {
//printf("TestCase::fail(%s)<br>\n", ($message) ? $message : '');
/* JUnit throws AssertionFailedError here. We just record the
failure and carry on */
$this->fExceptions[] = new Exception(&$message);
}
function error($message) {
/* report error that req