📄 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.1.1 2004/10/15 01:05:29 ajdonnison 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// 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();}if (phpversion() >= '4') { function PHPUnit_error_handler($errno, $errstr, $errfile, $errline) { global $PHPUnit_testRunning; $PHPUnit_testRunning[0]->fail("<B>PHP ERROR:</B> ".$errstr." <B>in</B> ".$errfile." <B>at line</B> ".$errline); }}class Exception { /* Emulate a Java exception, sort of... */ var $message; var $type; function Exception($message, $type = 'FAILURE') { $this->message = $message; $this->type = $type; } function getMessage() { return $this->message; } function getType() { return $this->type; }}class Assert { function assert($boolean, $message=0) { if (! $boolean) $this->fail($message); } function assertEquals($expected, $actual, $message=0) { if (gettype($expected) != gettype($actual)) { $this->failNotEquals($expected, $actual, "expected", $message); return; } if (phpversion() < '4') { if (is_object($expected) or is_object($actual) or is_array($expected) or is_array($actual)) { $this->error("INVALID TEST: cannot compare arrays or objects in PHP3"); return; } } if (phpversion() >= '4' && is_object($expected)) { if (get_class($expected) != get_class($actual)) { $this->failNotEquals($expected, $actual, "expected", $message); return; } if (method_exists($expected, "equals")) { if (! $expected->equals($actual)) { $this->failNotEquals($expected, $actual, "expected", $message); } return; // no further tests after equals() } } if (phpversion() >= '4.0.4') { if (is_null($expected) != is_null($actual)) { $this->failNotEquals($expected, $actual, "expected", $message); return; } } 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 assertEqualsMultilineStrings($string0, $string1, $message="") { $lines0 = split("\n",$string0); $lines1 = split("\n",$string1); if (sizeof($lines0) != sizeof($lines1)) { $this->failNotEquals(sizeof($lines0)." line(s)", sizeof($lines1)." line(s)", "expected", $message); } for($i=0; $i< sizeof($lines0); $i++) { $this->assertEquals(trim($lines0[$i]), trim($lines1[$i]), "line ".($i+1)." of multiline strings differ. ".$message); } } function _formatValue($value, $class="") { $translateValue = $value; if (phpversion() >= '4.0.0') { if (is_object($value)) { if (method_exists($value, "toString") ) { $translateValue = $value->toString(); } else { $translateValue = serialize($value); } } else if (is_array($value)) { $translateValue = serialize($value); } } $htmlValue = "<code class=\"$class\">" . htmlspecialchars($translateValue) . "</code>"; if (phpversion() >= '4.0.0') { if (is_bool($value)) { $htmlValue = $value ? "<i>true</i>" : "<i>false</i>"; } elseif (phpversion() >= '4.0.4' && is_null($value)) { $htmlValue = "<i>null</i>"; } $htmlValue .= " <span class=\"typeinfo\">"; $htmlValue .= "type:" . gettype($value); $htmlValue .= is_object($value) ? ", class:" . get_class($value) : ""; $htmlValue .= "</span>"; } return $htmlValue; } function failNotEquals($expected, $actual, $expected_label, $message=0) { // Private function for reporting failure to match. $str = $message ? ($message . ' ') : ''; //$str .= "($expected_label/actual)<br>"; $str .= "<br>"; $str .= sprintf("%s<br>%s", $this->_formatValue($expected, "expected"), $this->_formatValue($actual, "actual")); $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 $fClassName; 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 classname() { if (isset($this->fClassName)) { return $this->fClassName; } else { return get_class($this); } } function countTestCases() { return 1; } function runTest() { if (phpversion() >= '4') { global $PHPUnit_testRunning; eval('$PHPUnit_testRunning[0] = & $this;'); // Saved ref to current TestCase, so that the error handler // can access it. This code won't even parse in PHP3, so we // hide it in an eval. $old_handler = set_error_handler("PHPUnit_error_handler"); // errors will now be handled by our error handler } $name = $this->name(); if (phpversion() >= '4' && ! method_exists($this, $name)) { $this->error("Method '$name' does not exist"); } else $this->$name(); /* if (phpversion() >= '4') { set_error_handler($old_handler); // revert to prior error handler $PHPUnit_testRunning = null; } */ } 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, 'FAILURE'); } function error($message) { /* report error that requires correction in the test script itself, or (heaven forbid) in this testing infrastructure */ $this->fExceptions[] = new Exception(&$message, 'ERROR'); $this->fResult->stop(); // [does not work] } function failed() { reset($this->fExceptions); while (list($key, $exception) = each($this->fExceptions)) { if ($exception->type == 'FAILURE') return true; } return false; } function errored() { reset($this->fExceptions); while (list($key, $exception) = each($this->fExceptions)) { if ($exception->type == 'ERROR') return true; } return false; } function getExceptions() { return $this->fExceptions; } function name() { return $this->fName; } function runBare() { $this->setup(); $this->runTest(); $this->tearDown(); }}class TestSuite /* implements Test */ { /* Compose a set of Tests (instances of TestCase or TestSuite), and run them all. */ var $fTests = array(); var $fClassname; function TestSuite($classname=false) { // Find all methods of the given class whose name starts with
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -