|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00012 require_once(dirname(__FILE__) . '/scorer.php'); 00021 class XmlReporter extends SimpleReporter { 00022 var $_indent; 00023 var $_namespace; 00024 00031 function XmlReporter($namespace = false, $indent = ' ') { 00032 $this->SimpleReporter(); 00033 $this->_namespace = ($namespace ? $namespace . ':' : ''); 00034 $this->_indent = $indent; 00035 } 00036 00044 function _getIndent($offset = 0) { 00045 return str_repeat( 00046 $this->_indent, 00047 count($this->getTestList()) + $offset); 00048 } 00049 00057 function toParsedXml($text) { 00058 return str_replace( 00059 array('&', '<', '>', '"', '\''), 00060 array('&', '<', '>', '"', '''), 00061 $text); 00062 } 00063 00070 function paintGroupStart($test_name, $size) { 00071 parent::paintGroupStart($test_name, $size); 00072 print $this->_getIndent(); 00073 print "<" . $this->_namespace . "group size=\"$size\">\n"; 00074 print $this->_getIndent(1); 00075 print "<" . $this->_namespace . "name>" . 00076 $this->toParsedXml($test_name) . 00077 "</" . $this->_namespace . "name>\n"; 00078 } 00079 00085 function paintGroupEnd($test_name) { 00086 print $this->_getIndent(); 00087 print "</" . $this->_namespace . "group>\n"; 00088 parent::paintGroupEnd($test_name); 00089 } 00090 00096 function paintCaseStart($test_name) { 00097 parent::paintCaseStart($test_name); 00098 print $this->_getIndent(); 00099 print "<" . $this->_namespace . "case>\n"; 00100 print $this->_getIndent(1); 00101 print "<" . $this->_namespace . "name>" . 00102 $this->toParsedXml($test_name) . 00103 "</" . $this->_namespace . "name>\n"; 00104 } 00105 00111 function paintCaseEnd($test_name) { 00112 print $this->_getIndent(); 00113 print "</" . $this->_namespace . "case>\n"; 00114 parent::paintCaseEnd($test_name); 00115 } 00116 00122 function paintMethodStart($test_name) { 00123 parent::paintMethodStart($test_name); 00124 print $this->_getIndent(); 00125 print "<" . $this->_namespace . "test>\n"; 00126 print $this->_getIndent(1); 00127 print "<" . $this->_namespace . "name>" . 00128 $this->toParsedXml($test_name) . 00129 "</" . $this->_namespace . "name>\n"; 00130 } 00131 00138 function paintMethodEnd($test_name) { 00139 print $this->_getIndent(); 00140 print "</" . $this->_namespace . "test>\n"; 00141 parent::paintMethodEnd($test_name); 00142 } 00143 00149 function paintPass($message) { 00150 parent::paintPass($message); 00151 print $this->_getIndent(1); 00152 print "<" . $this->_namespace . "pass>"; 00153 print $this->toParsedXml($message); 00154 print "</" . $this->_namespace . "pass>\n"; 00155 } 00156 00162 function paintFail($message) { 00163 parent::paintFail($message); 00164 print $this->_getIndent(1); 00165 print "<" . $this->_namespace . "fail>"; 00166 print $this->toParsedXml($message); 00167 print "</" . $this->_namespace . "fail>\n"; 00168 } 00169 00175 function paintError($message) { 00176 parent::paintError($message); 00177 print $this->_getIndent(1); 00178 print "<" . $this->_namespace . "exception>"; 00179 print $this->toParsedXml($message); 00180 print "</" . $this->_namespace . "exception>\n"; 00181 } 00182 00188 function paintException($exception) { 00189 parent::paintException($exception); 00190 print $this->_getIndent(1); 00191 print "<" . $this->_namespace . "exception>"; 00192 $message = 'Unexpected exception of type [' . get_class($exception) . 00193 '] with message ['. $exception->getMessage() . 00194 '] in ['. $exception->getFile() . 00195 ' line ' . $exception->getLine() . ']'; 00196 print $this->toParsedXml($message); 00197 print "</" . $this->_namespace . "exception>\n"; 00198 } 00199 00205 function paintSkip($message) { 00206 parent::paintSkip($message); 00207 print $this->_getIndent(1); 00208 print "<" . $this->_namespace . "skip>"; 00209 print $this->toParsedXml($message); 00210 print "</" . $this->_namespace . "skip>\n"; 00211 } 00212 00218 function paintMessage($message) { 00219 parent::paintMessage($message); 00220 print $this->_getIndent(1); 00221 print "<" . $this->_namespace . "message>"; 00222 print $this->toParsedXml($message); 00223 print "</" . $this->_namespace . "message>\n"; 00224 } 00225 00232 function paintFormattedMessage($message) { 00233 parent::paintFormattedMessage($message); 00234 print $this->_getIndent(1); 00235 print "<" . $this->_namespace . "formatted>"; 00236 print "<![CDATA[$message]]>"; 00237 print "</" . $this->_namespace . "formatted>\n"; 00238 } 00239 00246 function paintSignal($type, $payload) { 00247 parent::paintSignal($type, $payload); 00248 print $this->_getIndent(1); 00249 print "<" . $this->_namespace . "signal type=\"$type\">"; 00250 print "<![CDATA[" . serialize($payload) . "]]>"; 00251 print "</" . $this->_namespace . "signal>\n"; 00252 } 00253 00261 function paintHeader($test_name) { 00262 if (! SimpleReporter::inCli()) { 00263 header('Content-type: text/xml'); 00264 } 00265 print "<?xml version=\"1.0\""; 00266 if ($this->_namespace) { 00267 print " xmlns:" . $this->_namespace . 00268 "=\"www.lastcraft.com/SimpleTest/Beta3/Report\""; 00269 } 00270 print "?>\n"; 00271 print "<" . $this->_namespace . "run>\n"; 00272 } 00273 00280 function paintFooter($test_name) { 00281 print "</" . $this->_namespace . "run>\n"; 00282 } 00283 } 00284 00292 class NestingXmlTag { 00293 var $_name; 00294 var $_attributes; 00295 00302 function NestingXmlTag($attributes) { 00303 $this->_name = false; 00304 $this->_attributes = $attributes; 00305 } 00306 00312 function setName($name) { 00313 $this->_name = $name; 00314 } 00315 00321 function getName() { 00322 return $this->_name; 00323 } 00324 00330 function _getAttributes() { 00331 return $this->_attributes; 00332 } 00333 } 00334 00342 class NestingMethodTag extends NestingXmlTag { 00343 00350 function NestingMethodTag($attributes) { 00351 $this->NestingXmlTag($attributes); 00352 } 00353 00360 function paintStart(&$listener) { 00361 $listener->paintMethodStart($this->getName()); 00362 } 00363 00370 function paintEnd(&$listener) { 00371 $listener->paintMethodEnd($this->getName()); 00372 } 00373 } 00374 00382 class NestingCaseTag extends NestingXmlTag { 00383 00390 function NestingCaseTag($attributes) { 00391 $this->NestingXmlTag($attributes); 00392 } 00393 00400 function paintStart(&$listener) { 00401 $listener->paintCaseStart($this->getName()); 00402 } 00403 00410 function paintEnd(&$listener) { 00411 $listener->paintCaseEnd($this->getName()); 00412 } 00413 } 00414 00422 class NestingGroupTag extends NestingXmlTag { 00423 00430 function NestingGroupTag($attributes) { 00431 $this->NestingXmlTag($attributes); 00432 } 00433 00440 function paintStart(&$listener) { 00441 $listener->paintGroupStart($this->getName(), $this->getSize()); 00442 } 00443 00450 function paintEnd(&$listener) { 00451 $listener->paintGroupEnd($this->getName()); 00452 } 00453 00459 function getSize() { 00460 $attributes = $this->_getAttributes(); 00461 if (isset($attributes['SIZE'])) { 00462 return (integer)$attributes['SIZE']; 00463 } 00464 return 0; 00465 } 00466 } 00467 00474 class SimpleTestXmlParser { 00475 var $_listener; 00476 var $_expat; 00477 var $_tag_stack; 00478 var $_in_content_tag; 00479 var $_content; 00480 var $_attributes; 00481 00488 function SimpleTestXmlParser(&$listener) { 00489 $this->_listener = &$listener; 00490 $this->_expat = &$this->_createParser(); 00491 $this->_tag_stack = array(); 00492 $this->_in_content_tag = false; 00493 $this->_content = ''; 00494 $this->_attributes = array(); 00495 } 00496 00504 function parse($chunk) { 00505 if (! xml_parse($this->_expat, $chunk)) { 00506 trigger_error('XML parse error with ' . 00507 xml_error_string(xml_get_error_code($this->_expat))); 00508 return false; 00509 } 00510 return true; 00511 } 00512 00518 function &_createParser() { 00519 $expat = xml_parser_create(); 00520 xml_set_object($expat, $this); 00521 xml_set_element_handler($expat, '_startElement', '_endElement'); 00522 xml_set_character_data_handler($expat, '_addContent'); 00523 xml_set_default_handler($expat, '_default'); 00524 return $expat; 00525 } 00526 00533 function _pushNestingTag($nested) { 00534 array_unshift($this->_tag_stack, $nested); 00535 } 00536 00543 function &_getCurrentNestingTag() { 00544 return $this->_tag_stack[0]; 00545 } 00546 00553 function _popNestingTag() { 00554 return array_shift($this->_tag_stack); 00555 } 00556 00563 function _isLeaf($tag) { 00564 return in_array($tag, array( 00565 'NAME', 'PASS', 'FAIL', 'EXCEPTION', 'SKIP', 'MESSAGE', 'FORMATTED', 'SIGNAL')); 00566 } 00567 00577 function _startElement($expat, $tag, $attributes) { 00578 $this->_attributes = $attributes; 00579 if ($tag == 'GROUP') { 00580 $this->_pushNestingTag(new NestingGroupTag($attributes)); 00581 } elseif ($tag == 'CASE') { 00582 $this->_pushNestingTag(new NestingCaseTag($attributes)); 00583 } elseif ($tag == 'TEST') { 00584 $this->_pushNestingTag(new NestingMethodTag($attributes)); 00585 } elseif ($this->_isLeaf($tag)) { 00586 $this->_in_content_tag = true; 00587 $this->_content = ''; 00588 } 00589 } 00590 00597 function _endElement($expat, $tag) { 00598 $this->_in_content_tag = false; 00599 if (in_array($tag, array('GROUP', 'CASE', 'TEST'))) { 00600 $nesting_tag = $this->_popNestingTag(); 00601 $nesting_tag->paintEnd($this->_listener); 00602 } elseif ($tag == 'NAME') { 00603 $nesting_tag = &$this->_getCurrentNestingTag(); 00604 $nesting_tag->setName($this->_content); 00605 $nesting_tag->paintStart($this->_listener); 00606 } elseif ($tag == 'PASS') { 00607 $this->_listener->paintPass($this->_content); 00608 } elseif ($tag == 'FAIL') { 00609 $this->_listener->paintFail($this->_content); 00610 } elseif ($tag == 'EXCEPTION') { 00611 $this->_listener->paintError($this->_content); 00612 } elseif ($tag == 'SKIP') { 00613 $this->_listener->paintSkip($this->_content); 00614 } elseif ($tag == 'SIGNAL') { 00615 $this->_listener->paintSignal( 00616 $this->_attributes['TYPE'], 00617 unserialize($this->_content)); 00618 } elseif ($tag == 'MESSAGE') { 00619 $this->_listener->paintMessage($this->_content); 00620 } elseif ($tag == 'FORMATTED') { 00621 $this->_listener->paintFormattedMessage($this->_content); 00622 } 00623 } 00624 00631 function _addContent($expat, $text) { 00632 if ($this->_in_content_tag) { 00633 $this->_content .= $text; 00634 } 00635 return true; 00636 } 00637 00644 function _default($expat, $default) { 00645 } 00646 } 00647 ?>