PageRenderTime 85ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/simpletest/scorer.php

https://github.com/quarkness/piwik
PHP | 863 lines | 307 code | 81 blank | 475 comment | 32 complexity | 705111c960989fb085ba9514301f673a MD5 | raw file
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: scorer.php 1723 2008-04-08 00:34:10Z lastcraft $
  7. */
  8. /**#@+*/
  9. require_once(dirname(__FILE__) . '/invoker.php');
  10. /**#@-*/
  11. /**
  12. * Can receive test events and display them. Display
  13. * is achieved by making display methods available
  14. * and visiting the incoming event.
  15. * @package SimpleTest
  16. * @subpackage UnitTester
  17. * @abstract
  18. */
  19. class SimpleScorer {
  20. var $_passes;
  21. var $_fails;
  22. var $_exceptions;
  23. var $_is_dry_run;
  24. /**
  25. * Starts the test run with no results.
  26. * @access public
  27. */
  28. function SimpleScorer() {
  29. $this->_passes = 0;
  30. $this->_fails = 0;
  31. $this->_exceptions = 0;
  32. $this->_is_dry_run = false;
  33. }
  34. /**
  35. * Signals that the next evaluation will be a dry
  36. * run. That is, the structure events will be
  37. * recorded, but no tests will be run.
  38. * @param boolean $is_dry Dry run if true.
  39. * @access public
  40. */
  41. function makeDry($is_dry = true) {
  42. $this->_is_dry_run = $is_dry;
  43. }
  44. /**
  45. * The reporter has a veto on what should be run.
  46. * @param string $test_case_name name of test case.
  47. * @param string $method Name of test method.
  48. * @access public
  49. */
  50. function shouldInvoke($test_case_name, $method) {
  51. return ! $this->_is_dry_run;
  52. }
  53. /**
  54. * Can wrap the invoker in preperation for running
  55. * a test.
  56. * @param SimpleInvoker $invoker Individual test runner.
  57. * @return SimpleInvoker Wrapped test runner.
  58. * @access public
  59. */
  60. function &createInvoker(&$invoker) {
  61. return $invoker;
  62. }
  63. /**
  64. * Accessor for current status. Will be false
  65. * if there have been any failures or exceptions.
  66. * Used for command line tools.
  67. * @return boolean True if no failures.
  68. * @access public
  69. */
  70. function getStatus() {
  71. if ($this->_exceptions + $this->_fails > 0) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. /**
  77. * Paints the start of a group test.
  78. * @param string $test_name Name of test or other label.
  79. * @param integer $size Number of test cases starting.
  80. * @access public
  81. */
  82. function paintGroupStart($test_name, $size) {
  83. }
  84. /**
  85. * Paints the end of a group test.
  86. * @param string $test_name Name of test or other label.
  87. * @access public
  88. */
  89. function paintGroupEnd($test_name) {
  90. }
  91. /**
  92. * Paints the start of a test case.
  93. * @param string $test_name Name of test or other label.
  94. * @access public
  95. */
  96. function paintCaseStart($test_name) {
  97. }
  98. /**
  99. * Paints the end of a test case.
  100. * @param string $test_name Name of test or other label.
  101. * @access public
  102. */
  103. function paintCaseEnd($test_name) {
  104. }
  105. /**
  106. * Paints the start of a test method.
  107. * @param string $test_name Name of test or other label.
  108. * @access public
  109. */
  110. function paintMethodStart($test_name) {
  111. }
  112. /**
  113. * Paints the end of a test method.
  114. * @param string $test_name Name of test or other label.
  115. * @access public
  116. */
  117. function paintMethodEnd($test_name) {
  118. }
  119. /**
  120. * Increments the pass count.
  121. * @param string $message Message is ignored.
  122. * @access public
  123. */
  124. function paintPass($message) {
  125. $this->_passes++;
  126. }
  127. /**
  128. * Increments the fail count.
  129. * @param string $message Message is ignored.
  130. * @access public
  131. */
  132. function paintFail($message) {
  133. $this->_fails++;
  134. }
  135. /**
  136. * Deals with PHP 4 throwing an error.
  137. * @param string $message Text of error formatted by
  138. * the test case.
  139. * @access public
  140. */
  141. function paintError($message) {
  142. $this->_exceptions++;
  143. }
  144. /**
  145. * Deals with PHP 5 throwing an exception.
  146. * @param Exception $exception The actual exception thrown.
  147. * @access public
  148. */
  149. function paintException($exception) {
  150. $this->_exceptions++;
  151. }
  152. /**
  153. * Prints the message for skipping tests.
  154. * @param string $message Text of skip condition.
  155. * @access public
  156. */
  157. function paintSkip($message) {
  158. }
  159. /**
  160. * Accessor for the number of passes so far.
  161. * @return integer Number of passes.
  162. * @access public
  163. */
  164. function getPassCount() {
  165. return $this->_passes;
  166. }
  167. /**
  168. * Accessor for the number of fails so far.
  169. * @return integer Number of fails.
  170. * @access public
  171. */
  172. function getFailCount() {
  173. return $this->_fails;
  174. }
  175. /**
  176. * Accessor for the number of untrapped errors
  177. * so far.
  178. * @return integer Number of exceptions.
  179. * @access public
  180. */
  181. function getExceptionCount() {
  182. return $this->_exceptions;
  183. }
  184. /**
  185. * Paints a simple supplementary message.
  186. * @param string $message Text to display.
  187. * @access public
  188. */
  189. function paintMessage($message) {
  190. }
  191. /**
  192. * Paints a formatted ASCII message such as a
  193. * variable dump.
  194. * @param string $message Text to display.
  195. * @access public
  196. */
  197. function paintFormattedMessage($message) {
  198. }
  199. /**
  200. * By default just ignores user generated events.
  201. * @param string $type Event type as text.
  202. * @param mixed $payload Message or object.
  203. * @access public
  204. */
  205. function paintSignal($type, $payload) {
  206. }
  207. }
  208. /**
  209. * Recipient of generated test messages that can display
  210. * page footers and headers. Also keeps track of the
  211. * test nesting. This is the main base class on which
  212. * to build the finished test (page based) displays.
  213. * @package SimpleTest
  214. * @subpackage UnitTester
  215. */
  216. class SimpleReporter extends SimpleScorer {
  217. var $_test_stack;
  218. var $_size;
  219. var $_progress;
  220. /**
  221. * Starts the display with no results in.
  222. * @access public
  223. */
  224. function SimpleReporter() {
  225. $this->SimpleScorer();
  226. $this->_test_stack = array();
  227. $this->_size = null;
  228. $this->_progress = 0;
  229. }
  230. /**
  231. * Gets the formatter for variables and other small
  232. * generic data items.
  233. * @return SimpleDumper Formatter.
  234. * @access public
  235. */
  236. function getDumper() {
  237. return new SimpleDumper();
  238. }
  239. /**
  240. * Paints the start of a group test. Will also paint
  241. * the page header and footer if this is the
  242. * first test. Will stash the size if the first
  243. * start.
  244. * @param string $test_name Name of test that is starting.
  245. * @param integer $size Number of test cases starting.
  246. * @access public
  247. */
  248. function paintGroupStart($test_name, $size) {
  249. if (! isset($this->_size)) {
  250. $this->_size = $size;
  251. }
  252. if (count($this->_test_stack) == 0) {
  253. $this->paintHeader($test_name);
  254. }
  255. $this->_test_stack[] = $test_name;
  256. }
  257. /**
  258. * Paints the end of a group test. Will paint the page
  259. * footer if the stack of tests has unwound.
  260. * @param string $test_name Name of test that is ending.
  261. * @param integer $progress Number of test cases ending.
  262. * @access public
  263. */
  264. function paintGroupEnd($test_name) {
  265. array_pop($this->_test_stack);
  266. if (count($this->_test_stack) == 0) {
  267. $this->paintFooter($test_name);
  268. }
  269. }
  270. /**
  271. * Paints the start of a test case. Will also paint
  272. * the page header and footer if this is the
  273. * first test. Will stash the size if the first
  274. * start.
  275. * @param string $test_name Name of test that is starting.
  276. * @access public
  277. */
  278. function paintCaseStart($test_name) {
  279. if (! isset($this->_size)) {
  280. $this->_size = 1;
  281. }
  282. if (count($this->_test_stack) == 0) {
  283. $this->paintHeader($test_name);
  284. }
  285. $this->_test_stack[] = $test_name;
  286. }
  287. /**
  288. * Paints the end of a test case. Will paint the page
  289. * footer if the stack of tests has unwound.
  290. * @param string $test_name Name of test that is ending.
  291. * @access public
  292. */
  293. function paintCaseEnd($test_name) {
  294. $this->_progress++;
  295. array_pop($this->_test_stack);
  296. if (count($this->_test_stack) == 0) {
  297. $this->paintFooter($test_name);
  298. }
  299. }
  300. /**
  301. * Paints the start of a test method.
  302. * @param string $test_name Name of test that is starting.
  303. * @access public
  304. */
  305. function paintMethodStart($test_name) {
  306. $this->_test_stack[] = $test_name;
  307. }
  308. /**
  309. * Paints the end of a test method. Will paint the page
  310. * footer if the stack of tests has unwound.
  311. * @param string $test_name Name of test that is ending.
  312. * @access public
  313. */
  314. function paintMethodEnd($test_name) {
  315. array_pop($this->_test_stack);
  316. }
  317. /**
  318. * Paints the test document header.
  319. * @param string $test_name First test top level
  320. * to start.
  321. * @access public
  322. * @abstract
  323. */
  324. function paintHeader($test_name) {
  325. }
  326. /**
  327. * Paints the test document footer.
  328. * @param string $test_name The top level test.
  329. * @access public
  330. * @abstract
  331. */
  332. function paintFooter($test_name) {
  333. }
  334. /**
  335. * Accessor for internal test stack. For
  336. * subclasses that need to see the whole test
  337. * history for display purposes.
  338. * @return array List of methods in nesting order.
  339. * @access public
  340. */
  341. function getTestList() {
  342. return $this->_test_stack;
  343. }
  344. /**
  345. * Accessor for total test size in number
  346. * of test cases. Null until the first
  347. * test is started.
  348. * @return integer Total number of cases at start.
  349. * @access public
  350. */
  351. function getTestCaseCount() {
  352. return $this->_size;
  353. }
  354. /**
  355. * Accessor for the number of test cases
  356. * completed so far.
  357. * @return integer Number of ended cases.
  358. * @access public
  359. */
  360. function getTestCaseProgress() {
  361. return $this->_progress;
  362. }
  363. /**
  364. * Static check for running in the comand line.
  365. * @return boolean True if CLI.
  366. * @access public
  367. * @static
  368. */
  369. static function inCli() {
  370. return php_sapi_name() == 'cli';
  371. }
  372. }
  373. /**
  374. * For modifying the behaviour of the visual reporters.
  375. * @package SimpleTest
  376. * @subpackage UnitTester
  377. */
  378. class SimpleReporterDecorator {
  379. var $_reporter;
  380. /**
  381. * Mediates between the reporter and the test case.
  382. * @param SimpleScorer $reporter Reporter to receive events.
  383. */
  384. function SimpleReporterDecorator(&$reporter) {
  385. $this->_reporter = &$reporter;
  386. }
  387. /**
  388. * Signals that the next evaluation will be a dry
  389. * run. That is, the structure events will be
  390. * recorded, but no tests will be run.
  391. * @param boolean $is_dry Dry run if true.
  392. * @access public
  393. */
  394. function makeDry($is_dry = true) {
  395. $this->_reporter->makeDry($is_dry);
  396. }
  397. /**
  398. * Accessor for current status. Will be false
  399. * if there have been any failures or exceptions.
  400. * Used for command line tools.
  401. * @return boolean True if no failures.
  402. * @access public
  403. */
  404. function getStatus() {
  405. return $this->_reporter->getStatus();
  406. }
  407. /**
  408. * The reporter has a veto on what should be run.
  409. * @param string $test_case_name name of test case.
  410. * @param string $method Name of test method.
  411. * @return boolean True if test should be run.
  412. * @access public
  413. */
  414. function shouldInvoke($test_case_name, $method) {
  415. return $this->_reporter->shouldInvoke($test_case_name, $method);
  416. }
  417. /**
  418. * Can wrap the invoker in preperation for running
  419. * a test.
  420. * @param SimpleInvoker $invoker Individual test runner.
  421. * @return SimpleInvoker Wrapped test runner.
  422. * @access public
  423. */
  424. function &createInvoker(&$invoker) {
  425. return $this->_reporter->createInvoker($invoker);
  426. }
  427. /**
  428. * Gets the formatter for variables and other small
  429. * generic data items.
  430. * @return SimpleDumper Formatter.
  431. * @access public
  432. */
  433. function getDumper() {
  434. return $this->_reporter->getDumper();
  435. }
  436. /**
  437. * Paints the start of a group test.
  438. * @param string $test_name Name of test or other label.
  439. * @param integer $size Number of test cases starting.
  440. * @access public
  441. */
  442. function paintGroupStart($test_name, $size) {
  443. $this->_reporter->paintGroupStart($test_name, $size);
  444. }
  445. /**
  446. * Paints the end of a group test.
  447. * @param string $test_name Name of test or other label.
  448. * @access public
  449. */
  450. function paintGroupEnd($test_name) {
  451. $this->_reporter->paintGroupEnd($test_name);
  452. }
  453. /**
  454. * Paints the start of a test case.
  455. * @param string $test_name Name of test or other label.
  456. * @access public
  457. */
  458. function paintCaseStart($test_name) {
  459. $this->_reporter->paintCaseStart($test_name);
  460. }
  461. /**
  462. * Paints the end of a test case.
  463. * @param string $test_name Name of test or other label.
  464. * @access public
  465. */
  466. function paintCaseEnd($test_name) {
  467. $this->_reporter->paintCaseEnd($test_name);
  468. }
  469. /**
  470. * Paints the start of a test method.
  471. * @param string $test_name Name of test or other label.
  472. * @access public
  473. */
  474. function paintMethodStart($test_name) {
  475. $this->_reporter->paintMethodStart($test_name);
  476. }
  477. /**
  478. * Paints the end of a test method.
  479. * @param string $test_name Name of test or other label.
  480. * @access public
  481. */
  482. function paintMethodEnd($test_name) {
  483. $this->_reporter->paintMethodEnd($test_name);
  484. }
  485. /**
  486. * Chains to the wrapped reporter.
  487. * @param string $message Message is ignored.
  488. * @access public
  489. */
  490. function paintPass($message) {
  491. $this->_reporter->paintPass($message);
  492. }
  493. /**
  494. * Chains to the wrapped reporter.
  495. * @param string $message Message is ignored.
  496. * @access public
  497. */
  498. function paintFail($message) {
  499. $this->_reporter->paintFail($message);
  500. }
  501. /**
  502. * Chains to the wrapped reporter.
  503. * @param string $message Text of error formatted by
  504. * the test case.
  505. * @access public
  506. */
  507. function paintError($message) {
  508. $this->_reporter->paintError($message);
  509. }
  510. /**
  511. * Chains to the wrapped reporter.
  512. * @param Exception $exception Exception to show.
  513. * @access public
  514. */
  515. function paintException($exception) {
  516. $this->_reporter->paintException($exception);
  517. }
  518. /**
  519. * Prints the message for skipping tests.
  520. * @param string $message Text of skip condition.
  521. * @access public
  522. */
  523. function paintSkip($message) {
  524. $this->_reporter->paintSkip($message);
  525. }
  526. /**
  527. * Chains to the wrapped reporter.
  528. * @param string $message Text to display.
  529. * @access public
  530. */
  531. function paintMessage($message) {
  532. $this->_reporter->paintMessage($message);
  533. }
  534. /**
  535. * Chains to the wrapped reporter.
  536. * @param string $message Text to display.
  537. * @access public
  538. */
  539. function paintFormattedMessage($message) {
  540. $this->_reporter->paintFormattedMessage($message);
  541. }
  542. /**
  543. * Chains to the wrapped reporter.
  544. * @param string $type Event type as text.
  545. * @param mixed $payload Message or object.
  546. * @return boolean Should return false if this
  547. * type of signal should fail the
  548. * test suite.
  549. * @access public
  550. */
  551. function paintSignal($type, &$payload) {
  552. $this->_reporter->paintSignal($type, $payload);
  553. }
  554. }
  555. /**
  556. * For sending messages to multiple reporters at
  557. * the same time.
  558. * @package SimpleTest
  559. * @subpackage UnitTester
  560. */
  561. class MultipleReporter {
  562. var $_reporters = array();
  563. /**
  564. * Adds a reporter to the subscriber list.
  565. * @param SimpleScorer $reporter Reporter to receive events.
  566. * @access public
  567. */
  568. function attachReporter(&$reporter) {
  569. $this->_reporters[] = &$reporter;
  570. }
  571. /**
  572. * Signals that the next evaluation will be a dry
  573. * run. That is, the structure events will be
  574. * recorded, but no tests will be run.
  575. * @param boolean $is_dry Dry run if true.
  576. * @access public
  577. */
  578. function makeDry($is_dry = true) {
  579. for ($i = 0; $i < count($this->_reporters); $i++) {
  580. $this->_reporters[$i]->makeDry($is_dry);
  581. }
  582. }
  583. /**
  584. * Accessor for current status. Will be false
  585. * if there have been any failures or exceptions.
  586. * If any reporter reports a failure, the whole
  587. * suite fails.
  588. * @return boolean True if no failures.
  589. * @access public
  590. */
  591. function getStatus() {
  592. for ($i = 0; $i < count($this->_reporters); $i++) {
  593. if (! $this->_reporters[$i]->getStatus()) {
  594. return false;
  595. }
  596. }
  597. return true;
  598. }
  599. /**
  600. * The reporter has a veto on what should be run.
  601. * It requires all reporters to want to run the method.
  602. * @param string $test_case_name name of test case.
  603. * @param string $method Name of test method.
  604. * @access public
  605. */
  606. function shouldInvoke($test_case_name, $method) {
  607. for ($i = 0; $i < count($this->_reporters); $i++) {
  608. if (! $this->_reporters[$i]->shouldInvoke($test_case_name, $method)) {
  609. return false;
  610. }
  611. }
  612. return true;
  613. }
  614. /**
  615. * Every reporter gets a chance to wrap the invoker.
  616. * @param SimpleInvoker $invoker Individual test runner.
  617. * @return SimpleInvoker Wrapped test runner.
  618. * @access public
  619. */
  620. function &createInvoker(&$invoker) {
  621. for ($i = 0; $i < count($this->_reporters); $i++) {
  622. $invoker = &$this->_reporters[$i]->createInvoker($invoker);
  623. }
  624. return $invoker;
  625. }
  626. /**
  627. * Gets the formatter for variables and other small
  628. * generic data items.
  629. * @return SimpleDumper Formatter.
  630. * @access public
  631. */
  632. function getDumper() {
  633. return new SimpleDumper();
  634. }
  635. /**
  636. * Paints the start of a group test.
  637. * @param string $test_name Name of test or other label.
  638. * @param integer $size Number of test cases starting.
  639. * @access public
  640. */
  641. function paintGroupStart($test_name, $size) {
  642. for ($i = 0; $i < count($this->_reporters); $i++) {
  643. $this->_reporters[$i]->paintGroupStart($test_name, $size);
  644. }
  645. }
  646. /**
  647. * Paints the end of a group test.
  648. * @param string $test_name Name of test or other label.
  649. * @access public
  650. */
  651. function paintGroupEnd($test_name) {
  652. for ($i = 0; $i < count($this->_reporters); $i++) {
  653. $this->_reporters[$i]->paintGroupEnd($test_name);
  654. }
  655. }
  656. /**
  657. * Paints the start of a test case.
  658. * @param string $test_name Name of test or other label.
  659. * @access public
  660. */
  661. function paintCaseStart($test_name) {
  662. for ($i = 0; $i < count($this->_reporters); $i++) {
  663. $this->_reporters[$i]->paintCaseStart($test_name);
  664. }
  665. }
  666. /**
  667. * Paints the end of a test case.
  668. * @param string $test_name Name of test or other label.
  669. * @access public
  670. */
  671. function paintCaseEnd($test_name) {
  672. for ($i = 0; $i < count($this->_reporters); $i++) {
  673. $this->_reporters[$i]->paintCaseEnd($test_name);
  674. }
  675. }
  676. /**
  677. * Paints the start of a test method.
  678. * @param string $test_name Name of test or other label.
  679. * @access public
  680. */
  681. function paintMethodStart($test_name) {
  682. for ($i = 0; $i < count($this->_reporters); $i++) {
  683. $this->_reporters[$i]->paintMethodStart($test_name);
  684. }
  685. }
  686. /**
  687. * Paints the end of a test method.
  688. * @param string $test_name Name of test or other label.
  689. * @access public
  690. */
  691. function paintMethodEnd($test_name) {
  692. for ($i = 0; $i < count($this->_reporters); $i++) {
  693. $this->_reporters[$i]->paintMethodEnd($test_name);
  694. }
  695. }
  696. /**
  697. * Chains to the wrapped reporter.
  698. * @param string $message Message is ignored.
  699. * @access public
  700. */
  701. function paintPass($message) {
  702. for ($i = 0; $i < count($this->_reporters); $i++) {
  703. $this->_reporters[$i]->paintPass($message);
  704. }
  705. }
  706. /**
  707. * Chains to the wrapped reporter.
  708. * @param string $message Message is ignored.
  709. * @access public
  710. */
  711. function paintFail($message) {
  712. for ($i = 0; $i < count($this->_reporters); $i++) {
  713. $this->_reporters[$i]->paintFail($message);
  714. }
  715. }
  716. /**
  717. * Chains to the wrapped reporter.
  718. * @param string $message Text of error formatted by
  719. * the test case.
  720. * @access public
  721. */
  722. function paintError($message) {
  723. for ($i = 0; $i < count($this->_reporters); $i++) {
  724. $this->_reporters[$i]->paintError($message);
  725. }
  726. }
  727. /**
  728. * Chains to the wrapped reporter.
  729. * @param Exception $exception Exception to display.
  730. * @access public
  731. */
  732. function paintException($exception) {
  733. for ($i = 0; $i < count($this->_reporters); $i++) {
  734. $this->_reporters[$i]->paintException($exception);
  735. }
  736. }
  737. /**
  738. * Prints the message for skipping tests.
  739. * @param string $message Text of skip condition.
  740. * @access public
  741. */
  742. function paintSkip($message) {
  743. for ($i = 0; $i < count($this->_reporters); $i++) {
  744. $this->_reporters[$i]->paintSkip($message);
  745. }
  746. }
  747. /**
  748. * Chains to the wrapped reporter.
  749. * @param string $message Text to display.
  750. * @access public
  751. */
  752. function paintMessage($message) {
  753. for ($i = 0; $i < count($this->_reporters); $i++) {
  754. $this->_reporters[$i]->paintMessage($message);
  755. }
  756. }
  757. /**
  758. * Chains to the wrapped reporter.
  759. * @param string $message Text to display.
  760. * @access public
  761. */
  762. function paintFormattedMessage($message) {
  763. for ($i = 0; $i < count($this->_reporters); $i++) {
  764. $this->_reporters[$i]->paintFormattedMessage($message);
  765. }
  766. }
  767. /**
  768. * Chains to the wrapped reporter.
  769. * @param string $type Event type as text.
  770. * @param mixed $payload Message or object.
  771. * @return boolean Should return false if this
  772. * type of signal should fail the
  773. * test suite.
  774. * @access public
  775. */
  776. function paintSignal($type, &$payload) {
  777. for ($i = 0; $i < count($this->_reporters); $i++) {
  778. $this->_reporters[$i]->paintSignal($type, $payload);
  779. }
  780. }
  781. }
  782. ?>