Source for file junit_xml_reporter.php

Documentation is available at junit_xml_reporter.php

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

Documentation generated on Sun, 04 May 2008 09:21:43 -0500 by phpDocumentor 1.3.0