PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/gacl/test_suite/phpunit/phpunit.php

https://github.com/md-tech/openemr
PHP | 633 lines | 435 code | 84 blank | 114 comment | 82 complexity | 1d00b3cdde49e88c91f2c81883ee40de MD5 | raw file
  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$
  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. if (phpversion() >= '4') {
  44. function PHPUnit_error_handler($errno, $errstr, $errfile, $errline) {
  45. global $PHPUnit_testRunning;
  46. $PHPUnit_testRunning[0]->fail("<B>PHP ERROR:</B> ".$errstr." <B>in</B> ".$errfile." <B>at line</B> ".$errline);
  47. }
  48. }
  49. class Exception {
  50. /* Emulate a Java exception, sort of... */
  51. var $message;
  52. var $type;
  53. function Exception($message, $type = 'FAILURE') {
  54. $this->message = $message;
  55. $this->type = $type;
  56. }
  57. function getMessage() {
  58. return $this->message;
  59. }
  60. function getType() {
  61. return $this->type;
  62. }
  63. }
  64. class Assert {
  65. function assert($boolean, $message=0) {
  66. if (! $boolean)
  67. $this->fail($message);
  68. }
  69. function assertEquals($expected, $actual, $message=0) {
  70. if (gettype($expected) != gettype($actual)) {
  71. $this->failNotEquals($expected, $actual, "expected", $message);
  72. return;
  73. }
  74. if (phpversion() < '4') {
  75. if (is_object($expected) or is_object($actual)
  76. or is_array($expected) or is_array($actual)) {
  77. $this->error("INVALID TEST: cannot compare arrays or objects in PHP3");
  78. return;
  79. }
  80. }
  81. if (phpversion() >= '4' && is_object($expected)) {
  82. if (get_class($expected) != get_class($actual)) {
  83. $this->failNotEquals($expected, $actual, "expected", $message);
  84. return;
  85. }
  86. if (method_exists($expected, "equals")) {
  87. if (! $expected->equals($actual)) {
  88. $this->failNotEquals($expected, $actual, "expected", $message);
  89. }
  90. return; // no further tests after equals()
  91. }
  92. }
  93. if (phpversion() >= '4.0.4') {
  94. if (is_null($expected) != is_null($actual)) {
  95. $this->failNotEquals($expected, $actual, "expected", $message);
  96. return;
  97. }
  98. }
  99. if ($expected != $actual) {
  100. $this->failNotEquals($expected, $actual, "expected", $message);
  101. }
  102. }
  103. function assertRegexp($regexp, $actual, $message=false) {
  104. if (! preg_match($regexp, $actual)) {
  105. $this->failNotEquals($regexp, $actual, "pattern", $message);
  106. }
  107. }
  108. function assertEqualsMultilineStrings($string0, $string1,
  109. $message="") {
  110. $lines0 = split("\n",$string0);
  111. $lines1 = split("\n",$string1);
  112. if (sizeof($lines0) != sizeof($lines1)) {
  113. $this->failNotEquals(sizeof($lines0)." line(s)",
  114. sizeof($lines1)." line(s)", "expected", $message);
  115. }
  116. for($i=0; $i< sizeof($lines0); $i++) {
  117. $this->assertEquals(trim($lines0[$i]),
  118. trim($lines1[$i]),
  119. "line ".($i+1)." of multiline strings differ. ".$message);
  120. }
  121. }
  122. function _formatValue($value, $class="") {
  123. $translateValue = $value;
  124. if (phpversion() >= '4.0.0') {
  125. if (is_object($value)) {
  126. if (method_exists($value, "toString") ) {
  127. $translateValue = $value->toString();
  128. }
  129. else {
  130. $translateValue = serialize($value);
  131. }
  132. }
  133. else if (is_array($value)) {
  134. $translateValue = serialize($value);
  135. }
  136. }
  137. $htmlValue = "<code class=\"$class\">"
  138. . htmlspecialchars($translateValue) . "</code>";
  139. if (phpversion() >= '4.0.0') {
  140. if (is_bool($value)) {
  141. $htmlValue = $value ? "<i>true</i>" : "<i>false</i>";
  142. }
  143. elseif (phpversion() >= '4.0.4' && is_null($value)) {
  144. $htmlValue = "<i>null</i>";
  145. }
  146. $htmlValue .= "&nbsp;&nbsp;&nbsp;<span class=\"typeinfo\">";
  147. $htmlValue .= "type:" . gettype($value);
  148. $htmlValue .= is_object($value) ? ", class:" . get_class($value) : "";
  149. $htmlValue .= "</span>";
  150. }
  151. return $htmlValue;
  152. }
  153. function failNotEquals($expected, $actual, $expected_label, $message=0) {
  154. // Private function for reporting failure to match.
  155. $str = $message ? ($message . ' ') : '';
  156. //$str .= "($expected_label/actual)<br>";
  157. $str .= "<br>";
  158. $str .= sprintf("%s<br>%s",
  159. $this->_formatValue($expected, "expected"),
  160. $this->_formatValue($actual, "actual"));
  161. $this->fail($str);
  162. }
  163. }
  164. class TestCase extends Assert /* implements Test */ {
  165. /* Defines context for running tests. Specific context -- such as
  166. instance variables, global variables, global state -- is defined
  167. by creating a subclass that specializes the setUp() and
  168. tearDown() methods. A specific test is defined by a subclass
  169. that specializes the runTest() method. */
  170. var $fName;
  171. var $fClassName;
  172. var $fResult;
  173. var $fExceptions = array();
  174. function TestCase($name) {
  175. $this->fName = $name;
  176. }
  177. function run($testResult=0) {
  178. /* Run this single test, by calling the run() method of the
  179. TestResult object which will in turn call the runBare() method
  180. of this object. That complication allows the TestResult object
  181. to do various kinds of progress reporting as it invokes each
  182. test. Create/obtain a TestResult object if none was passed in.
  183. Note that if a TestResult object was passed in, it must be by
  184. reference. */
  185. if (! $testResult)
  186. $testResult = $this->_createResult();
  187. $this->fResult = $testResult;
  188. $testResult->run(&$this);
  189. $this->fResult = 0;
  190. return $testResult;
  191. }
  192. function classname() {
  193. if (isset($this->fClassName)) {
  194. return $this->fClassName;
  195. } else {
  196. return get_class($this);
  197. }
  198. }
  199. function countTestCases() {
  200. return 1;
  201. }
  202. function runTest() {
  203. if (phpversion() >= '4') {
  204. global $PHPUnit_testRunning;
  205. eval('$PHPUnit_testRunning[0] = & $this;');
  206. // Saved ref to current TestCase, so that the error handler
  207. // can access it. This code won't even parse in PHP3, so we
  208. // hide it in an eval.
  209. $old_handler = set_error_handler("PHPUnit_error_handler");
  210. // errors will now be handled by our error handler
  211. }
  212. $name = $this->name();
  213. if (phpversion() >= '4' && ! method_exists($this, $name)) {
  214. $this->error("Method '$name' does not exist");
  215. }
  216. else
  217. $this->$name();
  218. /*
  219. if (phpversion() >= '4') {
  220. set_error_handler($old_handler); // revert to prior error handler
  221. $PHPUnit_testRunning = null;
  222. }
  223. */
  224. }
  225. function setUp() /* expect override */ {
  226. //print("TestCase::setUp()<br>\n");
  227. }
  228. function tearDown() /* possible override */ {
  229. //print("TestCase::tearDown()<br>\n");
  230. }
  231. ////////////////////////////////////////////////////////////////
  232. function _createResult() /* protected */ {
  233. /* override this to use specialized subclass of TestResult */
  234. return new TestResult;
  235. }
  236. function fail($message=0) {
  237. //printf("TestCase::fail(%s)<br>\n", ($message) ? $message : '');
  238. /* JUnit throws AssertionFailedError here. We just record the
  239. failure and carry on */
  240. $this->fExceptions[] = new Exception(&$message, 'FAILURE');
  241. }
  242. function error($message) {
  243. /* report error that requires correction in the test script
  244. itself, or (heaven forbid) in this testing infrastructure */
  245. $this->fExceptions[] = new Exception(&$message, 'ERROR');
  246. $this->fResult->stop(); // [does not work]
  247. }
  248. function failed() {
  249. reset($this->fExceptions);
  250. while (list($key, $exception) = each($this->fExceptions)) {
  251. if ($exception->type == 'FAILURE')
  252. return true;
  253. }
  254. return false;
  255. }
  256. function errored() {
  257. reset($this->fExceptions);
  258. while (list($key, $exception) = each($this->fExceptions)) {
  259. if ($exception->type == 'ERROR')
  260. return true;
  261. }
  262. return false;
  263. }
  264. function getExceptions() {
  265. return $this->fExceptions;
  266. }
  267. function name() {
  268. return $this->fName;
  269. }
  270. function runBare() {
  271. $this->setup();
  272. $this->runTest();
  273. $this->tearDown();
  274. }
  275. }
  276. class TestSuite /* implements Test */ {
  277. /* Compose a set of Tests (instances of TestCase or TestSuite), and
  278. run them all. */
  279. var $fTests = array();
  280. var $fClassname;
  281. function TestSuite($classname=false) {
  282. // Find all methods of the given class whose name starts with
  283. // "test" and add them to the test suite.
  284. // PHP3: We are just _barely_ able to do this with PHP's limited
  285. // introspection... Note that PHP seems to store method names in
  286. // lower case, and we have to avoid the constructor function for
  287. // the TestCase class superclass. Names of subclasses of TestCase
  288. // must not start with "Test" since such a class will have a
  289. // constructor method name also starting with "test" and we can't
  290. // distinquish such a construtor from the real test method names.
  291. // So don't name any TestCase subclasses as "Test..."!
  292. // PHP4: Never mind all that. We can now ignore constructor
  293. // methods, so a test class may be named "Test...".
  294. if (empty($classname))
  295. return;
  296. $this->fClassname = $classname;
  297. if (floor(phpversion()) >= 4) {
  298. // PHP4 introspection, submitted by Dylan Kuhn
  299. $names = get_class_methods($classname);
  300. while (list($key, $method) = @each($names)) {
  301. if (preg_match('/^test/', $method)) {
  302. $test = new $classname($method);
  303. if (strcasecmp($method, $classname) == 0 || is_subclass_of($test, $method)) {
  304. // Ignore the given method name since it is a constructor:
  305. // it's the name of our test class or it is the name of a
  306. // superclass of our test class. (This code smells funny.
  307. // Anyone got a better way?)
  308. //print "skipping $method<br>";
  309. }
  310. else {
  311. $this->addTest($test);
  312. }
  313. }
  314. }
  315. }
  316. else { // PHP3
  317. $dummy = new $classname("dummy");
  318. $names = (array) $dummy;
  319. while (list($key, $value) = each($names)) {
  320. $type = gettype($value);
  321. if ($type == "user function" && preg_match('/^test/', $key)
  322. && $key != "testcase") {
  323. $this->addTest(new $classname($key));
  324. }
  325. }
  326. }
  327. }
  328. function addTest($test) {
  329. /* Add TestCase or TestSuite to this TestSuite */
  330. $this->fTests[] = $test;
  331. }
  332. function run(&$testResult) {
  333. /* Run all TestCases and TestSuites comprising this TestSuite,
  334. accumulating results in the given TestResult object. */
  335. reset($this->fTests);
  336. while (list($na, $test) = each($this->fTests)) {
  337. if ($testResult->shouldStop())
  338. break;
  339. $test->run(&$testResult);
  340. }
  341. }
  342. function countTestCases() {
  343. /* Number of TestCases comprising this TestSuite (including those
  344. in any constituent TestSuites) */
  345. $count = 0;
  346. reset($fTests);
  347. while (list($na, $test_case) = each($this->fTests)) {
  348. $count += $test_case->countTestCases();
  349. }
  350. return $count;
  351. }
  352. }
  353. class TestFailure {
  354. /* Record failure of a single TestCase, associating it with the
  355. exception that occurred */
  356. var $fFailedTestName;
  357. var $fException;
  358. function TestFailure(&$test, &$exception) {
  359. $this->fFailedTestName = $test->name();
  360. $this->fException = $exception;
  361. }
  362. function getExceptions() {
  363. // deprecated
  364. return array($this->fException);
  365. }
  366. function getException() {
  367. return $this->fException;
  368. }
  369. function getTestName() {
  370. return $this->fFailedTestName;
  371. }
  372. }
  373. class TestResult {
  374. /* Collect the results of running a set of TestCases. */
  375. var $fFailures = array();
  376. var $fErrors = array();
  377. var $fRunTests = 0;
  378. var $fStop = false;
  379. function TestResult() { }
  380. function _endTest($test) /* protected */ {
  381. /* specialize this for end-of-test action, such as progress
  382. reports */
  383. }
  384. function addError($test, $exception) {
  385. $this->fErrors[] = new TestFailure(&$test, &$exception);
  386. }
  387. function addFailure($test, $exception) {
  388. $this->fFailures[] = new TestFailure(&$test, &$exception);
  389. }
  390. function getFailures() {
  391. return $this->fFailures;
  392. }
  393. function run($test) {
  394. /* Run a single TestCase in the context of this TestResult */
  395. $this->_startTest($test);
  396. $this->fRunTests++;
  397. $test->runBare();
  398. /* this is where JUnit would catch AssertionFailedError */
  399. $exceptions = $test->getExceptions();
  400. reset($exceptions);
  401. while (list($key, $exception) = each($exceptions)) {
  402. if ($exception->type == 'ERROR')
  403. $this->addError($test, $exception);
  404. else if ($exception->type == 'FAILURE')
  405. $this->addFailure($test, $exception);
  406. }
  407. // if ($exceptions)
  408. // $this->fFailures[] = new TestFailure(&$test, &$exceptions);
  409. $this->_endTest($test);
  410. }
  411. function countTests() {
  412. return $this->fRunTests;
  413. }
  414. function shouldStop() {
  415. return $this->fStop;
  416. }
  417. function _startTest($test) /* protected */ {
  418. /* specialize this for start-of-test actions */
  419. }
  420. function stop() {
  421. /* set indication that the test sequence should halt */
  422. $fStop = true;
  423. }
  424. function errorCount() {
  425. return count($this->fErrors);
  426. }
  427. function failureCount() {
  428. return count($this->fFailures);
  429. }
  430. function countFailures() {
  431. // deprecated
  432. return $this->failureCount();
  433. }
  434. }
  435. class TextTestResult extends TestResult {
  436. /* Specialize TestResult to produce text/html report */
  437. function TextTestResult() {
  438. $this->TestResult(); // call superclass constructor
  439. }
  440. function report() {
  441. /* report result of test run */
  442. $nRun = $this->countTests();
  443. $nFailures = $this->failureCount();
  444. $nErrors = $this->errorCount();
  445. printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's');
  446. printf("%s failure%s<br>\n", $nFailures, ($nFailures == 1) ? '' : 's');
  447. printf("%s error%s.<br>\n", $nErrors, ($nErrors == 1) ? '' : 's');
  448. if ($nFailures > 0) {
  449. print("<h2>Failures</h2>");
  450. print("<ol>\n");
  451. $failures = $this->getFailures();
  452. while (list($i, $failure) = each($failures)) {
  453. $failedTestName = $failure->getTestName();
  454. printf("<li>%s\n", $failedTestName);
  455. $exceptions = $failure->getExceptions();
  456. print("<ul>");
  457. while (list($na, $exception) = each($exceptions))
  458. printf("<li>%s\n", $exception->getMessage());
  459. print("</ul>");
  460. }
  461. print("</ol>\n");
  462. }
  463. if ($nErrors > 0) {
  464. print("<h2>Errors</h2>");
  465. print("<ol>\n");
  466. reset($this->fErrors);
  467. while (list($i, $error) = each($this->fErrors)) {
  468. $erroredTestName = $error->getTestName();
  469. printf("<li>%s\n", $failedTestName);
  470. $exception = $error->getException();
  471. print("<ul>");
  472. printf("<li>%s\n", $exception->getMessage());
  473. print("</ul>");
  474. }
  475. print("</ol>\n");
  476. }
  477. }
  478. function _startTest($test) {
  479. if (phpversion() > '4') {
  480. printf("%s - %s ", get_class($test), $test->name());
  481. } else {
  482. printf("%s ", $test->name());
  483. }
  484. flush();
  485. }
  486. function _endTest($test) {
  487. $outcome = $test->failed()
  488. ? "<font color=\"red\">FAIL</font>"
  489. : "<font color=\"green\">ok</font>";
  490. printf("$outcome<br>\n");
  491. flush();
  492. }
  493. }
  494. // PrettyTestResult created by BJG 17/11/01
  495. // beacuse the standard test result provided looks
  496. // rubbish.
  497. class PrettyTestResult extends TestResult {
  498. /* Specialize TestResult to produce text/html report */
  499. function PrettyTestResult() {
  500. $this->TestResult(); // call superclass constructor
  501. echo "<h2>Tests</h2>";
  502. echo "<TABLE CELLSPACING=\"1\" CELLPADDING=\"1\" BORDER=\"0\" WIDTH=\"90%\" ALIGN=\"CENTER\" class=\"details\">";
  503. echo "<TR><TH>Class</TH><TH>Function</TH><TH>Success?</TH></TR>";
  504. }
  505. function report() {
  506. echo "</TABLE>";
  507. /* report result of test run */
  508. $nRun = $this->countTests();
  509. $nFailures = $this->countFailures();
  510. echo "<h2>Summary</h2>";
  511. printf("<p>%s test%s run<br>", $nRun, ($nRun == 1) ? '' : 's');
  512. printf("%s failure%s.<br>\n", $nFailures, ($nFailures == 1) ? '' : 's');
  513. if ($nFailures == 0)
  514. return;
  515. echo "<h2>Failure Details</h2>";
  516. print("<ol>\n");
  517. $failures = $this->getFailures();
  518. while (list($i, $failure) = each($failures)) {
  519. $failedTestName = $failure->getTestName();
  520. printf("<li>%s\n", $failedTestName);
  521. $exceptions = $failure->getExceptions();
  522. print("<ul>");
  523. while (list($na, $exception) = each($exceptions))
  524. printf("<li>%s\n", $exception->getMessage());
  525. print("</ul>");
  526. }
  527. print("</ol>\n");
  528. }
  529. function _startTest($test) {
  530. printf("<TR><TD>%s </TD><TD>%s </TD>", $test->classname(),$test->name());
  531. flush();
  532. }
  533. function _endTest($test) {
  534. $outcome = $test->failed()
  535. ? " class=\"Failure\">FAIL"
  536. : " class=\"Pass\">OK";
  537. printf("<TD$outcome</TD></TR>");
  538. flush();
  539. }
  540. }
  541. class TestRunner {
  542. /* Run a suite of tests and report results. */
  543. function run($suite) {
  544. $result = new TextTestResult;
  545. $suite->run($result);
  546. $result->report();
  547. }
  548. }
  549. ?>