Source for file junit_xml_reporter.php

Documentation is available at junit_xml_reporter.php

  1. <?php
  2. /**
  3.  *    @package    SimpleTest
  4.  *    @subpackage    Extensions
  5.  *    @version    $Id: junit_xml_reporter.php 1982 2010-03-28 11:57:54Z lastcraft $
  6.  *  @author Patrice Neff - mailinglists@patrice.ch (original code)
  7.  */
  8.  
  9. /**
  10.  * include SimpleTest reporter
  11.  */
  12. require_once dirname(__FILE__).'/../reporter.php';
  13.  
  14. /**
  15.  * Reporter which outputs test results in a format compatible
  16.  * with JUnit / Maven XML output. Can be used for integrating
  17.  * test suite with continuous integration servers such as
  18.  * Atlassian Bamboo.
  19.  * @package    SimpleTest
  20.  * @subpackage    Extensions
  21.  */
  22. class JUnitXMLReporter extends SimpleReporter {
  23.     function __construct({
  24.         parent::__construct();
  25.         $this->doc new DOMDocument();
  26.         $this->doc->loadXML('<testsuite/>');
  27.         $this->root $this->doc->documentElement;
  28.     }
  29.  
  30.     function paintHeader($test_name{
  31.         $this->testsStart microtime(true);
  32.         
  33.         $this->root->setAttribute('name'$test_name);
  34.         $this->root->setAttribute('timestamp'date('c'));
  35.         $this->root->setAttribute('hostname''localhost');
  36.         
  37.         echo "<?xml version=\"1.0\"?>\n";
  38.         echo "<!-- starting test suite $test_name\n";
  39.     }
  40.  
  41.     /**
  42.      *    Paints the end of the test with a summary of
  43.      *    the passes and failures.
  44.      *    @param string $test_name        Name class of test.
  45.      *    @access public
  46.      */
  47.     function paintFooter($test_name{
  48.         echo "-->\n";
  49.         
  50.         $duration microtime(true$this->testsStart;
  51.  
  52.         $this->root->setAttribute('tests'$this->getPassCount($this->getFailCount($this->getExceptionCount());
  53.         $this->root->setAttribute('failures'$this->getFailCount());
  54.         $this->root->setAttribute('errors'$this->getExceptionCount());
  55.         $this->root->setAttribute('time'$duration);
  56.         
  57.         $this->doc->formatOutput true;
  58.         $xml $this->doc->saveXML();
  59.         // Cut out XML declaration
  60.         echo preg_replace('/<\?[^>]*\?>/'""$xml);
  61.         echo "\n";
  62.     }
  63.     
  64.     function paintCaseStart($case{
  65.         echo "- case start $case\n";
  66.         $this->currentCaseName $case;
  67.     }
  68.  
  69.     function paintCaseEnd($case{
  70.         // No output here
  71.     }
  72.     
  73.     function paintMethodStart($test{
  74.         echo "  - test start: $test\n";
  75.         
  76.         $this->methodStart microtime(true);
  77.         $this->currCase $this->doc->createElement('testcase');
  78.     }
  79.     
  80.     function paintMethodEnd($test{
  81.         $duration microtime(true$this->methodStart;
  82.         
  83.         $this->currCase->setAttribute('name'$test);
  84.         $this->currCase->setAttribute('classname'$this->currentCaseName);
  85.         $this->currCase->setAttribute('time'$duration);
  86.         $this->root->appendChild($this->currCase);
  87.     }
  88.     
  89.     function paintFail($message{
  90.         parent::paintFail($message);
  91.  
  92.         error_log("Failure: " $message);
  93.         $this->terminateAbnormally($message);
  94.     }
  95.     
  96.     function paintException($exception{
  97.         parent::paintException($exception);
  98.         
  99.         error_log("Exception: " $exception);
  100.         $this->terminateAbnormally($exception);
  101.     }
  102.     
  103.     function terminateAbnormally($message{
  104.         if (!$this->currCase{
  105.             error_log("!! currCase was not set.");
  106.             return;
  107.         }
  108.  
  109.         $ch $this->doc->createElement('failure');
  110.         $breadcrumb $this->getTestList();
  111.         $ch->setAttribute('message'$breadcrumb[count($breadcrumb)-1]);
  112.         $ch->setAttribute('type'$breadcrumb[count($breadcrumb)-1]);
  113.         
  114.         $message implode(' -> '$breadcrumb"\n\n\n" $message;
  115.         $content $this->doc->createTextNode($message);
  116.         $ch->appendChild($content);
  117.         
  118.         $this->currCase->appendChild($ch);
  119.     }
  120. }
  121. ?>

Documentation generated on Sun, 31 Oct 2010 16:31:40 -0500 by phpDocumentor 1.4.3