1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | <?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 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(); } 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 requires correction in the test script itself, or (heaven forbid) in this testing infrastructure */ printf('<b>ERROR: ' . $message . '</b><br>'); $this->fResult->stop(); } function failed() { return count($this->fExceptions); } 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(); function TestSuite($classname=false) { if ($classname) { // 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..."! 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) && $method != "testcase") { $this->addTest(new $classname($method)); } } } else { $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(s) that occurred */ var $fFailedTestName; var $fExceptions; function TestFailure(&$test, &$exceptions) { $this->fFailedTestName = $test->name(); $this->fExceptions = $exceptions; } function getExceptions() { return $this->fExceptions; } function getTestName() { return $this->fFailedTestName; } } class TestResult { /* Collect the results of running a set of TestCases. */ var $fFailures = 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 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(); 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 countFailures() { return count($this->fFailures); } } 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->countFailures(); 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; 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("%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(); } } class TestRunner { /* Run a suite of tests and report results. */ function run($suite) { $result = new TextTestResult; $suite->run($result); $result->report(); } } ?> |