test_case.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?php
  2. /**
  3. * Base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: test_case.php 1726 2008-04-08 01:20:10Z lastcraft $
  7. */
  8. /**#@+
  9. * Includes SimpleTest files and defined the root constant
  10. * for dependent libraries.
  11. */
  12. require_once(dirname(__FILE__) . '/invoker.php');
  13. require_once(dirname(__FILE__) . '/errors.php');
  14. require_once(dirname(__FILE__) . '/compatibility.php');
  15. require_once(dirname(__FILE__) . '/scorer.php');
  16. require_once(dirname(__FILE__) . '/expectation.php');
  17. require_once(dirname(__FILE__) . '/dumper.php');
  18. require_once(dirname(__FILE__) . '/simpletest.php');
  19. if (version_compare(phpversion(), '5') >= 0) {
  20. require_once(dirname(__FILE__) . '/exceptions.php');
  21. require_once(dirname(__FILE__) . '/reflection_php5.php');
  22. } else {
  23. require_once(dirname(__FILE__) . '/reflection_php4.php');
  24. }
  25. if (! defined('SIMPLE_TEST')) {
  26. /**
  27. * @ignore
  28. */
  29. define('SIMPLE_TEST', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  30. }
  31. /**#@-*/
  32. /**
  33. * Basic test case. This is the smallest unit of a test
  34. * suite. It searches for
  35. * all methods that start with the the string "test" and
  36. * runs them. Working test cases extend this class.
  37. * @package SimpleTest
  38. * @subpackage UnitTester
  39. */
  40. class SimpleTestCase {
  41. var $_label = false;
  42. var $_reporter;
  43. var $_observers;
  44. var $_should_skip = false;
  45. /**
  46. * Sets up the test with no display.
  47. * @param string $label If no test name is given then
  48. * the class name is used.
  49. * @access public
  50. */
  51. function SimpleTestCase($label = false) {
  52. if ($label) {
  53. $this->_label = $label;
  54. }
  55. }
  56. /**
  57. * Accessor for the test name for subclasses.
  58. * @return string Name of the test.
  59. * @access public
  60. */
  61. function getLabel() {
  62. return $this->_label ? $this->_label : get_class($this);
  63. }
  64. /**
  65. * This is a placeholder for skipping tests. In this
  66. * method you place skipIf() and skipUnless() calls to
  67. * set the skipping state.
  68. * @access public
  69. */
  70. function skip() {
  71. }
  72. /**
  73. * Will issue a message to the reporter and tell the test
  74. * case to skip if the incoming flag is true.
  75. * @param string $should_skip Condition causing the tests to be skipped.
  76. * @param string $message Text of skip condition.
  77. * @access public
  78. */
  79. function skipIf($should_skip, $message = '%s') {
  80. if ($should_skip && ! $this->_should_skip) {
  81. $this->_should_skip = true;
  82. $message = sprintf($message, 'Skipping [' . get_class($this) . ']');
  83. $this->_reporter->paintSkip($message . $this->getAssertionLine());
  84. }
  85. }
  86. /**
  87. * Will issue a message to the reporter and tell the test
  88. * case to skip if the incoming flag is false.
  89. * @param string $shouldnt_skip Condition causing the tests to be run.
  90. * @param string $message Text of skip condition.
  91. * @access public
  92. */
  93. function skipUnless($shouldnt_skip, $message = false) {
  94. $this->skipIf(! $shouldnt_skip, $message);
  95. }
  96. /**
  97. * Used to invoke the single tests.
  98. * @return SimpleInvoker Individual test runner.
  99. * @access public
  100. */
  101. function &createInvoker() {
  102. $invoker = &new SimpleErrorTrappingInvoker(new SimpleInvoker($this));
  103. if (version_compare(phpversion(), '5') >= 0) {
  104. $invoker = &new SimpleExceptionTrappingInvoker($invoker);
  105. }
  106. return $invoker;
  107. }
  108. /**
  109. * Uses reflection to run every method within itself
  110. * starting with the string "test" unless a method
  111. * is specified.
  112. * @param SimpleReporter $reporter Current test reporter.
  113. * @return boolean True if all tests passed.
  114. * @access public
  115. */
  116. function run(&$reporter) {
  117. $context = &SimpleTest::getContext();
  118. $context->setTest($this);
  119. $context->setReporter($reporter);
  120. $this->_reporter = &$reporter;
  121. $started = false;
  122. foreach ($this->getTests() as $method) {
  123. if ($reporter->shouldInvoke($this->getLabel(), $method)) {
  124. $this->skip();
  125. if ($this->_should_skip) {
  126. break;
  127. }
  128. if (! $started) {
  129. $reporter->paintCaseStart($this->getLabel());
  130. $started = true;
  131. }
  132. $invoker = &$this->_reporter->createInvoker($this->createInvoker());
  133. $invoker->before($method);
  134. $invoker->invoke($method);
  135. $invoker->after($method);
  136. }
  137. }
  138. if ($started) {
  139. $reporter->paintCaseEnd($this->getLabel());
  140. }
  141. unset($this->_reporter);
  142. return $reporter->getStatus();
  143. }
  144. /**
  145. * Gets a list of test names. Normally that will
  146. * be all internal methods that start with the
  147. * name "test". This method should be overridden
  148. * if you want a different rule.
  149. * @return array List of test names.
  150. * @access public
  151. */
  152. function getTests() {
  153. $methods = array();
  154. foreach (get_class_methods(get_class($this)) as $method) {
  155. if ($this->_isTest($method)) {
  156. $methods[] = $method;
  157. }
  158. }
  159. return $methods;
  160. }
  161. /**
  162. * Tests to see if the method is a test that should
  163. * be run. Currently any method that starts with 'test'
  164. * is a candidate unless it is the constructor.
  165. * @param string $method Method name to try.
  166. * @return boolean True if test method.
  167. * @access protected
  168. */
  169. function _isTest($method) {
  170. if (strtolower(substr($method, 0, 4)) == 'test') {
  171. return ! SimpleTestCompatibility::isA($this, strtolower($method));
  172. }
  173. return false;
  174. }
  175. /**
  176. * Announces the start of the test.
  177. * @param string $method Test method just started.
  178. * @access public
  179. */
  180. function before($method) {
  181. $this->_reporter->paintMethodStart($method);
  182. $this->_observers = array();
  183. }
  184. /**
  185. * Sets up unit test wide variables at the start
  186. * of each test method. To be overridden in
  187. * actual user test cases.
  188. * @access public
  189. */
  190. function setUp() {
  191. }
  192. /**
  193. * Clears the data set in the setUp() method call.
  194. * To be overridden by the user in actual user test cases.
  195. * @access public
  196. */
  197. function tearDown() {
  198. }
  199. /**
  200. * Announces the end of the test. Includes private clean up.
  201. * @param string $method Test method just finished.
  202. * @access public
  203. */
  204. function after($method) {
  205. for ($i = 0; $i < count($this->_observers); $i++) {
  206. $this->_observers[$i]->atTestEnd($method, $this);
  207. }
  208. $this->_reporter->paintMethodEnd($method);
  209. }
  210. /**
  211. * Sets up an observer for the test end.
  212. * @param object $observer Must have atTestEnd()
  213. * method.
  214. * @access public
  215. */
  216. function tell(&$observer) {
  217. $this->_observers[] = &$observer;
  218. }
  219. /**
  220. * @deprecated
  221. */
  222. function pass($message = "Pass") {
  223. if (! isset($this->_reporter)) {
  224. trigger_error('Can only make assertions within test methods');
  225. }
  226. $this->_reporter->paintPass(
  227. $message . $this->getAssertionLine());
  228. return true;
  229. }
  230. /**
  231. * Sends a fail event with a message.
  232. * @param string $message Message to send.
  233. * @access public
  234. */
  235. function fail($message = "Fail") {
  236. if (! isset($this->_reporter)) {
  237. trigger_error('Can only make assertions within test methods');
  238. }
  239. $this->_reporter->paintFail(
  240. $message . $this->getAssertionLine());
  241. return false;
  242. }
  243. /**
  244. * Formats a PHP error and dispatches it to the
  245. * reporter.
  246. * @param integer $severity PHP error code.
  247. * @param string $message Text of error.
  248. * @param string $file File error occoured in.
  249. * @param integer $line Line number of error.
  250. * @access public
  251. */
  252. function error($severity, $message, $file, $line) {
  253. if (! isset($this->_reporter)) {
  254. trigger_error('Can only make assertions within test methods');
  255. }
  256. $this->_reporter->paintError(
  257. "Unexpected PHP error [$message] severity [$severity] in [$file line $line]");
  258. }
  259. /**
  260. * Formats an exception and dispatches it to the
  261. * reporter.
  262. * @param Exception $exception Object thrown.
  263. * @access public
  264. */
  265. function exception($exception) {
  266. $this->_reporter->paintException($exception);
  267. }
  268. /**
  269. * @deprecated
  270. */
  271. function signal($type, &$payload) {
  272. if (! isset($this->_reporter)) {
  273. trigger_error('Can only make assertions within test methods');
  274. }
  275. $this->_reporter->paintSignal($type, $payload);
  276. }
  277. /**
  278. * Runs an expectation directly, for extending the
  279. * tests with new expectation classes.
  280. * @param SimpleExpectation $expectation Expectation subclass.
  281. * @param mixed $compare Value to compare.
  282. * @param string $message Message to display.
  283. * @return boolean True on pass
  284. * @access public
  285. */
  286. function assert(&$expectation, $compare, $message = '%s') {
  287. if ($expectation->test($compare)) {
  288. return $this->pass(sprintf(
  289. $message,
  290. $expectation->overlayMessage($compare, $this->_reporter->getDumper())));
  291. } else {
  292. return $this->fail(sprintf(
  293. $message,
  294. $expectation->overlayMessage($compare, $this->_reporter->getDumper())));
  295. }
  296. }
  297. /**
  298. * @deprecated
  299. */
  300. function assertExpectation(&$expectation, $compare, $message = '%s') {
  301. return $this->assert($expectation, $compare, $message);
  302. }
  303. /**
  304. * Uses a stack trace to find the line of an assertion.
  305. * @return string Line number of first assert*
  306. * method embedded in format string.
  307. * @access public
  308. */
  309. function getAssertionLine() {
  310. $trace = new SimpleStackTrace(array('assert', 'expect', 'pass', 'fail', 'skip'));
  311. return $trace->traceMethod();
  312. }
  313. /**
  314. * Sends a formatted dump of a variable to the
  315. * test suite for those emergency debugging
  316. * situations.
  317. * @param mixed $variable Variable to display.
  318. * @param string $message Message to display.
  319. * @return mixed The original variable.
  320. * @access public
  321. */
  322. function dump($variable, $message = false) {
  323. $dumper = $this->_reporter->getDumper();
  324. $formatted = $dumper->dump($variable);
  325. if ($message) {
  326. $formatted = $message . "\n" . $formatted;
  327. }
  328. $this->_reporter->paintFormattedMessage($formatted);
  329. return $variable;
  330. }
  331. /**
  332. * @deprecated
  333. */
  334. function sendMessage($message) {
  335. $this->_reporter->PaintMessage($message);
  336. }
  337. /**
  338. * Accessor for the number of subtests including myelf.
  339. * @return integer Number of test cases.
  340. * @access public
  341. * @static
  342. */
  343. function getSize() {
  344. return 1;
  345. }
  346. }
  347. /**
  348. * Helps to extract test cases automatically from a file.
  349. */
  350. class SimpleFileLoader {
  351. /**
  352. * Builds a test suite from a library of test cases.
  353. * The new suite is composed into this one.
  354. * @param string $test_file File name of library with
  355. * test case classes.
  356. * @return TestSuite The new test suite.
  357. * @access public
  358. */
  359. function &load($test_file) {
  360. $existing_classes = get_declared_classes();
  361. $existing_globals = get_defined_vars();
  362. include_once($test_file);
  363. $new_globals = get_defined_vars();
  364. $this->_makeFileVariablesGlobal($existing_globals, $new_globals);
  365. $new_classes = array_diff(get_declared_classes(), $existing_classes);
  366. if (empty($new_classes)) {
  367. $new_classes = $this->_scrapeClassesFromFile($test_file);
  368. }
  369. $classes = $this->selectRunnableTests($new_classes);
  370. $suite = &$this->createSuiteFromClasses($test_file, $classes);
  371. return $suite;
  372. }
  373. /**
  374. * Imports new variables into the global namespace.
  375. * @param hash $existing Variables before the file was loaded.
  376. * @param hash $new Variables after the file was loaded.
  377. * @access private
  378. */
  379. function _makeFileVariablesGlobal($existing, $new) {
  380. $globals = array_diff(array_keys($new), array_keys($existing));
  381. foreach ($globals as $global) {
  382. $_GLOBALS[$global] = $new[$global];
  383. }
  384. }
  385. /**
  386. * Lookup classnames from file contents, in case the
  387. * file may have been included before.
  388. * Note: This is probably too clever by half. Figuring this
  389. * out after a failed test case is going to be tricky for us,
  390. * never mind the user. A test case should not be included
  391. * twice anyway.
  392. * @param string $test_file File name with classes.
  393. * @access private
  394. */
  395. function _scrapeClassesFromFile($test_file) {
  396. preg_match_all('~^\s*class\s+(\w+)(\s+(extends|implements)\s+\w+)*\s*\{~mi',
  397. file_get_contents($test_file),
  398. $matches );
  399. return $matches[1];
  400. }
  401. /**
  402. * Calculates the incoming test cases. Skips abstract
  403. * and ignored classes.
  404. * @param array $candidates Candidate classes.
  405. * @return array New classes which are test
  406. * cases that shouldn't be ignored.
  407. * @access public
  408. */
  409. function selectRunnableTests($candidates) {
  410. $classes = array();
  411. foreach ($candidates as $class) {
  412. if (TestSuite::getBaseTestCase($class)) {
  413. $reflection = new SimpleReflection($class);
  414. if ($reflection->isAbstract()) {
  415. SimpleTest::ignore($class);
  416. } else {
  417. $classes[] = $class;
  418. }
  419. }
  420. }
  421. return $classes;
  422. }
  423. /**
  424. * Builds a test suite from a class list.
  425. * @param string $title Title of new group.
  426. * @param array $classes Test classes.
  427. * @return TestSuite Group loaded with the new
  428. * test cases.
  429. * @access public
  430. */
  431. function &createSuiteFromClasses($title, $classes) {
  432. if (count($classes) == 0) {
  433. $suite = &new BadTestSuite($title, "No runnable test cases in [$title]");
  434. return $suite;
  435. }
  436. SimpleTest::ignoreParentsIfIgnored($classes);
  437. $suite = &new TestSuite($title);
  438. foreach ($classes as $class) {
  439. if (! SimpleTest::isIgnored($class)) {
  440. $suite->addTestClass($class);
  441. }
  442. }
  443. return $suite;
  444. }
  445. }
  446. /**
  447. * This is a composite test class for combining
  448. * test cases and other RunnableTest classes into
  449. * a group test.
  450. * @package SimpleTest
  451. * @subpackage UnitTester
  452. */
  453. class TestSuite {
  454. var $_label;
  455. var $_test_cases;
  456. /**
  457. * Sets the name of the test suite.
  458. * @param string $label Name sent at the start and end
  459. * of the test.
  460. * @access public
  461. */
  462. function TestSuite($label = false) {
  463. $this->_label = $label;
  464. $this->_test_cases = array();
  465. }
  466. /**
  467. * Accessor for the test name for subclasses. If the suite
  468. * wraps a single test case the label defaults to the name of that test.
  469. * @return string Name of the test.
  470. * @access public
  471. */
  472. function getLabel() {
  473. if (! $this->_label) {
  474. return ($this->getSize() == 1) ?
  475. get_class($this->_test_cases[0]) : get_class($this);
  476. } else {
  477. return $this->_label;
  478. }
  479. }
  480. /**
  481. * @deprecated
  482. */
  483. function addTestCase(&$test_case) {
  484. $this->_test_cases[] = &$test_case;
  485. }
  486. /**
  487. * @deprecated
  488. */
  489. function addTestClass($class) {
  490. if (TestSuite::getBaseTestCase($class) == 'testsuite') {
  491. $this->_test_cases[] = &new $class();
  492. } else {
  493. $this->_test_cases[] = $class;
  494. }
  495. }
  496. /**
  497. * Adds a test into the suite by instance or class. The class will
  498. * be instantiated if it's a test suite.
  499. * @param SimpleTestCase $test_case Suite or individual test
  500. * case implementing the
  501. * runnable test interface.
  502. * @access public
  503. */
  504. function add(&$test_case) {
  505. $class = null;
  506. if (! is_string($test_case)) {
  507. $this->_test_cases[] = &$test_case;
  508. } elseif (TestSuite::getBaseTestCase($class) == 'testsuite') {
  509. $this->_test_cases[] = &new $class();
  510. } else {
  511. $this->_test_cases[] = $class;
  512. }
  513. }
  514. /**
  515. * @deprecated
  516. */
  517. function addTestFile($test_file) {
  518. $this->addFile($test_file);
  519. }
  520. /**
  521. * Builds a test suite from a library of test cases.
  522. * The new suite is composed into this one.
  523. * @param string $test_file File name of library with
  524. * test case classes.
  525. * @access public
  526. */
  527. function addFile($test_file) {
  528. $extractor = new SimpleFileLoader();
  529. $this->add($extractor->load($test_file));
  530. }
  531. /**
  532. * Delegates to a visiting collector to add test
  533. * files.
  534. * @param string $path Path to scan from.
  535. * @param SimpleCollector $collector Directory scanner.
  536. * @access public
  537. */
  538. function collect($path, &$collector) {
  539. $collector->collect($this, $path);
  540. }
  541. /**
  542. * Invokes run() on all of the held test cases, instantiating
  543. * them if necessary.
  544. * @param SimpleReporter $reporter Current test reporter.
  545. * @access public
  546. */
  547. function run(&$reporter) {
  548. $reporter->paintGroupStart($this->getLabel(), $this->getSize());
  549. for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) {
  550. if (is_string($this->_test_cases[$i])) {
  551. $class = $this->_test_cases[$i];
  552. $test = &new $class();
  553. $test->run($reporter);
  554. unset($test);
  555. } else {
  556. $this->_test_cases[$i]->run($reporter);
  557. }
  558. }
  559. $reporter->paintGroupEnd($this->getLabel());
  560. return $reporter->getStatus();
  561. }
  562. /**
  563. * Number of contained test cases.
  564. * @return integer Total count of cases in the group.
  565. * @access public
  566. */
  567. function getSize() {
  568. $count = 0;
  569. foreach ($this->_test_cases as $case) {
  570. if (is_string($case)) {
  571. if (! SimpleTest::isIgnored($case)) {
  572. $count++;
  573. }
  574. } else {
  575. $count += $case->getSize();
  576. }
  577. }
  578. return $count;
  579. }
  580. /**
  581. * Test to see if a class is derived from the
  582. * SimpleTestCase class.
  583. * @param string $class Class name.
  584. * @access public
  585. * @static
  586. */
  587. function getBaseTestCase($class) {
  588. while ($class = get_parent_class($class)) {
  589. $class = strtolower($class);
  590. if ($class == 'simpletestcase' || $class == 'testsuite') {
  591. return $class;
  592. }
  593. }
  594. return false;
  595. }
  596. }
  597. /**
  598. * @package SimpleTest
  599. * @subpackage UnitTester
  600. * @deprecated
  601. */
  602. class GroupTest extends TestSuite { }
  603. /**
  604. * This is a failing group test for when a test suite hasn't
  605. * loaded properly.
  606. * @package SimpleTest
  607. * @subpackage UnitTester
  608. */
  609. class BadTestSuite {
  610. var $_label;
  611. var $_error;
  612. /**
  613. * Sets the name of the test suite and error message.
  614. * @param string $label Name sent at the start and end
  615. * of the test.
  616. * @access public
  617. */
  618. function BadTestSuite($label, $error) {
  619. $this->_label = $label;
  620. $this->_error = $error;
  621. }
  622. /**
  623. * Accessor for the test name for subclasses.
  624. * @return string Name of the test.
  625. * @access public
  626. */
  627. function getLabel() {
  628. return $this->_label;
  629. }
  630. /**
  631. * Sends a single error to the reporter.
  632. * @param SimpleReporter $reporter Current test reporter.
  633. * @access public
  634. */
  635. function run(&$reporter) {
  636. $reporter->paintGroupStart($this->getLabel(), $this->getSize());
  637. $reporter->paintFail('Bad TestSuite [' . $this->getLabel() .
  638. '] with error [' . $this->_error . ']');
  639. $reporter->paintGroupEnd($this->getLabel());
  640. return $reporter->getStatus();
  641. }
  642. /**
  643. * Number of contained test cases. Always zero.
  644. * @return integer Total count of cases in the group.
  645. * @access public
  646. */
  647. function getSize() {
  648. return 0;
  649. }
  650. }
  651. /**
  652. * @package SimpleTest
  653. * @subpackage UnitTester
  654. * @deprecated
  655. */
  656. class BadGroupTest extends BadTestSuite { }
  657. ?>