default_reporter.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Optional include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: default_reporter.php 2011 2011-04-29 08:22:48Z pp11 $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/simpletest.php');
  12. require_once(dirname(__FILE__) . '/scorer.php');
  13. require_once(dirname(__FILE__) . '/reporter.php');
  14. require_once(dirname(__FILE__) . '/xml.php');
  15. /**#@-*/
  16. /**
  17. * Parser for command line arguments. Extracts
  18. * the a specific test to run and engages XML
  19. * reporting when necessary.
  20. * @package SimpleTest
  21. * @subpackage UnitTester
  22. */
  23. class SimpleCommandLineParser {
  24. private $to_property = array(
  25. 'case' => 'case', 'c' => 'case',
  26. 'test' => 'test', 't' => 'test',
  27. );
  28. private $case = '';
  29. private $test = '';
  30. private $xml = false;
  31. private $help = false;
  32. private $no_skips = false;
  33. /**
  34. * Parses raw command line arguments into object properties.
  35. * @param string $arguments Raw commend line arguments.
  36. */
  37. function __construct($arguments) {
  38. if (! is_array($arguments)) {
  39. return;
  40. }
  41. foreach ($arguments as $i => $argument) {
  42. if (preg_match('/^--?(test|case|t|c)=(.+)$/', $argument, $matches)) {
  43. $property = $this->to_property[$matches[1]];
  44. $this->$property = $matches[2];
  45. } elseif (preg_match('/^--?(test|case|t|c)$/', $argument, $matches)) {
  46. $property = $this->to_property[$matches[1]];
  47. if (isset($arguments[$i + 1])) {
  48. $this->$property = $arguments[$i + 1];
  49. }
  50. } elseif (preg_match('/^--?(xml|x)$/', $argument)) {
  51. $this->xml = true;
  52. } elseif (preg_match('/^--?(no-skip|no-skips|s)$/', $argument)) {
  53. $this->no_skips = true;
  54. } elseif (preg_match('/^--?(help|h)$/', $argument)) {
  55. $this->help = true;
  56. }
  57. }
  58. }
  59. /**
  60. * Run only this test.
  61. * @return string Test name to run.
  62. */
  63. function getTest() {
  64. return $this->test;
  65. }
  66. /**
  67. * Run only this test suite.
  68. * @return string Test class name to run.
  69. */
  70. function getTestCase() {
  71. return $this->case;
  72. }
  73. /**
  74. * Output should be XML or not.
  75. * @return boolean True if XML desired.
  76. */
  77. function isXml() {
  78. return $this->xml;
  79. }
  80. /**
  81. * Output should suppress skip messages.
  82. * @return boolean True for no skips.
  83. */
  84. function noSkips() {
  85. return $this->no_skips;
  86. }
  87. /**
  88. * Output should be a help message. Disabled during XML mode.
  89. * @return boolean True if help message desired.
  90. */
  91. function help() {
  92. return $this->help && ! $this->xml;
  93. }
  94. /**
  95. * Returns plain-text help message for command line runner.
  96. * @return string String help message
  97. */
  98. function getHelpText() {
  99. return <<<HELP
  100. SimpleTest command line default reporter (autorun)
  101. Usage: php <test_file> [args...]
  102. -c <class> Run only the test-case <class>
  103. -t <method> Run only the test method <method>
  104. -s Suppress skip messages
  105. -x Return test results in XML
  106. -h Display this help message
  107. HELP;
  108. }
  109. }
  110. /**
  111. * The default reporter used by SimpleTest's autorun
  112. * feature. The actual reporters used are dependency
  113. * injected and can be overridden.
  114. * @package SimpleTest
  115. * @subpackage UnitTester
  116. */
  117. class DefaultReporter extends SimpleReporterDecorator {
  118. /**
  119. * Assembles the appropriate reporter for the environment.
  120. */
  121. function __construct() {
  122. if (SimpleReporter::inCli()) {
  123. $parser = new SimpleCommandLineParser($_SERVER['argv']);
  124. $interfaces = $parser->isXml() ? array('XmlReporter') : array('TextReporter');
  125. if ($parser->help()) {
  126. // I'm not sure if we should do the echo'ing here -- ezyang
  127. echo $parser->getHelpText();
  128. exit(1);
  129. }
  130. $reporter = new SelectiveReporter(
  131. SimpleTest::preferred($interfaces),
  132. $parser->getTestCase(),
  133. $parser->getTest());
  134. if ($parser->noSkips()) {
  135. $reporter = new NoSkipsReporter($reporter);
  136. }
  137. } else {
  138. $reporter = new SelectiveReporter(
  139. SimpleTest::preferred('HtmlReporter'),
  140. @$_GET['c'],
  141. @$_GET['t']);
  142. if (@$_GET['skips'] == 'no' || @$_GET['show-skips'] == 'no') {
  143. $reporter = new NoSkipsReporter($reporter);
  144. }
  145. }
  146. parent::__construct($reporter);
  147. }
  148. }
  149. ?>