default_reporter.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Optional include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: default_reporter.php 1704 2008-03-25 00:47:04Z lastcraft $
  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. var $_to_property = array(
  25. 'case' => '_case', 'c' => '_case',
  26. 'test' => '_test', 't' => '_test',
  27. 'xml' => '_xml', 'x' => '_xml');
  28. var $_case = '';
  29. var $_test = '';
  30. var $_xml = false;
  31. var $_no_skips = false;
  32. /**
  33. * Parses raw command line arguments into object properties.
  34. * @param string $arguments Raw commend line arguments.
  35. */
  36. function SimpleCommandLineParser($arguments) {
  37. if (! is_array($arguments)) {
  38. return;
  39. }
  40. foreach ($arguments as $i => $argument) {
  41. if (preg_match('/^--?(test|case|t|c)=(.+)$/', $argument, $matches)) {
  42. $property = $this->_to_property[$matches[1]];
  43. $this->$property = $matches[2];
  44. } elseif (preg_match('/^--?(test|case|t|c)$/', $argument, $matches)) {
  45. $property = $this->_to_property[$matches[1]];
  46. if (isset($arguments[$i + 1])) {
  47. $this->$property = $arguments[$i + 1];
  48. }
  49. } elseif (preg_match('/^--?(xml|x)$/', $argument)) {
  50. $this->_xml = true;
  51. } elseif (preg_match('/^--?(no-skip|no-skips|s)$/', $argument)) {
  52. $this->_no_skips = true;
  53. }
  54. }
  55. }
  56. /**
  57. * Run only this test.
  58. * @return string Test name to run.
  59. * @access public
  60. */
  61. function getTest() {
  62. return $this->_test;
  63. }
  64. /**
  65. * Run only this test suite.
  66. * @return string Test class name to run.
  67. * @access public
  68. */
  69. function getTestCase() {
  70. return $this->_case;
  71. }
  72. /**
  73. * Output should be XML or not.
  74. * @return boolean True if XML desired.
  75. * @access public
  76. */
  77. function isXml() {
  78. return $this->_xml;
  79. }
  80. /**
  81. * Output should suppress skip messages.
  82. * @return boolean True for no skips.
  83. * @access public
  84. */
  85. function noSkips() {
  86. return $this->_no_skips;
  87. }
  88. }
  89. /**
  90. * The default reporter used by SimpleTest's autorun
  91. * feature. The actual reporters used are dependency
  92. * injected and can be overridden.
  93. * @package SimpleTest
  94. * @subpackage UnitTester
  95. */
  96. class DefaultReporter extends SimpleReporterDecorator {
  97. /**
  98. * Assembles the appopriate reporter for the environment.
  99. */
  100. function DefaultReporter() {
  101. if (SimpleReporter::inCli()) {
  102. global $argv;
  103. $parser = new SimpleCommandLineParser($argv);
  104. $interfaces = $parser->isXml() ? array('XmlReporter') : array('TextReporter');
  105. $reporter = &new SelectiveReporter(
  106. SimpleTest::preferred($interfaces),
  107. $parser->getTestCase(),
  108. $parser->getTest());
  109. if ($parser->noSkips()) {
  110. $reporter = &new NoSkipsReporter($reporter);
  111. }
  112. } else {
  113. $reporter = &new SelectiveReporter(
  114. SimpleTest::preferred('HtmlReporter'),
  115. @$_GET['c'],
  116. @$_GET['t']);
  117. if (@$_GET['skips'] == 'no' || @$_GET['show-skips'] == 'no') {
  118. $reporter = &new NoSkipsReporter($reporter);
  119. }
  120. }
  121. $this->SimpleReporterDecorator($reporter);
  122. }
  123. }
  124. ?>