|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * $Id: CoverageReporter.php,v 1.2 2010/12/14 17:35:59 moodlerobot Exp $ 00004 * 00005 * Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved. 00006 * Licensed under the Open Software License version 2.1 00007 * (See http://www.spikesource.com/license.html) 00008 */ 00009 ?> 00010 <?php 00011 00012 if(!defined("__PHPCOVERAGE_HOME")) { 00013 define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__))); 00014 } 00015 require_once __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php"; 00016 require_once __PHPCOVERAGE_HOME . "/util/Utility.php"; 00017 00018 /*{{{ Defines */ 00019 00020 define("TOTAL_FILES_EXPLAIN", "count of included source code files"); 00021 define("TOTAL_LINES_EXPLAIN", "includes comments and whitespaces"); 00022 define("TOTAL_COVERED_LINES_EXPLAIN", "lines of code that were executed"); 00023 define("TOTAL_UNCOVERED_LINES_EXPLAIN", "lines of executable code that were not executed"); 00024 define ("TOTAL_LINES_OF_CODE_EXPLAIN", "lines of executable code"); 00025 00026 /*}}}*/ 00027 00037 abstract class CoverageReporter { 00038 // {{{ Members 00039 00040 protected $logger; 00041 00042 // Report heading - will be displayed as the title of the main page. 00043 protected $heading; 00044 // CSS file path to be used. 00045 protected $style; 00046 // Directory where the report file(s) are written. 00047 protected $outputDir; 00048 00049 // Total number of lines in all the source files. 00050 protected $grandTotalLines; 00051 // Total number of lines covered in code coverage measurement. 00052 protected $grandTotalCoveredLines; 00053 // Total number of executable code lines that were left untouched. 00054 protected $grandTotalUncoveredLines; 00055 // Total number of files included 00056 protected $grandTotalFiles; 00057 protected $fileCoverage = array(); 00058 protected $recorder = false; 00059 00060 // }}} 00061 /*{{{ public function __construct()*/ 00062 00071 public function __construct( 00072 $heading="Coverage Report", 00073 $style="", 00074 $dir="report" 00075 ) { 00076 00077 global $util; 00078 $this->heading = $heading; 00079 $this->style = $style; 00080 $this->outputDir = $util->replaceBackslashes($dir); 00081 // Create the directory if not there 00082 $this->createReportDir(); 00083 $this->grandTotalFiles = 0; 00084 $this->grandTotalLines = 0; 00085 $this->grandTotalCoveredLines = 0; 00086 $this->grandTotalUncoveredLines = 0; 00087 00088 // Configure 00089 $this->logger = $util->getLogger(); 00090 } 00091 00092 /*}}}*/ 00093 /*{{{ protected function createReportDir() */ 00094 00100 protected function createReportDir() { 00101 global $util; 00102 if(!file_exists($this->outputDir)) { 00103 $util->makeDirRecursive($this->outputDir, 0755); 00104 } 00105 if(file_exists($this->outputDir)) { 00106 $this->outputDir = $util->replaceBackslashes(realpath($this->outputDir)); 00107 } 00108 } 00109 00110 /*}}}*/ 00111 /*{{{ protected function updateGrandTotals() */ 00112 00119 protected function updateGrandTotals(&$coverageCounts) { 00120 $this->grandTotalLines += $coverageCounts['total']; 00121 $this->grandTotalCoveredLines += $coverageCounts['covered']; 00122 $this->grandTotalUncoveredLines += $coverageCounts['uncovered']; 00123 00124 $this->recordFileCoverageInfo($coverageCounts); 00125 } 00126 00127 /*}}}*/ 00128 /*{{{ public function getGrandCodeCoveragePercentage()*/ 00129 00136 public function getGrandCodeCoveragePercentage() { 00137 if($this->grandTotalCoveredLines+$this->grandTotalUncoveredLines == 0) { 00138 return round(0, 2); 00139 } 00140 return round(((double)$this->grandTotalCoveredLines/((double)$this->grandTotalCoveredLines + (double)$this->grandTotalUncoveredLines)) * 100.0, 2); 00141 } 00142 00143 /*}}}*/ 00144 /*{{{ public function getFileCoverageInfo() */ 00145 00158 public function getFileCoverageInfo() { 00159 return $this->fileCoverage; 00160 } 00161 00162 /*}}}*/ 00163 /*{{{ public function recordFileCoverageInfo() */ 00164 00171 protected function recordFileCoverageInfo(&$fileCoverage) { 00172 $this->fileCoverage[] = $fileCoverage; 00173 } 00174 00175 /*}}}*/ 00176 /*{{{ public function printTextSummary() */ 00177 00184 public function printTextSummary($filename=false) { 00185 global $util; 00186 $str = "\n"; 00187 $str .= "##############################################\n"; 00188 $str .= " Code Coverage Summary: " . $this->heading . "\n"; 00189 $str .= " Total Files: " . $this->grandTotalFiles . "\n"; 00190 $str .= " Total Lines: " . $this->grandTotalLines . "\n"; 00191 $str .= " Total Covered Lines of Code: " . $this->grandTotalCoveredLines . "\n"; 00192 $str .= " Total Missed Lines of Code: " . $this->grandTotalUncoveredLines . "\n"; 00193 $str .= " Total Lines of Code: " . ($this->grandTotalCoveredLines + $this->grandTotalUncoveredLines) . "\n"; 00194 $str .= " Code Coverage: " . $this->getGrandCodeCoveragePercentage() . "%\n"; 00195 $str .= "##############################################\n"; 00196 00197 if(empty($filename)) { 00198 echo $str; 00199 //file_put_contents("php://stdout", $str); 00200 } 00201 else { 00202 $filename = $util->replaceBackslashes($filename); 00203 if(!file_exists(dirname($filename))) { 00204 $ret = $util->makeDirRecursive(dirname($filename), 0755); 00205 if(!$ret) { 00206 die ("Cannot create directory " . dirname($filename) . "\n"); 00207 } 00208 } 00209 file_put_contents($filename, $str); 00210 } 00211 } 00212 00213 /*}}}*/ 00214 /*{{{ protected function makeRelative() */ 00215 00224 protected function makeRelative($filepath) { 00225 $dirPath = realpath($this->outputDir); 00226 $absFilePath = realpath($filepath); 00227 00228 if(strpos($absFilePath, $dirPath) === 0) { 00229 $relPath = substr($absFilePath, strlen($dirPath)+1); 00230 return $relPath; 00231 } 00232 return $absFilePath; 00233 } 00234 00235 /*}}}*/ 00236 /*{{{ protected function getRelativeOutputDirPath() */ 00237 00238 00247 protected function getRelativeOutputDirPath($filepath) { 00248 $relPath = ""; 00249 $filepath = dirname($filepath); 00250 while($filepath !== false && $filepath != ".") { 00251 $relPath = "../" . $relPath; 00252 $filepath = dirname($filepath); 00253 } 00254 return $relPath; 00255 } 00256 00257 /*}}}*/ 00258 /*{{{ public abstract function generateReport() */ 00259 00267 public abstract function generateReport(&$data); 00268 00269 /*}}}*/ 00270 /*{{{ Getters and Setters */ 00271 00272 public function setHeading($heading) { 00273 $this->heading = $heading; 00274 } 00275 00276 public function getHeading() { 00277 return $this->heading; 00278 } 00279 00280 public function setStyle($style) { 00281 $this->style = $style; 00282 } 00283 00284 public function getStyle() { 00285 return $this->style; 00286 } 00287 00288 public function setOutputDir($dir) { 00289 $this->outputDir = $dir; 00290 } 00291 00292 public function getOutputDir() { 00293 return $this->outputDir; 00294 } 00295 00296 public function setCoverageRecorder(&$recorder) { 00297 $this->recorder = $recorder; 00298 } 00299 00300 /*}}}*/ 00301 } 00302 ?>