| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * $Id: CoverageReporter.php 14665 2005-03-23 19:37:50Z npac $ 4 * 5 * Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved. 6 * Licensed under the Open Source License version 2.1 7 * (See http://www.spikesource.com/license.html) 8 */ 9 ?> 10 <?php 11 12 if(!defined("__PHPCOVERAGE_HOME")) { 13 define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__))); 14 } 15 require_once __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php"; 16 require_once __PHPCOVERAGE_HOME . "/util/Utility.php"; 17 18 /*{{{ Defines */ 19 20 define("TOTAL_FILES_EXPLAIN", "count of included source code files"); 21 define("TOTAL_LINES_EXPLAIN", "includes comments and whitespaces"); 22 define("TOTAL_COVERED_LINES_EXPLAIN", "lines of code that were executed"); 23 define("TOTAL_UNCOVERED_LINES_EXPLAIN", "lines of executable code that were not executed"); 24 define ("TOTAL_LINES_OF_CODE_EXPLAIN", "lines of executable code"); 25 26 /*}}}*/ 27 28 /** 29 * The base class for reporting coverage. This is an abstract as it does not 30 * implement the generateReport() function. Every concrete subclass must 31 * implement this method to generate a report. 32 * 33 * @author Nimish Pachapurkar <npac@spikesource.com> 34 * @version $Revision: 14665 $ 35 * @package tests_runner 36 */ 37 abstract class CoverageReporter { 38 // {{{ Members 39 40 protected $logger; 41 42 // Report heading - will be displayed as the title of the main page. 43 protected $heading; 44 // CSS file path to be used. 45 protected $style; 46 // Directory where the report file(s) are written. 47 protected $outputDir; 48 49 // Total number of lines in all the source files. 50 protected $grandTotalLines; 51 // Total number of lines covered in code coverage measurement. 52 protected $grandTotalCoveredLines; 53 // Total number of executable code lines that were left untouched. 54 protected $grandTotalUncoveredLines; 55 // Total number of files included 56 protected $grandTotalFiles; 57 protected $fileCoverage = array(); 58 protected $recorder = false; 59 60 // }}} 61 /*{{{ public function __construct()*/ 62 63 /** 64 * The constructor (PHP5 compatible) 65 * 66 * @param $heading 67 * @param $style 68 * @param $dir 69 * @access public 70 */ 71 public function __construct( 72 $heading="Coverage Report", 73 $style="", 74 $dir="report" 75 ) { 76 77 global $util; 78 $this->heading = $heading; 79 $this->style = $style; 80 $this->outputDir = $util->replaceBackslashes($dir); 81 // Create the directory if not there 82 $this->createReportDir(); 83 $this->grandTotalFiles = 0; 84 $this->grandTotalLines = 0; 85 $this->grandTotalCoveredLines = 0; 86 $this->grandTotalUncoveredLines = 0; 87 88 // Configure 89 $this->logger = $util->getLogger(); 90 } 91 92 /*}}}*/ 93 /*{{{ protected function createReportDir() */ 94 95 /** 96 * Create the report directory if it does not exists 97 * 98 * @access protected 99 */ 100 protected function createReportDir() { 101 global $util; 102 if(!file_exists($this->outputDir)) { 103 $util->makeDirRecursive($this->outputDir, 0755); 104 } 105 if(file_exists($this->outputDir)) { 106 $this->outputDir = $util->replaceBackslashes(realpath($this->outputDir)); 107 } 108 } 109 110 /*}}}*/ 111 /*{{{ protected function updateGrandTotals() */ 112 113 /** 114 * Update the grand totals 115 * 116 * @param &$coverageCounts Coverage counts for a file 117 * @access protected 118 */ 119 protected function updateGrandTotals(&$coverageCounts) { 120 $this->grandTotalLines += $coverageCounts['total']; 121 $this->grandTotalCoveredLines += $coverageCounts['covered']; 122 $this->grandTotalUncoveredLines += $coverageCounts['uncovered']; 123 124 $this->recordFileCoverageInfo($coverageCounts); 125 } 126 127 /*}}}*/ 128 /*{{{ public function getGrandCodeCoveragePercentage()*/ 129 130 /** 131 * Returns Overall Code Coverage percentage 132 * 133 * @return double Code Coverage percentage rounded to two decimals 134 * @access public 135 */ 136 public function getGrandCodeCoveragePercentage() { 137 if($this->grandTotalCoveredLines+$this->grandTotalUncoveredLines == 0) { 138 return round(0, 2); 139 } 140 return round(((double)$this->grandTotalCoveredLines/((double)$this->grandTotalCoveredLines + (double)$this->grandTotalUncoveredLines)) * 100.0, 2); 141 } 142 143 /*}}}*/ 144 /*{{{ public function getFileCoverageInfo() */ 145 146 /** 147 * Return the array containing file coverage information. 148 * 149 * The array returned contains following fields 150 * * filename: Name of the file 151 * * total: Total number of lines in that file 152 * * covered: Total number of executed lines in that file 153 * * uncovered: Total number of executable lines that were not executed. 154 * 155 * @return array Array of file coverage information 156 * @access public 157 */ 158 public function getFileCoverageInfo() { 159 return $this->fileCoverage; 160 } 161 162 /*}}}*/ 163 /*{{{ public function recordFileCoverageInfo() */ 164 165 /** 166 * Record the file coverage information for a file. 167 * 168 * @param &$fileCoverage Coverage information for a file 169 * @access protected 170 */ 171 protected function recordFileCoverageInfo(&$fileCoverage) { 172 $this->fileCoverage[] = $fileCoverage; 173 } 174 175 /*}}}*/ 176 /*{{{ public function printTextSummary() */ 177 178 /** 179 * Print the coverage summary to filename (if specified) or stderr 180 * 181 * @param $filename=false Filename to write the log to 182 * @access public 183 */ 184 public function printTextSummary($filename=false) { 185 global $util; 186 $str = "\n"; 187 $str .= "##############################################\n"; 188 $str .= " Code Coverage Summary: " . $this->heading . "\n"; 189 $str .= " Total Files: " . $this->grandTotalFiles . "\n"; 190 $str .= " Total Lines: " . $this->grandTotalLines . "\n"; 191 $str .= " Total Covered Lines of Code: " . $this->grandTotalCoveredLines . "\n"; 192 $str .= " Total Missed Lines of Code: " . $this->grandTotalUncoveredLines . "\n"; 193 $str .= " Total Lines of Code: " . ($this->grandTotalCoveredLines + $this->grandTotalUncoveredLines) . "\n"; 194 $str .= " Code Coverage: " . $this->getGrandCodeCoveragePercentage() . "%\n"; 195 $str .= "##############################################\n"; 196 197 if(empty($filename)) { 198 file_put_contents("php://stdout", $str); 199 } 200 else { 201 $filename = $util->replaceBackslashes($filename); 202 if(!file_exists(dirname($filename))) { 203 $ret = $util->makeDirRecursive(dirname($filename), 0755); 204 if(!$ret) { 205 die ("Cannot create directory " . dirname($filename) . "\n"); 206 } 207 } 208 file_put_contents($filename, $str); 209 } 210 } 211 212 /*}}}*/ 213 /*{{{ protected function makeRelative() */ 214 215 /** 216 * Convert the absolute path to PHP file markup to a path relative 217 * to the report dir. 218 * 219 * @param $filepath PHP markup file path 220 * @return Relative file path 221 * @access protected 222 */ 223 protected function makeRelative($filepath) { 224 $dirPath = realpath($this->outputDir); 225 $absFilePath = realpath($filepath); 226 227 if(strpos($absFilePath, $dirPath) === 0) { 228 $relPath = substr($absFilePath, strlen($dirPath)+1); 229 return $relPath; 230 } 231 return $absFilePath; 232 } 233 234 /*}}}*/ 235 /*{{{ protected function getRelativeOutputDirPath() */ 236 237 238 /** 239 * Get the relative path of report directory with respect to the given 240 * filepath 241 * 242 * @param $filepath Path of the file (relative to the report dir) 243 * @return String Relative path of report directory w.r.t. filepath 244 * @access protected 245 */ 246 protected function getRelativeOutputDirPath($filepath) { 247 $relPath = ""; 248 $filepath = dirname($filepath); 249 while($filepath !== false && $filepath != ".") { 250 $relPath = "../" . $relPath; 251 $filepath = dirname($filepath); 252 } 253 return $relPath; 254 } 255 256 /*}}}*/ 257 /*{{{ public abstract function generateReport() */ 258 259 /** 260 * 261 * This function generates report using one of the concrete subclasses. 262 * 263 * @param &$data Coverage Data recorded by coverage recorder. 264 * @access public 265 */ 266 public abstract function generateReport(&$data); 267 268 /*}}}*/ 269 /*{{{ Getters and Setters */ 270 271 public function setHeading($heading) { 272 $this->heading = $heading; 273 } 274 275 public function getHeading() { 276 return $this->heading; 277 } 278 279 public function setStyle($style) { 280 $this->style = $style; 281 } 282 283 public function getStyle() { 284 return $this->style; 285 } 286 287 public function setOutputDir($dir) { 288 $this->outputDir = $dir; 289 } 290 291 public function getOutputDir() { 292 return $this->outputDir; 293 } 294 295 public function setCoverageRecorder(&$recorder) { 296 $this->recorder = $recorder; 297 } 298 299 /*}}}*/ 300 } 301 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Dec 2 03:54:09 2008 | Cross-referenced by PHPXref 0.7 |