3 // PHP framework for testing, based on the design of "JUnit".
5 // Written by Fred Yankowski <fred@ontosys.com>
6 // OntoSys, Inc <http://www.OntoSys.com>
10 // Copyright (c) 2000 Fred Yankowski
12 // Permission is hereby granted, free of charge, to any person
13 // obtaining a copy of this software and associated documentation
14 // files (the "Software"), to deal in the Software without
15 // restriction, including without limitation the rights to use, copy,
16 // modify, merge, publish, distribute, sublicense, and/or sell copies
17 // of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE |
33 E_CORE_ERROR | E_CORE_WARNING);
37 function run(&$aTestResult);
38 function countTestCases();
42 function trace($msg) {
49 class phpUnitException {
50 /* Emulate a Java exception, sort of... */
53 function phpUnitException($message, $type = 'FAILURE') {
54 $this->message = $message;
57 function getMessage() {
58 return $this->message;
63 function assert($boolean, $message=0) {
65 $this->fail($message);
68 function assertEquals($expected, $actual, $message=0) {
69 if ($expected != $actual) {
70 $this->failNotEquals($expected, $actual, "expected", $message);
74 function assertRegexp($regexp, $actual, $message=false) {
75 if (! preg_match($regexp, $actual)) {
76 $this->failNotEquals($regexp, $actual, "pattern", $message);
80 function failNotEquals($expected, $actual, $expected_label, $message=0) {
81 // Private function for reporting failure to match.
82 $str = $message ? ($message . ' ') : '';
83 $str .= "($expected_label/actual)<br>";
84 $htmlExpected = htmlspecialchars($expected);
85 $htmlActual = htmlspecialchars($actual);
86 $str .= sprintf("<pre>%s\n--------\n%s</pre>",
87 $htmlExpected, $htmlActual);
92 class TestCase extends Assert /* implements Test */ {
93 /* Defines context for running tests. Specific context -- such as
94 instance variables, global variables, global state -- is defined
95 by creating a subclass that specializes the setUp() and
96 tearDown() methods. A specific test is defined by a subclass
97 that specializes the runTest() method. */
100 var $fExceptions = array();
102 function TestCase($name) {
103 $this->fName = $name;
106 function run($testResult=0) {
107 /* Run this single test, by calling the run() method of the
108 TestResult object which will in turn call the runBare() method
109 of this object. That complication allows the TestResult object
110 to do various kinds of progress reporting as it invokes each
111 test. Create/obtain a TestResult object if none was passed in.
112 Note that if a TestResult object was passed in, it must be by
115 $testResult = $this->_createResult();
116 $this->fResult = $testResult;
117 $testResult->run(&$this);
122 function countTestCases() {
127 $name = $this->name();
128 // Since isset($this->$name) is false, no way to run defensive checks
132 function setUp() /* expect override */ {
133 //print("TestCase::setUp()<br>\n");
136 function tearDown() /* possible override */ {
137 //print("TestCase::tearDown()<br>\n");
140 ////////////////////////////////////////////////////////////////
143 function _createResult() /* protected */ {
144 /* override this to use specialized subclass of TestResult */
145 return new TestResult;
148 function fail($message=0) {
149 //printf("TestCase::fail(%s)<br>\n", ($message) ? $message : '');
150 /* JUnit throws AssertionFailedError here. We just record the
151 failure and carry on */
152 $this->fExceptions[] = new Exception(&$message);
155 function error($message) {
156 /* report error that requires correction in the test script
157 itself, or (heaven forbid) in this testing infrastructure */
158 printf('<b>ERROR: ' . $message . '</b><br>');
159 $this->fResult->stop();
163 return count($this->fExceptions);
166 function getExceptions() {
167 return $this->fExceptions;
182 class TestSuite /* implements Test */ {
183 /* Compose a set of Tests (instances of TestCase or TestSuite), and
185 var $fTests = array();
187 function TestSuite($classname=false) {
189 // Find all methods of the given class whose name starts with
190 // "test" and add them to the test suite. We are just _barely_
191 // able to do this with PHP's limited introspection... Note
192 // that PHP seems to store method names in lower case, and we
193 // have to avoid the constructor function for the TestCase class
194 // superclass. This will fail when $classname starts with
195 // "Test" since that will have a constructor method that will
196 // get matched below and then treated (incorrectly) as a test
197 // method. So don't name any TestCase subclasses as "Test..."!
198 if (floor(phpversion()) >= 4) {
199 // PHP4 introspection, submitted by Dylan Kuhn
200 $names = get_class_methods($classname);
201 while (list($key, $method) = each($names)) {
202 if (preg_match('/^test/', $method) && $method != "testcase") {
203 $this->addTest(new $classname($method));
208 $dummy = new $classname("dummy");
209 $names = (array) $dummy;
210 while (list($key, $value) = each($names)) {
211 $type = gettype($value);
212 if ($type == "user function" && preg_match('/^test/', $key)
213 && $key != "testcase") {
214 $this->addTest(new $classname($key));
221 function addTest($test) {
222 /* Add TestCase or TestSuite to this TestSuite */
223 $this->fTests[] = $test;
226 function run(&$testResult) {
227 /* Run all TestCases and TestSuites comprising this TestSuite,
228 accumulating results in the given TestResult object. */
229 reset($this->fTests);
230 while (list($na, $test) = each($this->fTests)) {
231 if ($testResult->shouldStop())
233 $test->run(&$testResult);
237 function countTestCases() {
238 /* Number of TestCases comprising this TestSuite (including those
239 in any constituent TestSuites) */
242 while (list($na, $test_case) = each($this->fTests)) {
243 $count += $test_case->countTestCases();
251 /* Record failure of a single TestCase, associating it with the
252 exception(s) that occurred */
253 var $fFailedTestName;
256 function TestFailure(&$test, &$exceptions) {
257 $this->fFailedTestName = $test->name();
258 $this->fExceptions = $exceptions;
261 function getExceptions() {
262 return $this->fExceptions;
264 function getTestName() {
265 return $this->fFailedTestName;
271 /* Collect the results of running a set of TestCases. */
272 var $fFailures = array();
276 function TestResult() { }
278 function _endTest($test) /* protected */ {
279 /* specialize this for end-of-test action, such as progress
283 function getFailures() {
284 return $this->fFailures;
287 function run($test) {
288 /* Run a single TestCase in the context of this TestResult */
289 $this->_startTest($test);
294 /* this is where JUnit would catch AssertionFailedError */
295 $exceptions = $test->getExceptions();
297 $this->fFailures[] = new TestFailure(&$test, &$exceptions);
298 $this->_endTest($test);
301 function countTests() {
302 return $this->fRunTests;
305 function shouldStop() {
309 function _startTest($test) /* protected */ {
310 /* specialize this for start-of-test actions */
314 /* set indication that the test sequence should halt */
318 function countFailures() {
319 return count($this->fFailures);
324 class TextTestResult extends TestResult {
325 /* Specialize TestResult to produce text/html report */
326 function TextTestResult() {
327 $this->TestResult(); // call superclass constructor
331 /* report result of test run */
332 $nRun = $this->countTests();
333 $nFailures = $this->countFailures();
334 printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's');
335 printf("%s failure%s.<br>\n", $nFailures, ($nFailures == 1) ? '' : 's');
340 $failures = $this->getFailures();
341 while (list($i, $failure) = each($failures)) {
342 $failedTestName = $failure->getTestName();
343 printf("<li>%s\n", $failedTestName);
345 $exceptions = $failure->getExceptions();
347 while (list($na, $exception) = each($exceptions))
348 printf("<li>%s\n", $exception->getMessage());
354 function _startTest($test) {
355 printf("%s ", $test->name());
359 function _endTest($test) {
360 $outcome = $test->failed()
361 ? "<font color=\"red\">FAIL</font>"
362 : "<font color=\"green\">ok</font>";
363 printf("$outcome<br>\n");
370 /* Run a suite of tests and report results. */
371 function run($suite) {
372 $result = new TextTestResult;
373 $suite->run($result);