3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
4 * Copyright 2013-2014,2016, Franck Villaume - TrivialDev
6 * This file is a part of Fusionforge.
8 * Fusionforge is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Fusionforge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
22 require_once 'hudson.class.php';
23 require_once 'HudsonJobURLMalformedException.class.php';
24 require_once 'HudsonJobURLFileException.class.php';
25 require_once 'HudsonJobURLFileNotFoundException.class.php';
27 class HudsonTestResult {
29 protected $hudson_test_result_url;
34 * Construct an Hudson job from a job URL
36 function __construct($hudson_job_url) {
37 $parsed_url = parse_url($hudson_job_url);
39 if ( ! $parsed_url || ! array_key_exists('scheme', $parsed_url) ) {
40 throw new HudsonJobURLMalformedException(vsprintf(_("Wrong Job URL: %s"), array($hudson_job_url)));
43 $this->hudson_test_result_url = $hudson_job_url . "/lastBuild/testReport/api/xml/";
44 $this->_setStreamContext();
45 $this->buildJobObject();
48 function getHudsonControler() {
52 public function buildJobObject() {
53 $this->dom_job = $this->_getXMLObject($this->hudson_test_result_url);
56 protected function _getXMLObject($hudson_test_result_url) {
57 $xmlstr = @file_get_contents($hudson_test_result_url, false, $this->context);
58 if ($xmlstr !== false) {
59 $xmlobj = simplexml_load_string($xmlstr);
60 if ($xmlobj !== false) {
63 throw new HudsonJobURLFileException(vsprintf(_("Unable to read file at URL: %s"), array($hudson_test_result_url)));
66 throw new HudsonJobURLFileNotFoundException(vsprintf(_("File not found at URL: %s"), array($hudson_test_result_url)));
70 private function _setStreamContext() {
71 if (array_key_exists('sys_proxy', $GLOBALS) && $GLOBALS['sys_proxy']) {
75 'proxy' => $GLOBALS['sys_proxy'],
76 'request_fulluri' => True,
80 $this->context = stream_context_create($context_opt);
82 $this->context = null;
86 function getFailCount() {
87 return $this->dom_job->failCount;
90 function getPassCount() {
91 return $this->dom_job->passCount;
94 function getSkipCount() {
95 return $this->dom_job->skipCount;
98 function getTotalCount() {
99 return $this->getFailCount() + $this->getPassCount() + $this->getSkipCount();
102 function getTestResultPieChart() {
104 html_use_jqueryjqplotpluginPie();
105 html_use_jqueryjqplotpluginhighlighter();
106 html_use_jqueryjqplotpluginCanvas();
107 echo $HTML->getJavascripts();
108 echo $HTML->getStylesheets();
109 $chartid = md5($this->hudson_test_result_url);
110 $pie_labels = array();
112 $pie_labels[0] = vsprintf(_("Pass (%s)"), $this->getPassCount());
113 $pie_vals[0] = $this->getPassCount();
114 $pie_labels[1] = vsprintf(_("Fail (%s)"), $this->getFailCount());
115 $pie_vals[1] = $this->getFailCount();
116 $pie_labels[2] = vsprintf(_("Skip (%s)"), $this->getSkipCount());
117 $pie_vals[2] = $this->getSkipCount();
118 echo '<script type="text/javascript">//<![CDATA['."\n";
119 echo 'var data'.$chartid.' = new Array();';
120 for ($i = 0; $i < count($pie_vals); $i++) {
121 echo 'data'.$chartid.'.push([\''.htmlentities($pie_labels[$i]).'\','.$pie_vals[$i].']);';
123 echo 'var plot'.$chartid.';';
124 echo 'jQuery(document).ready(function(){
125 plot'.$chartid.' = jQuery.jqplot (\'chart'.$chartid.'\', [data'.$chartid.'],
127 title : \'Test result: '.$this->getPassCount().'/'.$this->getTotalCount().'\',
129 renderer: jQuery.jqplot.PieRenderer,
131 showDataLabels: true,
132 dataLabels: \'percent\',
136 show:true, location: \'e\',
141 echo 'jQuery(window).resize(function() {
142 plot'.$chartid.'.replot( { resetAxes: true } );
144 echo '//]]></script>';
145 echo '<div id="chart'.$chartid.'"></div>';