3 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
5 * This file is a part of Codendi.
7 * Codendi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Codendi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
20 require_once 'hudson.class.php';
21 require_once 'HudsonJobURLMalformedException.class.php';
22 require_once 'HudsonJobURLFileException.class.php';
23 require_once 'HudsonJobURLFileNotFoundException.class.php';
25 class HudsonTestResult {
27 protected $hudson_test_result_url;
33 * Construct an Hudson job from a job URL
35 function HudsonTestResult($hudson_job_url) {
36 $parsed_url = parse_url($hudson_job_url);
38 if ( ! $parsed_url || ! array_key_exists('scheme', $parsed_url) ) {
39 throw new HudsonJobURLMalformedException(vsprintf(_("Wrong Job URL: %s"), array($hudson_job_url)));
42 $this->hudson_test_result_url = $hudson_job_url . "/lastBuild/testReport/api/xml/";
44 $controler = $this->getHudsonControler();
46 $this->_setStreamContext();
48 $this->buildJobObject();
51 function getHudsonControler() {
55 public function buildJobObject() {
56 $this->dom_job = $this->_getXMLObject($this->hudson_test_result_url);
59 protected function _getXMLObject($hudson_test_result_url) {
60 $xmlstr = @file_get_contents($hudson_test_result_url, false, $this->context);
61 if ($xmlstr !== false) {
62 $xmlobj = simplexml_load_string($xmlstr);
63 if ($xmlobj !== false) {
66 throw new HudsonJobURLFileException(vsprintf(_("Unable to read file at URL: %s"), array($hudson_test_result_url)));
69 throw new HudsonJobURLFileNotFoundException(vsprintf(_("File not found at URL: %s"), array($hudson_test_result_url)));
73 private function _setStreamContext() {
74 if (array_key_exists('sys_proxy', $GLOBALS) && $GLOBALS['sys_proxy']) {
78 'proxy' => $GLOBALS['sys_proxy'],
79 'request_fulluri' => True,
83 $this->context = stream_context_create($context_opt);
85 $this->context = null;
89 function getFailCount() {
90 return $this->dom_job->failCount;
92 function getPassCount() {
93 return $this->dom_job->passCount;
95 function getSkipCount() {
96 return $this->dom_job->skipCount;
98 function getTotalCount() {
99 return $this->getFailCount() + $this->getPassCount() + $this->getSkipCount();
102 function getTestResultPieChart() {
103 return '<img class="test_result_pie_chart" src="/plugins/hudson/test_result_pie_chart.php?p='.$this->getPassCount().'&f='.$this->getFailCount().'&s='.$this->getSkipCount().'" alt="Test result: '.$this->getPassCount().'/'.$this->getTotalCount().'" title="Test result: '.$this->getPassCount().'/'.$this->getTotalCount().'" />';