Merge branch 'MDL-39444_23' of git://github.com/timhunt/moodle into MOODLE_23_STABLE
[moodle.git] / lib / simpletestlib / default_reporter.php
blob6feb67a30883f4bc1288b41e1109a6126ee21c9d
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 */
9 /**#@+
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__) . '/simpletest.php');
13 require_once(dirname(__FILE__) . '/scorer.php');
14 require_once(dirname(__FILE__) . '/reporter.php');
15 require_once(dirname(__FILE__) . '/xml.php');
16 /**#@-*/
18 /**
19 * Parser for command line arguments. Extracts
20 * the a specific test to run and engages XML
21 * reporting when necessary.
22 * @package SimpleTest
23 * @subpackage UnitTester
25 class SimpleCommandLineParser {
26 private $to_property = array(
27 'case' => 'case', 'c' => 'case',
28 'test' => 'test', 't' => 'test',
30 private $case = '';
31 private $test = '';
32 private $xml = false;
33 private $help = false;
34 private $no_skips = false;
36 /**
37 * Parses raw command line arguments into object properties.
38 * @param string $arguments Raw commend line arguments.
40 function __construct($arguments) {
41 if (! is_array($arguments)) {
42 return;
44 foreach ($arguments as $i => $argument) {
45 if (preg_match('/^--?(test|case|t|c)=(.+)$/', $argument, $matches)) {
46 $property = $this->to_property[$matches[1]];
47 $this->$property = $matches[2];
48 } elseif (preg_match('/^--?(test|case|t|c)$/', $argument, $matches)) {
49 $property = $this->to_property[$matches[1]];
50 if (isset($arguments[$i + 1])) {
51 $this->$property = $arguments[$i + 1];
53 } elseif (preg_match('/^--?(xml|x)$/', $argument)) {
54 $this->xml = true;
55 } elseif (preg_match('/^--?(no-skip|no-skips|s)$/', $argument)) {
56 $this->no_skips = true;
57 } elseif (preg_match('/^--?(help|h)$/', $argument)) {
58 $this->help = true;
63 /**
64 * Run only this test.
65 * @return string Test name to run.
67 function getTest() {
68 return $this->test;
71 /**
72 * Run only this test suite.
73 * @return string Test class name to run.
75 function getTestCase() {
76 return $this->case;
79 /**
80 * Output should be XML or not.
81 * @return boolean True if XML desired.
83 function isXml() {
84 return $this->xml;
87 /**
88 * Output should suppress skip messages.
89 * @return boolean True for no skips.
91 function noSkips() {
92 return $this->no_skips;
95 /**
96 * Output should be a help message. Disabled during XML mode.
97 * @return boolean True if help message desired.
99 function help() {
100 return $this->help && ! $this->xml;
104 * Returns plain-text help message for command line runner.
105 * @return string String help message
107 function getHelpText() {
108 return <<<HELP
109 SimpleTest command line default reporter (autorun)
110 Usage: php <test_file> [args...]
112 -c <class> Run only the test-case <class>
113 -t <method> Run only the test method <method>
114 -s Suppress skip messages
115 -x Return test results in XML
116 -h Display this help message
118 HELP;
124 * The default reporter used by SimpleTest's autorun
125 * feature. The actual reporters used are dependency
126 * injected and can be overridden.
127 * @package SimpleTest
128 * @subpackage UnitTester
130 class DefaultReporter extends SimpleReporterDecorator {
133 * Assembles the appropriate reporter for the environment.
135 function __construct() {
136 if (SimpleReporter::inCli()) {
137 $parser = new SimpleCommandLineParser($_SERVER['argv']);
138 $interfaces = $parser->isXml() ? array('XmlReporter') : array('TextReporter');
139 if ($parser->help()) {
140 // I'm not sure if we should do the echo'ing here -- ezyang
141 echo $parser->getHelpText();
142 exit(1);
144 $reporter = new SelectiveReporter(
145 SimpleTest::preferred($interfaces),
146 $parser->getTestCase(),
147 $parser->getTest());
148 if ($parser->noSkips()) {
149 $reporter = new NoSkipsReporter($reporter);
151 } else {
152 $reporter = new SelectiveReporter(
153 SimpleTest::preferred('HtmlReporter'),
154 @$_GET['c'],
155 @$_GET['t']);
156 if (@$_GET['skips'] == 'no' || @$_GET['show-skips'] == 'no') {
157 $reporter = new NoSkipsReporter($reporter);
160 parent::__construct($reporter);