Commit 554c26a9 authored by meus's avatar meus

fixing code coverage report

parent 28abbc2f
<?php
// include doctrine, and register it's autoloader
require_once dirname(__FILE__) . '/../lib/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload'));
$result = unserialize(file_get_contents("coverage.txt"));
$path = $result["path"];
$coverage = $result["coverage"];
$key ="percentage";
if(isset($_GET["order"])){
$key = $_GET["order"];
}
$totallines = 0;
$totalcovered = 0;
$totalmaybe = 0;
$totalnotcovered = 0;
$coveredArray = array();
foreach ($coverage as $file => $lines) {
$pos = strpos($file, $path);
if($pos === false && $pos !== 0){
continue;
}
$class = str_replace(DIRECTORY_SEPARATOR, '_', substr($file, strlen($path), -4));
$class = str_replace($path, Doctrine::getPath(), $class);
if (strpos($class, '_Interface')) {
continue;
}
if(!class_exists($class)){
continue;
}
$total = count($lines) -1; //we have to remove one since it always reports the last line as a hit
$covered = 0;
$maybe = 0;
$notcovered = 0;
foreach($lines as $result){
switch($result){
case "1":
$covered++;
break;
case "-1":
$notcovered++;
break;
case "-2":
$maybe++;
break;
}
}
$covered--; //again we have to remove that last line.
$totallines += $total;
$totalcovered += $covered;
$totalnotcovered += $notcovered;
$totalmaybe += $maybe;
if ($total === 0) {
$total = 1;
}
$percentage = round((($covered + $maybe) / $total) * 100, 2);
$coveredArray[$class] = array("covered" => $covered, "maybe" => $maybe, "notcovered"=>$notcovered, "total" => $total, "percentage" => $percentage);
}
//lets sort it
uasort($coveredArray, "sortArray");
if(isset($_GET["desc"]) && $_GET["desc"] == "true"){
$coveredArray = array_reverse($coveredArray, true);
}
?>
<h1>Coverage report for Doctrine</h1>
<p>Default mode shows results sorted by perentage. This can be changed with order = covered|total|maybe|notcovered|percentage and desc=true GET variables</p>
<?php
echo "<table>";
echo "<tr><th></th><th>Percentage</th><th>Total</th><th>Covered</th><th>Maybe</th><th>Not Covered</th><th></th></tr>";
print "<tr><td>" . TOTAL . "</td><td>" . round((($totalcovered + $totalmaybe) / $totallines) * 100, 2) . " % </td><td>$totallines</td><td>$totalcovered</td><td>$totalmaybe</td><td>$totaldead</td><td></td></tr>";
foreach($coveredArray as $class => $info){
print "<tr><td>" . $class . "</td><td>" . $info["percentage"] . " % </td><td>" . $info["total"] . "</td><td>" . $info["covered"] . "</td><td>" . $info["maybe"] . "</td><td>" . $info["notcovered"]. "</td><td>" . printLink($class) . "</td></tr>";
}
print "</table>";
function sortArray($a, $b){
global $key;
if ($a[$key] == $b[$key]) {
return 0;
}
return ($a[$key] < $b[$key]) ? -1 : 1;
}
function printLink($className){
global $path;
$className = str_replace("_", "/", $className) . ".php";
return '<a href="coverage.php?file=' . $path . $className . '">coverage</a>';
}
function countCovered($value, $key){
global $covered;
if($value >= 1){
$covered++;
}
}
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
// include doctrine, and register it's autoloader // include doctrine, and register it's autoloader
require_once dirname(__FILE__) . '/../lib/Doctrine.php'; require_once dirname(__FILE__) . '/../lib/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'autoload'));
$path = "/home/bjartka/workspace/doctrine/lib/";
?> ?>
<html> <html>
...@@ -10,7 +9,8 @@ $path = "/home/bjartka/workspace/doctrine/lib/"; ...@@ -10,7 +9,8 @@ $path = "/home/bjartka/workspace/doctrine/lib/";
<style type="text/css"> <style type="text/css">
.covered{ background: green;} .covered{ background: green;}
.normal{ background: white;} .normal{ background: white;}
.error{ background: red;} .red{ background: red;}
.orange{ background: #f90;}
dl.table-display dl.table-display
{ {
...@@ -38,9 +38,11 @@ dt { clear: both; } ...@@ -38,9 +38,11 @@ dt { clear: both; }
</head> </head>
<body> <body>
<? <?
$result = unserialize(file_get_contents("coverage.txt"));
$coverage = $result["coverage"];
function getCoverageReport($file){ function getCoverageReport($file){
$coverage = unserialize(file_get_contents("coverage.txt")); global $coverage;
$html = '<div id="coverage">'; $html = '<div id="coverage">';
if(!isset($coverage[$file])){ if(!isset($coverage[$file])){
$html .= 'No coverage for this file</div>'; $html .= 'No coverage for this file</div>';
...@@ -56,7 +58,9 @@ function getCoverageReport($file){ ...@@ -56,7 +58,9 @@ function getCoverageReport($file){
if(isset($coveredLines[$linenum]) && $coveredLines[$linenum] == 1){ if(isset($coveredLines[$linenum]) && $coveredLines[$linenum] == 1){
$class = "covered"; $class = "covered";
}else if(isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -1){ }else if(isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -1){
$class ="error"; $class ="red";
}else if(isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -2){
$class ="orange";
} }
$html .= '<dd class="' . $class . '">' . htmlspecialchars($line) . '</dd>' . "\n"; $html .= '<dd class="' . $class . '">' . htmlspecialchars($line) . '</dd>' . "\n";
} }
...@@ -67,12 +71,13 @@ function getCoverageReport($file){ ...@@ -67,12 +71,13 @@ function getCoverageReport($file){
if(isset($_GET["file"])){ if(isset($_GET["file"])){
$file = $_GET["file"]; $file = $_GET["file"];
echo '<a href="coverage.php">Back to filelist</a>'; echo '<a href="coverage.php">Back to filelist</a>';
echo '<a href="cc.php">Back to coverage report</a>';
echo '<h1>Coverage for ' . $file . '</h1>'; echo '<h1>Coverage for ' . $file . '</h1>';
echo getCoverageReport($file); echo getCoverageReport($file);
}else{ }else{
echo "<ul>"; echo "<ul>";
$it = new RecursiveDirectoryIterator($path); $it = new RecursiveDirectoryIterator(Doctrine::getPath());
foreach(new RecursiveIteratorIterator($it) as $file){ foreach(new RecursiveIteratorIterator($it) as $file){
if(strpos($file->getPathname(), ".svn")){ if(strpos($file->getPathname(), ".svn")){
continue; continue;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -418,68 +418,19 @@ if(PHP_SAPI === "cli"){ ...@@ -418,68 +418,19 @@ if(PHP_SAPI === "cli"){
}else{ }else{
$reporter = new MyReporter(); $reporter = new MyReporter();
} }
//uncomment this to run codecoverage
//xdebug_start_code_coverage();
$test->run($reporter);
/* remote to enable coverage
$path = Doctrine::getPath() . DIRECTORY_SEPARATOR;
?>
<table>
<tr>
<td>class</td>
<td>coverage</td>
</tr>
<?php
$coverage = xdebug_get_code_coverage();
$totallines = 0;
$totalcovered = 0;
foreach ($coverage as $file => $lines) {
$pos = strpos($file, $path);
if($pos === false && $pos !== 0){
continue;
}
$covered = 0;
$coveredLines = array_values($lines);
array_walk($coveredLines, "countCovered");
$class = str_replace(DIRECTORY_SEPARATOR, '_', substr($file, strlen($path), -4));
if (strpos($class, '_Interface')) {
continue;
}
if(!class_exists($class)){
continue;
}
$refl = new ReflectionClass($class);
$total = 0;
foreach ($refl->getMethods() as $method) {
$total += ($method->getEndLine() - $method->getStartLine());
}
$totallines += $total;
$totalcovered += $covered;
if ($total === 0) { //use this for normal testing
$total = 1; $test->run($reporter);
}
print "<tr><td>" . $class . "</td><td>" . round(($covered / $total) * 100, 2) . " % </td></tr>";
}
if ($totallines === 0) {
$totallines = 1;
}
print "<tr><td>TOTAL</td><td>" . round(($totalcovered / $totallines) * 100, 2) . " % </td></tr>";
function countCovered($value, $key){ /*
global $covered; *
if($value >= 1){ * Use this code to get code coverage. Then goto cc.php file to see resul
$covered++; *
} xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
} $test->run($reporter);
?> $result["path"] = Doctrine::getPath() . DIRECTORY_SEPARATOR;
</table> $result["coverage"] = xdebug_get_code_coverage();
</body> xdebug_stop_code_coverage();
</html> file_put_contents("coverage.txt", serialize($result));
*/ //look at the cc.php file in a browser to see the report
?> //*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment