FOSSology  4.4.0
Open Source License Compliance by Open Source Software
junit_xml_reporter.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © Fossology contributors
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
17 require_once dirname(__FILE__).'/../reporter.php';
18 
27 class JUnitXMLReporter extends SimpleReporter {
28  function __construct() {
29  parent::__construct();
30  $this->doc = new DOMDocument();
31  $this->doc->loadXML('<testsuite/>');
32  $this->root = $this->doc->documentElement;
33  }
34 
35  function paintHeader($test_name) {
36  $this->testsStart = microtime(true);
37 
38  $this->root->setAttribute('name', $test_name);
39  $this->root->setAttribute('timestamp', date('c'));
40  $this->root->setAttribute('hostname', 'localhost');
41 
42  echo "<?xml version=\"1.0\"?>\n";
43  echo "<!-- starting test suite $test_name\n";
44  }
45 
52  function paintFooter($test_name) {
53  echo "-->\n";
54  echo "\n<testsuites>\n";
55 
56  $duration = microtime(true) - $this->testsStart;
57 
58  $this->root->setAttribute('tests', $this->getPassCount() + $this->getFailCount() + $this->getExceptionCount());
59  $this->root->setAttribute('failures', $this->getFailCount());
60  $this->root->setAttribute('errors', $this->getExceptionCount());
61  $this->root->setAttribute('time', $duration);
62 
63  $this->doc->formatOutput = true;
64  $xml = $this->doc->saveXML();
65  // Cut out XML declaration
66  echo preg_replace('/<\?[^>]*\?>/', "", $xml);
67  echo "</testsuites>\n";
68  }
69 
70  function paintCaseStart($case) {
71  echo "- case start $case\n";
72  $this->currentCaseName = $case;
73  }
74 
75  function paintCaseEnd($case) {
76  // No output here
77  }
78 
79  function paintMethodStart($test) {
80  echo " - test start: $test\n";
81 
82  $this->methodStart = microtime(true);
83  $this->currCase = $this->doc->createElement('testcase');
84  }
85 
86  function paintMethodEnd($test) {
87  $duration = microtime(true) - $this->methodStart;
88 
89  $this->currCase->setAttribute('name', $test);
90  $this->currCase->setAttribute('classname', $this->currentCaseName);
91  $this->currCase->setAttribute('time', $duration);
92  $this->root->appendChild($this->currCase);
93  }
94 
95  function paintFail($message) {
96  parent::paintFail($message);
97 
98  error_log("Failure: " . $message);
99  $this->terminateAbnormally($message);
100  }
101 
102  function paintException($exception) {
103  parent::paintException($exception);
104 
105  error_log("Exception: " . $exception);
106  $this->terminateAbnormally($exception);
107  }
108 
109  function terminateAbnormally($message) {
110  if (!$this->currCase) {
111  error_log("!! currCase was not set.");
112  return;
113  }
114 
115  $ch = $this->doc->createElement('failure');
116  $breadcrumb = $this->getTestList();
117  $ch->setAttribute('message', $breadcrumb[count($breadcrumb)-1]);
118  $ch->setAttribute('type', $breadcrumb[count($breadcrumb)-1]);
119 
120  $message = implode(' -> ', $breadcrumb) . "\n\n\n" . $message;
121  $content = $this->doc->createTextNode($message);
122  $ch->appendChild($content);
123 
124  $this->currCase->appendChild($ch);
125  }
126 }