phpunit.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. //
  3. // PHP framework for testing, based on the design of "JUnit".
  4. //
  5. // Written by Fred Yankowski <fred@ontosys.com>
  6. // OntoSys, Inc <http://www.OntoSys.com>
  7. //
  8. // $Id: phpunit.php,v 1.1 2002/03/30 19:32:17 bmatzelle Exp $
  9. // Copyright (c) 2000 Fred Yankowski
  10. // Permission is hereby granted, free of charge, to any person
  11. // obtaining a copy of this software and associated documentation
  12. // files (the "Software"), to deal in the Software without
  13. // restriction, including without limitation the rights to use, copy,
  14. // modify, merge, publish, distribute, sublicense, and/or sell copies
  15. // of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. // SOFTWARE.
  29. //
  30. error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE |
  31. E_CORE_ERROR | E_CORE_WARNING);
  32. /*
  33. interface Test {
  34. function run(&$aTestResult);
  35. function countTestCases();
  36. }
  37. */
  38. function trace($msg) {
  39. return;
  40. print($msg);
  41. flush();
  42. }
  43. class Exception {
  44. /* Emulate a Java exception, sort of... */
  45. var $message;
  46. function Exception($message) {
  47. $this->message = $message;
  48. }
  49. function getMessage() {
  50. return $this->message;
  51. }
  52. }
  53. class Assert {
  54. function assert($boolean, $message=0) {
  55. if (! $boolean)
  56. $this->fail($message);
  57. }
  58. function assertEquals($expected, $actual, $message=0) {
  59. if ($expected != $actual) {
  60. $this->failNotEquals($expected, $actual, "expected", $message);
  61. }
  62. }
  63. function assertRegexp($regexp, $actual, $message=false) {
  64. if (! preg_match($regexp, $actual)) {
  65. $this->failNotEquals($regexp, $actual, "pattern", $message);
  66. }
  67. }
  68. function failNotEquals($expected, $actual, $expected_label, $message=0) {
  69. // Private function for reporting failure to match.
  70. $str = $message ? ($message . ' ') : '';
  71. $str .= "($expected_label/actual)<br>";
  72. $htmlExpected = htmlspecialchars($expected);
  73. $htmlActual = htmlspecialchars($actual);
  74. $str .= sprintf("<pre>%s\n--------\n%s</pre>",
  75. $htmlExpected, $htmlActual);
  76. $this->fail($str);
  77. }
  78. }
  79. class TestCase extends Assert /* implements Test */ {
  80. /* Defines context for running tests. Specific context -- such as
  81. instance variables, global variables, global state -- is defined
  82. by creating a subclass that specializes the setUp() and
  83. tearDown() methods. A specific test is defined by a subclass
  84. that specializes the runTest() method. */
  85. var $fName;
  86. var $fResult;
  87. var $fExceptions = array();
  88. function TestCase($name) {
  89. $this->fName = $name;
  90. }
  91. function run($testResult=0) {
  92. /* Run this single test, by calling the run() method of the
  93. TestResult object which will in turn call the runBare() method
  94. of this object. That complication allows the TestResult object
  95. to do various kinds of progress reporting as it invokes each
  96. test. Create/obtain a TestResult object if none was passed in.
  97. Note that if a TestResult object was passed in, it must be by
  98. reference. */
  99. if (! $testResult)
  100. $testResult = $this->_createResult();
  101. $this->fResult = $testResult;
  102. $testResult->run(&$this);
  103. $this->fResult = 0;
  104. return $testResult;
  105. }
  106. function countTestCases() {
  107. return 1;
  108. }
  109. function runTest() {
  110. $name = $this->name();
  111. // Since isset($this->$name) is false, no way to run defensive checks
  112. $this->$name();
  113. }
  114. function setUp() /* expect override */ {
  115. //print("TestCase::setUp()<br>\n");
  116. }
  117. function tearDown() /* possible override */ {
  118. //print("TestCase::tearDown()<br>\n");
  119. }
  120. ////////////////////////////////////////////////////////////////
  121. function _createResult() /* protected */ {
  122. /* override this to use specialized subclass of TestResult */
  123. return new TestResult;
  124. }
  125. function fail($message=0) {
  126. //printf("TestCase::fail(%s)<br>\n", ($message) ? $message : '');
  127. /* JUnit throws AssertionFailedError here. We just record the
  128. failure and carry on */
  129. $this->fExceptions[] = new Exception(&$message);
  130. }
  131. function error($message) {
  132. /* report error that requires correction in the test script
  133. itself, or (heaven forbid) in this testing infrastructure */
  134. printf('<b>ERROR: ' . $message . '</b><br>');
  135. $this->fResult->stop();
  136. }
  137. function failed() {
  138. return count($this->fExceptions);
  139. }
  140. function getExceptions() {
  141. return $this->fExceptions;
  142. }
  143. function name() {
  144. return $this->fName;
  145. }
  146. function runBare() {
  147. $this->setup();
  148. $this->runTest();
  149. $this->tearDown();
  150. }
  151. }
  152. class TestSuite /* implements Test */ {
  153. /* Compose a set of Tests (instances of TestCase or TestSuite), and
  154. run them all. */
  155. var $fTests = array();
  156. function TestSuite($classname=false) {
  157. if ($classname) {
  158. // Find all methods of the given class whose name starts with
  159. // "test" and add them to the test suite. We are just _barely_
  160. // able to do this with PHP's limited introspection... Note
  161. // that PHP seems to store method names in lower case, and we
  162. // have to avoid the constructor function for the TestCase class
  163. // superclass. This will fail when $classname starts with
  164. // "Test" since that will have a constructor method that will
  165. // get matched below and then treated (incorrectly) as a test
  166. // method. So don't name any TestCase subclasses as "Test..."!
  167. if (floor(phpversion()) >= 4) {
  168. // PHP4 introspection, submitted by Dylan Kuhn
  169. $names = get_class_methods($classname);
  170. while (list($key, $method) = each($names)) {
  171. if (preg_match('/^test/', $method) && $method != "testcase") {
  172. $this->addTest(new $classname($method));
  173. }
  174. }
  175. }
  176. else {
  177. $dummy = new $classname("dummy");
  178. $names = (array) $dummy;
  179. while (list($key, $value) = each($names)) {
  180. $type = gettype($value);
  181. if ($type == "user function" && preg_match('/^test/', $key)
  182. && $key != "testcase") {
  183. $this->addTest(new $classname($key));
  184. }
  185. }
  186. }
  187. }
  188. }
  189. function addTest($test) {
  190. /* Add TestCase or TestSuite to this TestSuite */
  191. $this->fTests[] = $test;
  192. }
  193. function run(&$testResult) {
  194. /* Run all TestCases and TestSuites comprising this TestSuite,
  195. accumulating results in the given TestResult object. */
  196. reset($this->fTests);
  197. while (list($na, $test) = each($this->fTests)) {
  198. if ($testResult->shouldStop())
  199. break;
  200. $test->run(&$testResult);
  201. }
  202. }
  203. function countTestCases() {
  204. /* Number of TestCases comprising this TestSuite (including those
  205. in any constituent TestSuites) */
  206. $count = 0;
  207. reset($fTests);
  208. while (list($na, $test_case) = each($this->fTests)) {
  209. $count += $test_case->countTestCases();
  210. }
  211. return $count;
  212. }
  213. }
  214. class TestFailure {
  215. /* Record failure of a single TestCase, associating it with the
  216. exception(s) that occurred */
  217. var $fFailedTestName;
  218. var $fExceptions;
  219. function TestFailure(&$test, &$exceptions) {
  220. $this->fFailedTestName = $test->name();
  221. $this->fExceptions = $exceptions;
  222. }
  223. function getExceptions() {
  224. return $this->fExceptions;
  225. }
  226. function getTestName() {
  227. return $this->fFailedTestName;
  228. }
  229. }
  230. class TestResult {
  231. /* Collect the results of running a set of TestCases. */
  232. var $fFailures = array();
  233. var $fRunTests = 0;
  234. var $fStop = false;
  235. function TestResult() { }
  236. function _endTest($test) /* protected */ {
  237. /* specialize this for end-of-test action, such as progress
  238. reports */
  239. }
  240. function getFailures() {
  241. return $this->fFailures;
  242. }
  243. function run($test) {
  244. /* Run a single TestCase in the context of this TestResult */
  245. $this->_startTest($test);
  246. $this->fRunTests++;
  247. $test->runBare();
  248. /* this is where JUnit would catch AssertionFailedError */
  249. $exceptions = $test->getExceptions();
  250. if ($exceptions)
  251. $this->fFailures[] = new TestFailure(&$test, &$exceptions);
  252. $this->_endTest($test);
  253. }
  254. function countTests() {
  255. return $this->fRunTests;
  256. }
  257. function shouldStop() {
  258. return $this->fStop;
  259. }
  260. function _startTest($test) /* protected */ {
  261. /* specialize this for start-of-test actions */
  262. }
  263. function stop() {
  264. /* set indication that the test sequence should halt */
  265. $fStop = true;
  266. }
  267. function countFailures() {
  268. return count($this->fFailures);
  269. }
  270. }
  271. class TextTestResult extends TestResult {
  272. /* Specialize TestResult to produce text/html report */
  273. function TextTestResult() {
  274. $this->TestResult(); // call superclass constructor
  275. }
  276. function report() {
  277. /* report result of test run */
  278. $nRun = $this->countTests();
  279. $nFailures = $this->countFailures();
  280. printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's');
  281. printf("%s failure%s.<br>\n", $nFailures, ($nFailures == 1) ? '' : 's');
  282. if ($nFailures == 0)
  283. return;
  284. print("<ol>\n");
  285. $failures = $this->getFailures();
  286. while (list($i, $failure) = each($failures)) {
  287. $failedTestName = $failure->getTestName();
  288. printf("<li>%s\n", $failedTestName);
  289. $exceptions = $failure->getExceptions();
  290. print("<ul>");
  291. while (list($na, $exception) = each($exceptions))
  292. printf("<li>%s\n", $exception->getMessage());
  293. print("</ul>");
  294. }
  295. print("</ol>\n");
  296. }
  297. function _startTest($test) {
  298. printf("%s ", $test->name());
  299. flush();
  300. }
  301. function _endTest($test) {
  302. $outcome = $test->failed()
  303. ? "<font color=\"red\">FAIL</font>"
  304. : "<font color=\"green\">ok</font>";
  305. printf("$outcome<br>\n");
  306. flush();
  307. }
  308. }
  309. class TestRunner {
  310. /* Run a suite of tests and report results. */
  311. function run($suite) {
  312. $result = new TextTestResult;
  313. $suite->run($result);
  314. $result->report();
  315. }
  316. }
  317. ?>