PageRenderTime 91ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/simpletest/scorer.php

https://github.com/3scale/3scale_ws_api_for_php
PHP | 875 lines | 314 code | 82 blank | 479 comment | 34 complexity | 78f754bdaf75b3cd1072dbe9787a0668 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: scorer.php 2011 2011-04-29 08:22:48Z pp11 $
  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. private $passes;
  21. private $fails;
  22. private $exceptions;
  23. private $is_dry_run;
  24. /**
  25. * Starts the test run with no results.
  26. * @access public
  27. */
  28. function __construct() {
  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. * privateiable 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. private $test_stack;
  218. private $size;
  219. private $progress;
  220. /**
  221. * Starts the display with no results in.
  222. * @access public
  223. */
  224. function __construct() {
  225. parent::__construct();
  226. $this->test_stack = array();
  227. $this->size = null;
  228. $this->progress = 0;
  229. }
  230. /**
  231. * Gets the formatter for small generic data items.
  232. * @return SimpleDumper Formatter.
  233. * @access public
  234. */
  235. function getDumper() {
  236. return new SimpleDumper();
  237. }
  238. /**
  239. * Paints the start of a group test. Will also paint
  240. * the page header and footer if this is the
  241. * first test. Will stash the size if the first
  242. * start.
  243. * @param string $test_name Name of test that is starting.
  244. * @param integer $size Number of test cases starting.
  245. * @access public
  246. */
  247. function paintGroupStart($test_name, $size) {
  248. if (! isset($this->size)) {
  249. $this->size = $size;
  250. }
  251. if (count($this->test_stack) == 0) {
  252. $this->paintHeader($test_name);
  253. }
  254. $this->test_stack[] = $test_name;
  255. }
  256. /**
  257. * Paints the end of a group test. Will paint the page
  258. * footer if the stack of tests has unwound.
  259. * @param string $test_name Name of test that is ending.
  260. * @param integer $progress Number of test cases ending.
  261. * @access public
  262. */
  263. function paintGroupEnd($test_name) {
  264. array_pop($this->test_stack);
  265. if (count($this->test_stack) == 0) {
  266. $this->paintFooter($test_name);
  267. }
  268. }
  269. /**
  270. * Paints the start of a test case. Will also paint
  271. * the page header and footer if this is the
  272. * first test. Will stash the size if the first
  273. * start.
  274. * @param string $test_name Name of test that is starting.
  275. * @access public
  276. */
  277. function paintCaseStart($test_name) {
  278. if (! isset($this->size)) {
  279. $this->size = 1;
  280. }
  281. if (count($this->test_stack) == 0) {
  282. $this->paintHeader($test_name);
  283. }
  284. $this->test_stack[] = $test_name;
  285. }
  286. /**
  287. * Paints the end of a test case. Will paint the page
  288. * footer if the stack of tests has unwound.
  289. * @param string $test_name Name of test that is ending.
  290. * @access public
  291. */
  292. function paintCaseEnd($test_name) {
  293. $this->progress++;
  294. array_pop($this->test_stack);
  295. if (count($this->test_stack) == 0) {
  296. $this->paintFooter($test_name);
  297. }
  298. }
  299. /**
  300. * Paints the start of a test method.
  301. * @param string $test_name Name of test that is starting.
  302. * @access public
  303. */
  304. function paintMethodStart($test_name) {
  305. $this->test_stack[] = $test_name;
  306. }
  307. /**
  308. * Paints the end of a test method. Will paint the page
  309. * footer if the stack of tests has unwound.
  310. * @param string $test_name Name of test that is ending.
  311. * @access public
  312. */
  313. function paintMethodEnd($test_name) {
  314. array_pop($this->test_stack);
  315. }
  316. /**
  317. * Paints the test document header.
  318. * @param string $test_name First test top level
  319. * to start.
  320. * @access public
  321. * @abstract
  322. */
  323. function paintHeader($test_name) {
  324. }
  325. /**
  326. * Paints the test document footer.
  327. * @param string $test_name The top level test.
  328. * @access public
  329. * @abstract
  330. */
  331. function paintFooter($test_name) {
  332. }
  333. /**
  334. * Accessor for internal test stack. For
  335. * subclasses that need to see the whole test
  336. * history for display purposes.
  337. * @return array List of methods in nesting order.
  338. * @access public
  339. */
  340. function getTestList() {
  341. return $this->test_stack;
  342. }
  343. /**
  344. * Accessor for total test size in number
  345. * of test cases. Null until the first
  346. * test is started.
  347. * @return integer Total number of cases at start.
  348. * @access public
  349. */
  350. function getTestCaseCount() {
  351. return $this->size;
  352. }
  353. /**
  354. * Accessor for the number of test cases
  355. * completed so far.
  356. * @return integer Number of ended cases.
  357. * @access public
  358. */
  359. function getTestCaseProgress() {
  360. return $this->progress;
  361. }
  362. /**
  363. * Static check for running in the comand line.
  364. * @return boolean True if CLI.
  365. * @access public
  366. */
  367. static function inCli() {
  368. return php_sapi_name() == 'cli';
  369. }
  370. }
  371. /**
  372. * For modifying the behaviour of the visual reporters.
  373. * @package SimpleTest
  374. * @subpackage UnitTester
  375. */
  376. class SimpleReporterDecorator {
  377. protected $reporter;
  378. /**
  379. * Mediates between the reporter and the test case.
  380. * @param SimpleScorer $reporter Reporter to receive events.
  381. */
  382. function __construct($reporter) {
  383. $this->reporter = $reporter;
  384. }
  385. /**
  386. * Signals that the next evaluation will be a dry
  387. * run. That is, the structure events will be
  388. * recorded, but no tests will be run.
  389. * @param boolean $is_dry Dry run if true.
  390. * @access public
  391. */
  392. function makeDry($is_dry = true) {
  393. $this->reporter->makeDry($is_dry);
  394. }
  395. /**
  396. * Accessor for current status. Will be false
  397. * if there have been any failures or exceptions.
  398. * Used for command line tools.
  399. * @return boolean True if no failures.
  400. * @access public
  401. */
  402. function getStatus() {
  403. return $this->reporter->getStatus();
  404. }
  405. /**
  406. * The nesting of the test cases so far. Not
  407. * all reporters have this facility.
  408. * @return array Test list if accessible.
  409. * @access public
  410. */
  411. function getTestList() {
  412. if (method_exists($this->reporter, 'getTestList')) {
  413. return $this->reporter->getTestList();
  414. } else {
  415. return array();
  416. }
  417. }
  418. /**
  419. * The reporter has a veto on what should be run.
  420. * @param string $test_case_name Name of test case.
  421. * @param string $method Name of test method.
  422. * @return boolean True if test should be run.
  423. * @access public
  424. */
  425. function shouldInvoke($test_case_name, $method) {
  426. return $this->reporter->shouldInvoke($test_case_name, $method);
  427. }
  428. /**
  429. * Can wrap the invoker in preparation for running
  430. * a test.
  431. * @param SimpleInvoker $invoker Individual test runner.
  432. * @return SimpleInvoker Wrapped test runner.
  433. * @access public
  434. */
  435. function createInvoker($invoker) {
  436. return $this->reporter->createInvoker($invoker);
  437. }
  438. /**
  439. * Gets the formatter for privateiables and other small
  440. * generic data items.
  441. * @return SimpleDumper Formatter.
  442. * @access public
  443. */
  444. function getDumper() {
  445. return $this->reporter->getDumper();
  446. }
  447. /**
  448. * Paints the start of a group test.
  449. * @param string $test_name Name of test or other label.
  450. * @param integer $size Number of test cases starting.
  451. * @access public
  452. */
  453. function paintGroupStart($test_name, $size) {
  454. $this->reporter->paintGroupStart($test_name, $size);
  455. }
  456. /**
  457. * Paints the end of a group test.
  458. * @param string $test_name Name of test or other label.
  459. * @access public
  460. */
  461. function paintGroupEnd($test_name) {
  462. $this->reporter->paintGroupEnd($test_name);
  463. }
  464. /**
  465. * Paints the start of a test case.
  466. * @param string $test_name Name of test or other label.
  467. * @access public
  468. */
  469. function paintCaseStart($test_name) {
  470. $this->reporter->paintCaseStart($test_name);
  471. }
  472. /**
  473. * Paints the end of a test case.
  474. * @param string $test_name Name of test or other label.
  475. * @access public
  476. */
  477. function paintCaseEnd($test_name) {
  478. $this->reporter->paintCaseEnd($test_name);
  479. }
  480. /**
  481. * Paints the start of a test method.
  482. * @param string $test_name Name of test or other label.
  483. * @access public
  484. */
  485. function paintMethodStart($test_name) {
  486. $this->reporter->paintMethodStart($test_name);
  487. }
  488. /**
  489. * Paints the end of a test method.
  490. * @param string $test_name Name of test or other label.
  491. * @access public
  492. */
  493. function paintMethodEnd($test_name) {
  494. $this->reporter->paintMethodEnd($test_name);
  495. }
  496. /**
  497. * Chains to the wrapped reporter.
  498. * @param string $message Message is ignored.
  499. * @access public
  500. */
  501. function paintPass($message) {
  502. $this->reporter->paintPass($message);
  503. }
  504. /**
  505. * Chains to the wrapped reporter.
  506. * @param string $message Message is ignored.
  507. * @access public
  508. */
  509. function paintFail($message) {
  510. $this->reporter->paintFail($message);
  511. }
  512. /**
  513. * Chains to the wrapped reporter.
  514. * @param string $message Text of error formatted by
  515. * the test case.
  516. * @access public
  517. */
  518. function paintError($message) {
  519. $this->reporter->paintError($message);
  520. }
  521. /**
  522. * Chains to the wrapped reporter.
  523. * @param Exception $exception Exception to show.
  524. * @access public
  525. */
  526. function paintException($exception) {
  527. $this->reporter->paintException($exception);
  528. }
  529. /**
  530. * Prints the message for skipping tests.
  531. * @param string $message Text of skip condition.
  532. * @access public
  533. */
  534. function paintSkip($message) {
  535. $this->reporter->paintSkip($message);
  536. }
  537. /**
  538. * Chains to the wrapped reporter.
  539. * @param string $message Text to display.
  540. * @access public
  541. */
  542. function paintMessage($message) {
  543. $this->reporter->paintMessage($message);
  544. }
  545. /**
  546. * Chains to the wrapped reporter.
  547. * @param string $message Text to display.
  548. * @access public
  549. */
  550. function paintFormattedMessage($message) {
  551. $this->reporter->paintFormattedMessage($message);
  552. }
  553. /**
  554. * Chains to the wrapped reporter.
  555. * @param string $type Event type as text.
  556. * @param mixed $payload Message or object.
  557. * @return boolean Should return false if this
  558. * type of signal should fail the
  559. * test suite.
  560. * @access public
  561. */
  562. function paintSignal($type, $payload) {
  563. $this->reporter->paintSignal($type, $payload);
  564. }
  565. }
  566. /**
  567. * For sending messages to multiple reporters at
  568. * the same time.
  569. * @package SimpleTest
  570. * @subpackage UnitTester
  571. */
  572. class MultipleReporter {
  573. private $reporters = array();
  574. /**
  575. * Adds a reporter to the subscriber list.
  576. * @param SimpleScorer $reporter Reporter to receive events.
  577. * @access public
  578. */
  579. function attachReporter($reporter) {
  580. $this->reporters[] = $reporter;
  581. }
  582. /**
  583. * Signals that the next evaluation will be a dry
  584. * run. That is, the structure events will be
  585. * recorded, but no tests will be run.
  586. * @param boolean $is_dry Dry run if true.
  587. * @access public
  588. */
  589. function makeDry($is_dry = true) {
  590. for ($i = 0; $i < count($this->reporters); $i++) {
  591. $this->reporters[$i]->makeDry($is_dry);
  592. }
  593. }
  594. /**
  595. * Accessor for current status. Will be false
  596. * if there have been any failures or exceptions.
  597. * If any reporter reports a failure, the whole
  598. * suite fails.
  599. * @return boolean True if no failures.
  600. * @access public
  601. */
  602. function getStatus() {
  603. for ($i = 0; $i < count($this->reporters); $i++) {
  604. if (! $this->reporters[$i]->getStatus()) {
  605. return false;
  606. }
  607. }
  608. return true;
  609. }
  610. /**
  611. * The reporter has a veto on what should be run.
  612. * It requires all reporters to want to run the method.
  613. * @param string $test_case_name name of test case.
  614. * @param string $method Name of test method.
  615. * @access public
  616. */
  617. function shouldInvoke($test_case_name, $method) {
  618. for ($i = 0; $i < count($this->reporters); $i++) {
  619. if (! $this->reporters[$i]->shouldInvoke($test_case_name, $method)) {
  620. return false;
  621. }
  622. }
  623. return true;
  624. }
  625. /**
  626. * Every reporter gets a chance to wrap the invoker.
  627. * @param SimpleInvoker $invoker Individual test runner.
  628. * @return SimpleInvoker Wrapped test runner.
  629. * @access public
  630. */
  631. function createInvoker($invoker) {
  632. for ($i = 0; $i < count($this->reporters); $i++) {
  633. $invoker = $this->reporters[$i]->createInvoker($invoker);
  634. }
  635. return $invoker;
  636. }
  637. /**
  638. * Gets the formatter for privateiables and other small
  639. * generic data items.
  640. * @return SimpleDumper Formatter.
  641. * @access public
  642. */
  643. function getDumper() {
  644. return new SimpleDumper();
  645. }
  646. /**
  647. * Paints the start of a group test.
  648. * @param string $test_name Name of test or other label.
  649. * @param integer $size Number of test cases starting.
  650. * @access public
  651. */
  652. function paintGroupStart($test_name, $size) {
  653. for ($i = 0; $i < count($this->reporters); $i++) {
  654. $this->reporters[$i]->paintGroupStart($test_name, $size);
  655. }
  656. }
  657. /**
  658. * Paints the end of a group test.
  659. * @param string $test_name Name of test or other label.
  660. * @access public
  661. */
  662. function paintGroupEnd($test_name) {
  663. for ($i = 0; $i < count($this->reporters); $i++) {
  664. $this->reporters[$i]->paintGroupEnd($test_name);
  665. }
  666. }
  667. /**
  668. * Paints the start of a test case.
  669. * @param string $test_name Name of test or other label.
  670. * @access public
  671. */
  672. function paintCaseStart($test_name) {
  673. for ($i = 0; $i < count($this->reporters); $i++) {
  674. $this->reporters[$i]->paintCaseStart($test_name);
  675. }
  676. }
  677. /**
  678. * Paints the end of a test case.
  679. * @param string $test_name Name of test or other label.
  680. * @access public
  681. */
  682. function paintCaseEnd($test_name) {
  683. for ($i = 0; $i < count($this->reporters); $i++) {
  684. $this->reporters[$i]->paintCaseEnd($test_name);
  685. }
  686. }
  687. /**
  688. * Paints the start of a test method.
  689. * @param string $test_name Name of test or other label.
  690. * @access public
  691. */
  692. function paintMethodStart($test_name) {
  693. for ($i = 0; $i < count($this->reporters); $i++) {
  694. $this->reporters[$i]->paintMethodStart($test_name);
  695. }
  696. }
  697. /**
  698. * Paints the end of a test method.
  699. * @param string $test_name Name of test or other label.
  700. * @access public
  701. */
  702. function paintMethodEnd($test_name) {
  703. for ($i = 0; $i < count($this->reporters); $i++) {
  704. $this->reporters[$i]->paintMethodEnd($test_name);
  705. }
  706. }
  707. /**
  708. * Chains to the wrapped reporter.
  709. * @param string $message Message is ignored.
  710. * @access public
  711. */
  712. function paintPass($message) {
  713. for ($i = 0; $i < count($this->reporters); $i++) {
  714. $this->reporters[$i]->paintPass($message);
  715. }
  716. }
  717. /**
  718. * Chains to the wrapped reporter.
  719. * @param string $message Message is ignored.
  720. * @access public
  721. */
  722. function paintFail($message) {
  723. for ($i = 0; $i < count($this->reporters); $i++) {
  724. $this->reporters[$i]->paintFail($message);
  725. }
  726. }
  727. /**
  728. * Chains to the wrapped reporter.
  729. * @param string $message Text of error formatted by
  730. * the test case.
  731. * @access public
  732. */
  733. function paintError($message) {
  734. for ($i = 0; $i < count($this->reporters); $i++) {
  735. $this->reporters[$i]->paintError($message);
  736. }
  737. }
  738. /**
  739. * Chains to the wrapped reporter.
  740. * @param Exception $exception Exception to display.
  741. * @access public
  742. */
  743. function paintException($exception) {
  744. for ($i = 0; $i < count($this->reporters); $i++) {
  745. $this->reporters[$i]->paintException($exception);
  746. }
  747. }
  748. /**
  749. * Prints the message for skipping tests.
  750. * @param string $message Text of skip condition.
  751. * @access public
  752. */
  753. function paintSkip($message) {
  754. for ($i = 0; $i < count($this->reporters); $i++) {
  755. $this->reporters[$i]->paintSkip($message);
  756. }
  757. }
  758. /**
  759. * Chains to the wrapped reporter.
  760. * @param string $message Text to display.
  761. * @access public
  762. */
  763. function paintMessage($message) {
  764. for ($i = 0; $i < count($this->reporters); $i++) {
  765. $this->reporters[$i]->paintMessage($message);
  766. }
  767. }
  768. /**
  769. * Chains to the wrapped reporter.
  770. * @param string $message Text to display.
  771. * @access public
  772. */
  773. function paintFormattedMessage($message) {
  774. for ($i = 0; $i < count($this->reporters); $i++) {
  775. $this->reporters[$i]->paintFormattedMessage($message);
  776. }
  777. }
  778. /**
  779. * Chains to the wrapped reporter.
  780. * @param string $type Event type as text.
  781. * @param mixed $payload Message or object.
  782. * @return boolean Should return false if this
  783. * type of signal should fail the
  784. * test suite.
  785. * @access public
  786. */
  787. function paintSignal($type, $payload) {
  788. for ($i = 0; $i < count($this->reporters); $i++) {
  789. $this->reporters[$i]->paintSignal($type, $payload);
  790. }
  791. }
  792. }
  793. ?>