📄 phpunit.php
字号:
// "test" and add them to the test suite. // PHP3: 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. Names of subclasses of TestCase // must not start with "Test" since such a class will have a // constructor method name also starting with "test" and we can't // distinquish such a construtor from the real test method names. // So don't name any TestCase subclasses as "Test..."! // PHP4: Never mind all that. We can now ignore constructor // methods, so a test class may be named "Test...". if (empty($classname)) return; $this->fClassname = $classname; if (floor(phpversion()) >= 4) { // PHP4 introspection, submitted by Dylan Kuhn $names = get_class_methods($classname); while (list($key, $method) = @each($names)) { if (preg_match('/^test/', $method)) { $test = new $classname($method); if (strcasecmp($method, $classname) == 0 || is_subclass_of($test, $method)) { // Ignore the given method name since it is a constructor: // it's the name of our test class or it is the name of a // superclass of our test class. (This code smells funny. // Anyone got a better way?) //print "skipping $method<br>"; } else { $this->addTest($test); } } } } else { // PHP3 $dummy = new $classname("dummy"); $names = (array) $dummy; while (list($key, $value) = each($names)) { $type = gettype($value); if ($type == "user function" && preg_match('/^test/', $key) && $key != "testcase") { $this->addTest(new $classname($key)); } } } } function addTest($test) { /* Add TestCase or TestSuite to this TestSuite */ $this->fTests[] = $test; } function run(&$testResult) { /* Run all TestCases and TestSuites comprising this TestSuite, accumulating results in the given TestResult object. */ reset($this->fTests); while (list($na, $test) = each($this->fTests)) { if ($testResult->shouldStop()) break; $test->run(&$testResult); } } function countTestCases() { /* Number of TestCases comprising this TestSuite (including those in any constituent TestSuites) */ $count = 0; reset($fTests); while (list($na, $test_case) = each($this->fTests)) { $count += $test_case->countTestCases(); } return $count; }}class TestFailure { /* Record failure of a single TestCase, associating it with the exception that occurred */ var $fFailedTestName; var $fException; function TestFailure(&$test, &$exception) { $this->fFailedTestName = $test->name(); $this->fException = $exception; } function getExceptions() { // deprecated return array($this->fException); } function getException() { return $this->fException; } function getTestName() { return $this->fFailedTestName; }}class TestResult { /* Collect the results of running a set of TestCases. */ var $fFailures = array(); var $fErrors = array(); var $fRunTests = 0; var $fStop = false; function TestResult() { } function _endTest($test) /* protected */ { /* specialize this for end-of-test action, such as progress reports */ } function addError($test, $exception) { $this->fErrors[] = new TestFailure(&$test, &$exception); } function addFailure($test, $exception) { $this->fFailures[] = new TestFailure(&$test, &$exception); } function getFailures() { return $this->fFailures; } function run($test) { /* Run a single TestCase in the context of this TestResult */ $this->_startTest($test); $this->fRunTests++; $test->runBare(); /* this is where JUnit would catch AssertionFailedError */ $exceptions = $test->getExceptions(); reset($exceptions); while (list($key, $exception) = each($exceptions)) { if ($exception->type == 'ERROR') $this->addError($test, $exception); else if ($exception->type == 'FAILURE') $this->addFailure($test, $exception); } // if ($exceptions) // $this->fFailures[] = new TestFailure(&$test, &$exceptions); $this->_endTest($test); } function countTests() { return $this->fRunTests; } function shouldStop() { return $this->fStop; } function _startTest($test) /* protected */ { /* specialize this for start-of-test actions */ } function stop() { /* set indication that the test sequence should halt */ $fStop = true; } function errorCount() { return count($this->fErrors); } function failureCount() { return count($this->fFailures); } function countFailures() { // deprecated return $this->failureCount(); }}class TextTestResult extends TestResult { /* Specialize TestResult to produce text/html report */ function TextTestResult() { $this->TestResult(); // call superclass constructor } function report() { /* report result of test run */ $nRun = $this->countTests(); $nFailures = $this->failureCount(); $nErrors = $this->errorCount(); printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's'); printf("%s failure%s<br>\n", $nFailures, ($nFailures == 1) ? '' : 's'); printf("%s error%s.<br>\n", $nErrors, ($nErrors == 1) ? '' : 's'); if ($nFailures > 0) { print("<h2>Failures</h2>"); print("<ol>\n"); $failures = $this->getFailures(); while (list($i, $failure) = each($failures)) { $failedTestName = $failure->getTestName(); printf("<li>%s\n", $failedTestName); $exceptions = $failure->getExceptions(); print("<ul>"); while (list($na, $exception) = each($exceptions)) printf("<li>%s\n", $exception->getMessage()); print("</ul>"); } print("</ol>\n"); } if ($nErrors > 0) { print("<h2>Errors</h2>"); print("<ol>\n"); reset($this->fErrors); while (list($i, $error) = each($this->fErrors)) { $erroredTestName = $error->getTestName(); printf("<li>%s\n", $failedTestName); $exception = $error->getException(); print("<ul>"); printf("<li>%s\n", $exception->getMessage()); print("</ul>"); } print("</ol>\n"); } } function _startTest($test) { if (phpversion() > '4') { printf("%s - %s ", get_class($test), $test->name()); } else { printf("%s ", $test->name()); } flush(); } function _endTest($test) { $outcome = $test->failed() ? "<font color=\"red\">FAIL</font>" : "<font color=\"green\">ok</font>"; printf("$outcome<br>\n"); flush(); }}// PrettyTestResult created by BJG 17/11/01// beacuse the standard test result provided looks// rubbish.class PrettyTestResult extends TestResult { /* Specialize TestResult to produce text/html report */ function PrettyTestResult() { $this->TestResult(); // call superclass constructor echo "<h2>Tests</h2>"; echo "<TABLE CELLSPACING=\"1\" CELLPADDING=\"1\" BORDER=\"0\" WIDTH=\"90%\" ALIGN=\"CENTER\" class=\"details\">"; echo "<TR><TH>Class</TH><TH>Function</TH><TH>Success?</TH></TR>"; } function report() { echo "</TABLE>"; /* report result of test run */ $nRun = $this->countTests(); $nFailures = $this->countFailures(); echo "<h2>Summary</h2>"; printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's'); printf("%s failure%s.<br>\n", $nFailures, ($nFailures == 1) ? '' : 's'); if ($nFailures == 0) return; echo "<h2>Failure Details</h2>"; print("<ol>\n"); $failures = $this->getFailures(); while (list($i, $failure) = each($failures)) { $failedTestName = $failure->getTestName(); printf("<li>%s\n", $failedTestName); $exceptions = $failure->getExceptions(); print("<ul>"); while (list($na, $exception) = each($exceptions)) printf("<li>%s\n", $exception->getMessage()); print("</ul>"); } print("</ol>\n"); } function _startTest($test) { printf("<TR><TD>%s </TD><TD>%s </TD>", $test->classname(),$test->name()); flush(); } function _endTest($test) { $outcome = $test->failed() ? " class=\"Failure\">FAIL" : " class=\"Pass\">OK"; printf("<TD$outcome</TD></TR>"); flush(); }}class TestRunner { /* Run a suite of tests and report results. */ function run($suite) { $result = new TextTestResult; $suite->run($result); $result->report(); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -