3 * base include file for SimpleTest PUnit reporter
5 * @subpackage UnitTester
10 * @ignore originally defined in simple_test.php
12 if (!defined("SIMPLE_TEST")) {
13 define("SIMPLE_TEST", "simpletest/");
15 require_once(SIMPLE_TEST
. 'runner.php');
16 require_once(SIMPLE_TEST
. 'reporter.php');
18 * Main sprintf template for the start of the page.
19 * Sequence of parameters is:
21 * - script path - string
22 * - script path - string
24 * - additional css - string
26 * - image path - string
28 define('SIMPLETEST_WEBUNIT_HEAD', <<<EOS
32 <script type="text/javascript" src="%sx.js"></script>
33 <script type="text/javascript" src="%swebunit.js"></script>
34 <link rel="stylesheet" type="text/css" href="%swebunit.css" title="Default"></link>
35 <style type="text/css">
41 <h1> Running %s </h1>
43 <img src="%swait.gif" border="0"><br />
45 <script type="text/javascript">
49 <div id="run"></div><br />
51 <div id="visible_tab">visible tab content</div>
52 <span id="failtab" class="activetab"> <a href="javascript:activate_tab('fail');">Fail</a> </span>
53 <span id="treetab" class="inactivetab"> <a href="javascript:activate_tab('tree');">Tree</a> </span>
55 <div id="msg">Click on a failed test case method in the tree tab to view output here.</div>
59 <!-- open a new script to capture js vars as the tests run -->
60 <script type="text/javascript">
68 * May be needed for localized styles we need at runtime, not in the stylesheet.
70 define('SIMPLETEST_WEBUNIT_CSS', '/* this space reseved for future use */');
73 * Sample minimal test displayer. Generates only
74 * failure messages and a pass count.
76 * @subpackage UnitTester
78 class WebUnitReporter
extends SimpleReporter
{
80 * @var string Base directory for PUnit script, images and style sheets.
81 * Needs to be a relative path from where the test scripts are run
82 * (and obviously, visible in the document root).
87 * Does nothing yet. The first output will
88 * be sent on the first test start. For use
92 function WebUnitReporter($path='../ui/') {
93 $this->SimpleReporter();
98 * Paints the top of the web page setting the
99 * title to the name of the starting test.
100 * @param string $test_name Name class of test.
103 function paintHeader($test_name) {
104 $this->sendNoCacheHeaders();
106 SIMPLETEST_WEBUNIT_HEAD
119 * Send the headers necessary to ensure the page is
120 * reloaded on every request. Otherwise you could be
121 * scratching your head over out of date test data.
125 function sendNoCacheHeaders() {
126 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
127 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
128 header("Cache-Control: no-store, no-cache, must-revalidate");
129 header("Cache-Control: post-check=0, pre-check=0", false);
130 header("Pragma: no-cache");
134 * Paints the CSS. Add additional styles here.
135 * @return string CSS code as text.
139 return SIMPLETEST_WEBUNIT_CSS
;
143 * Paints the end of the test with a summary of
144 * the passes and failures.
145 * @param string $test_name Name class of test.
148 function paintFooter($test_name) {
149 echo 'make_tree();</script>'.$this->outputScript("xHide('wait');");
150 $colour = ($this->getFailCount() +
$this->getExceptionCount() > 0 ?
"red" : "green");
151 $content = "<h1>$test_name</h1>\n";
152 $content .= "<div style=\"";
153 $content .= "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
155 $content .= $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
156 $content .= " test cases complete:\n";
157 $content .= "<strong>" . $this->getPassCount() . "</strong> passes, ";
158 $content .= "<strong>" . $this->getFailCount() . "</strong> fails and ";
159 $content .= "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
160 $content .= "</div>\n";
162 echo $this->outputScript('foo = "'.$this->toJsString($content).'";'."\nset_div_content('run', foo);");
163 echo "\n</body>\n</html>\n";
168 * Paints formatted text such as dumped variables.
169 * @param string $message Text to show.
172 function paintFormattedMessage($message) {
173 echo "add_log(\"".$this->toJsString("<pre>$message</pre>", true)."\");\n";
177 * Paints the start of a group test. Will also paint
178 * the page header and footer if this is the
179 * first test. Will stash the size if the first
181 * @param string $test_name Name of test that is starting.
182 * @param integer $size Number of test cases starting.
185 function paintGroupStart($test_name, $size) {
186 Parent
::paintGroupStart($test_name, $size);
187 echo "add_group('$test_name');\n";
191 * Paints the start of a test case. Will also paint
192 * the page header and footer if this is the
193 * first test. Will stash the size if the first
195 * @param string $test_name Name of test that is starting.
198 function paintCaseStart($test_name) {
199 Parent
::paintCaseStart($test_name);
200 echo "add_case('$test_name');\n";
205 * Paints the start of a test method.
206 * @param string $test_name Name of test that is starting.
209 function paintMethodStart($test_name) {
210 Parent
::paintMethodStart($test_name);
211 echo "add_method('$test_name');\n";
215 * Paints the end of a test method.
216 * @param string $test_name Name of test that is ending.
219 function paintMethodEnd($test_name) {
220 Parent
::paintMethodEnd($test_name);
224 * Paints the test failure with a breadcrumbs
225 * trail of the nesting test suites below the
227 * @param string $message Failure message displayed in
228 * the context of the other tests.
231 function paintFail($message) {
232 parent
::paintFail($message);
233 $msg = "<span class=\"fail\">Fail</span>: ";
234 $breadcrumb = $this->getTestList();
235 array_shift($breadcrumb);
236 $msg .= implode("->", $breadcrumb);
237 $msg .= "->" . htmlentities($message) . "<br />";
238 echo "add_fail('$msg');\n";
242 * Paints a PHP error or exception.
243 * @param string $message Message is ignored.
247 function paintException($message) {
248 parent
::paintException($message);
249 $msg = "<span class=\"fail\">Exception</span>: ";
250 $breadcrumb = $this->getTestList();
251 array_shift($breadcrumb);
252 $msg .= implode("->", $breadcrumb);
253 $msg .= "-><strong>" . htmlentities($message) . "</strong><br />";
254 echo "add_fail('$msg');\n";
258 * Returns the script passed in wrapped in script tags.
260 * @param string $script the script to output
261 * @return string the script wrapped with script tags
263 function outputScript($script)
265 return "<script type=\"text/javascript\">\n".$script."\n</script>\n";
270 * Transform a string into a format acceptable to JavaScript
271 * @param string $str the string to transform
274 function toJsString($str, $preserveCr=false) {
275 $cr = ($preserveCr) ?
'\\n' : '';