PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Xinc/Plugin/Repos/Gui/PhpUnitTestResults/Widget.php

http://xinc.googlecode.com/
PHP | 238 lines | 172 code | 31 blank | 35 comment | 24 complexity | 06f7241a4ff645362484a770e4ebfc04 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. declare(encoding = 'utf-8');
  3. /**
  4. * Xinc - Continuous Integration.
  5. *
  6. * PHP version 5
  7. *
  8. * @category Development
  9. * @package Xinc.Plugin.Repos.Gui.PhpUnitTestResults
  10. * @author Arno Schneider <username@example.org>
  11. * @copyright 2007 Arno Schneider, Barcelona
  12. * @license http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php
  13. * This file is part of Xinc.
  14. * Xinc is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU Lesser General Public License as
  16. * published by the Free Software Foundation; either version 2.1 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * Xinc is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with Xinc, write to the Free Software Foundation,
  26. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. * @link http://xincplus.sourceforge.net
  28. */
  29. require_once 'Xinc/Gui/Widget/Interface.php';
  30. require_once 'Xinc/Build/Iterator.php';
  31. require_once 'Xinc/Project.php';
  32. require_once 'Xinc/Build.php';
  33. require_once 'Xinc/Plugin/Repos/Gui/Dashboard/Detail/Extension.php';
  34. require_once 'Xinc/Plugin/Repos/Gui/PhpUnitTestResults/Extension/Dashboard.php';
  35. require_once 'Xinc/Data/Repository.php';
  36. class Xinc_Plugin_Repos_Gui_PhpUnitTestResults_Widget implements Xinc_Gui_Widget_Interface
  37. {
  38. protected $_plugin;
  39. private $_extensions = array();
  40. public $projects = array();
  41. public $builds;
  42. public function __construct(Xinc_Plugin_Interface $plugin)
  43. {
  44. $this->_plugin = $plugin;
  45. }
  46. public function mime_content_type2($fileName)
  47. {
  48. $contentType = null;
  49. /**if (function_exists('finfo_open')) {
  50. $finfo = finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
  51. if(!$finfo) return;
  52. $contentType = finfo_file($finfo, $fileName);
  53. finfo_close($finfo);
  54. } else*/
  55. if (function_exists('mime_content_type')) {
  56. $contentType = mime_content_type($fileName);
  57. }
  58. return $contentType;
  59. }
  60. public function handleEvent($eventId)
  61. {
  62. $query = $_SERVER['REQUEST_URI'];
  63. $projectName = isset($_REQUEST['project'])?$_REQUEST['project']:null;
  64. $buildTime = isset($_REQUEST['buildtime'])?$_REQUEST['buildtime']:null;
  65. if (empty($projectName) || empty($buildTime)) {
  66. die('Could not find phpunit test results');
  67. }
  68. $project = new Xinc_Project();
  69. $project->setName($projectName);
  70. try {
  71. $build = Xinc_Build::unserialize(
  72. $project,
  73. $buildTime,
  74. Xinc_Gui_Handler::getInstance()->getStatusDir()
  75. );
  76. $buildLabel = $build->getLabel();
  77. $timezone = $build->getConfigDirective('timezone');
  78. if ($timezone !== null) {
  79. Xinc_Timezone::set($timezone);
  80. } else {
  81. $xincTimezone = Xinc_Gui_Handler::getInstance()->getConfigDirective('timezone');
  82. if ($xincTimezone !== null) {
  83. Xinc_Timezone::set($xincTimezone);
  84. } else {
  85. Xinc_Timezone::set(Xinc_Gui_Handler::getInstance()->getSystemTimezone());
  86. }
  87. }
  88. $sourceFile = $build->getInternalProperties()->get('phpunit.file');
  89. if ($sourceFile != null && file_exists($sourceFile) && class_exists('XSLTProcessor')) {
  90. $xslFile = PEAR_Config::singleton()->get('data_dir') . DIRECTORY_SEPARATOR . 'Xinc' . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'phpunit' . DIRECTORY_SEPARATOR . 'details.xsl';
  91. try {
  92. $outputFileName = Xinc_Ini::getInstance()->get('tmp_dir', 'xinc') . DIRECTORY_SEPARATOR . 'phpunit_details_' . $projectName . '_' . $buildTime;
  93. } catch (Exception $e) {
  94. Xinc_Logger::getInstance()->error('Cannot get xinc.ini configuration');
  95. $outputFileName = 'phpunit_details_' . $projectName . '_' . $buildTime;
  96. }
  97. if (file_exists($outputFileName)) {
  98. $details = file_get_contents($outputFileName);
  99. } else {
  100. $details = $this->_transformResults($sourceFile, $xslFile, $outputFileName);
  101. }
  102. //$click = 'openMenuTab(\'phpunit-'.$projectName.'-'.$buildTimestamp.'\',\'PHPUnit - '.$projectName.'\',\''.$url.'\',null,false,false,\'auto\');';
  103. $title='PHPUnit Test Results';
  104. $buildTimeString = date('Y-m-d H:i:s', $build->getBuildTime()) . '-' . Xinc_Timezone::get();
  105. $content = str_replace(array('{title}','{details}','{projectName}','{buildLabel}','{buildTime}'),
  106. array($title,$details, $projectName, $buildLabel, $buildTimeString), $details);
  107. } else {
  108. $content = false;
  109. }
  110. Xinc_Timezone::reset();
  111. echo $content;
  112. } catch (Exception $e) {
  113. echo "Could not find phpunit test results";
  114. }
  115. }
  116. public function getPaths()
  117. {
  118. return array('/phpunit/results/', '/phpunit/results');
  119. }
  120. public function getTestResults(Xinc_Build_Interface $build)
  121. {
  122. require_once 'PEAR/Config.php';
  123. $statusDir = Xinc_Gui_Handler::getInstance()->getStatusDir();
  124. $projectName = $build->getProject()->getName();
  125. $buildTimestamp = $build->getBuildTime();
  126. $buildLabel = $build->getLabel();
  127. $templateFile = Xinc_Data_Repository::getInstance()->get(
  128. 'templates' . DIRECTORY_SEPARATOR . 'dashboard' . DIRECTORY_SEPARATOR
  129. . 'detail' . DIRECTORY_SEPARATOR . 'extension' . DIRECTORY_SEPARATOR
  130. .'phpunit-summary.phtml'
  131. );
  132. $template = file_get_contents($templateFile);
  133. $url = '/phpunit/results/?project='.$projectName . '&buildtime=' . $buildTimestamp .'&f=results.html';
  134. $sourceFile = $build->getInternalProperties()->get('phpunit.file');
  135. if ($sourceFile != null && file_exists($sourceFile) && class_exists('XSLTProcessor')) {
  136. $xslFile = PEAR_Config::singleton()->get('data_dir') . DIRECTORY_SEPARATOR . 'Xinc' . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'phpunit' . DIRECTORY_SEPARATOR . 'summary.xsl';
  137. try {
  138. $outputFileName = Xinc_Ini::getInstance()->get('tmp_dir', 'xinc') . DIRECTORY_SEPARATOR . 'phpunit_summary_' . $projectName . '_' . $buildTimestamp;
  139. } catch (Exception $e) {
  140. Xinc_Logger::getInstance()->error('Cannot get xinc.ini configuration');
  141. $outputFileName = 'phpunit_summary_' . $projectName . '_' . $buildTimestamp;
  142. }
  143. if (file_exists($outputFileName)) {
  144. $summary = file_get_contents($outputFileName);
  145. } else {
  146. $summary = $this->_transformResults($sourceFile, $xslFile, $outputFileName);
  147. }
  148. //$click = 'openMenuTab(\'phpunit-'.$projectName.'-'.$buildTimestamp.'\',\'PHPUnit - '.$projectName.'\',\''.$url.'\',null,false,false,\'auto\');';
  149. $detailsLink='<a href="'.$url.'">Details</a>';
  150. $content = str_replace(array('{detailsLink}','{summary}'),
  151. array($detailsLink,$summary), $template);
  152. } else {
  153. $content = false;
  154. }
  155. return $content;
  156. }
  157. private function _fixPackages(DOMDocument $document)
  158. {
  159. $testsuites = $document->getElementsByTagName('testsuite');
  160. foreach ($testsuites as $testsuite) {
  161. if (!$testsuite->hasAttribute('package')) {
  162. $testsuite->setAttribute('package', 'default');
  163. }
  164. }
  165. }
  166. private function _transformResults($xmlFile, $xslFile, $outputFileName)
  167. {
  168. $xml = new DOMDocument;
  169. $xml->load($xmlFile);
  170. $this->_fixPackages($xml);
  171. $xsl = new DOMDocument;
  172. $xsl->load($xslFile);
  173. // Configure the transformer
  174. $proc = new XSLTProcessor;
  175. $proc->importStyleSheet($xsl); // attach the xsl rules
  176. $return=$proc->transformToXml($xml);
  177. if ($return) {
  178. file_put_contents($outputFileName, $return);
  179. } else {
  180. $return = '<span style="color:red"><b>ERROR - could not transform</b></span>';
  181. }
  182. return $return;
  183. }
  184. public function init()
  185. {
  186. $detailWidget = Xinc_Gui_Widget_Repository::getInstance()
  187. ->getWidgetByClassName('Xinc_Plugin_Repos_Gui_Dashboard_Detail');
  188. $extension = new Xinc_Plugin_Repos_Gui_PhpUnitTestResults_Extension_Dashboard();
  189. $extension->setWidget($this);
  190. $detailWidget->registerExtension('BUILD_DETAILS', $extension);
  191. }
  192. public function registerExtension($extensionPoint, $extension)
  193. {
  194. $this->_extensions[$extensionPoint] = $extension;
  195. }
  196. public function getExtensionPoints()
  197. {
  198. return array();
  199. }
  200. public function hasExceptionHandler()
  201. {
  202. return false;
  203. }
  204. public function handleException(Exception $e)
  205. {
  206. }
  207. }