adding coverage CLI flag to testrunner and updating coverage report
This commit is contained in:
parent
c67cfaf218
commit
d1a9e7a70b
113
tests/cc.php
113
tests/cc.php
@ -67,10 +67,18 @@ if(isset($_GET["file"])){
|
|||||||
class Doctrine_Coverage_Report
|
class Doctrine_Coverage_Report
|
||||||
{
|
{
|
||||||
|
|
||||||
|
const COVERED = 1;
|
||||||
|
const MAYBE = -2;
|
||||||
|
const NOTCOVERED = -1;
|
||||||
|
|
||||||
private $path;
|
private $path;
|
||||||
private $coverage;
|
private $coverage;
|
||||||
private $key;
|
private $key;
|
||||||
private $covered;
|
private $covered;
|
||||||
|
private $totallines = 0;
|
||||||
|
private $totalcovered = 0;
|
||||||
|
private $totalmaybe = 0;
|
||||||
|
private $totalnotcovered = 0;
|
||||||
|
|
||||||
public function __construct($file)
|
public function __construct($file)
|
||||||
{
|
{
|
||||||
@ -87,7 +95,7 @@ class Doctrine_Coverage_Report
|
|||||||
$html = '<div id="coverage">';
|
$html = '<div id="coverage">';
|
||||||
if( !isset( $this->coverage[$key]))
|
if( !isset( $this->coverage[$key]))
|
||||||
{
|
{
|
||||||
echo 'No coverage for this file</div>';
|
echo '<h2>This file has not been tested!</h2>';
|
||||||
}
|
}
|
||||||
$coveredLines = $this->coverage[$key];
|
$coveredLines = $this->coverage[$key];
|
||||||
$fileArray = file(Doctrine::getPath() . "/".$fileName);
|
$fileArray = file(Doctrine::getPath() . "/".$fileName);
|
||||||
@ -109,16 +117,84 @@ class Doctrine_Coverage_Report
|
|||||||
echo $html;
|
echo $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function generateNotCoveredFiles()
|
||||||
|
{
|
||||||
|
$it = new RecursiveDirectoryIterator(Doctrine::getPath());
|
||||||
|
|
||||||
|
$notCoveredArray = array();
|
||||||
|
foreach( new RecursiveIteratorIterator($it) as $file){
|
||||||
|
if( strpos($file->getPathname(), ".svn")){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$path = Doctrine::getPath() . DIRECTORY_SEPERATOR;
|
||||||
|
$coveredPath = str_replace($path, $this->path, $file->getPathname());
|
||||||
|
if( isset($this->coverage[$coveredPath])){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$class = str_replace(DIRECTORY_SEPARATOR, '_', substr($file, strlen($this->path), -4));
|
||||||
|
if (strpos($class, '_Interface')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!class_exists($class)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
$refClass = new ReflectionClass($class);
|
||||||
|
}catch(Exception $e){
|
||||||
|
echo $e->getMessage();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$lines = 0;
|
||||||
|
$methodLines = 0;
|
||||||
|
foreach($refClass->getMethods() as $refMethod){
|
||||||
|
|
||||||
|
if($refMethod->getDeclaringClass() != $refClass){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$methodLines = $refMethod->getEndLine() - $refMethod->getStartLine();
|
||||||
|
$lines += $methodLines;
|
||||||
|
}
|
||||||
|
if($methodLines == 0){
|
||||||
|
$notCoveredArray[$class] = array("covered" => 0, "maybe" => 0, "notcovered"=>$lines, "total" => $lines, "percentage" => 100);
|
||||||
|
}else{
|
||||||
|
$notCoveredArray[$class] = array("covered" => 0, "maybe" => 0, "notcovered"=>$lines, "total" => $lines, "percentage" => 0);
|
||||||
|
}
|
||||||
|
$this->totallines += $lines;
|
||||||
|
$this->totalnotcovered += $lines;
|
||||||
|
}
|
||||||
|
return $notCoveredArray;
|
||||||
|
}
|
||||||
|
|
||||||
public function showSummary()
|
public function showSummary()
|
||||||
{
|
{
|
||||||
if(isset($_GET["order"])){
|
if(isset($_GET["order"])){
|
||||||
$this->sortBy = $_GET["order"];
|
$this->sortBy = $_GET["order"];
|
||||||
}
|
}
|
||||||
$totallines = 0;
|
$coveredArray = $this->generateCoverage();
|
||||||
$totalcovered = 0;
|
$notcoveredArray = $this->generateNotCoveredFiles();
|
||||||
$totalmaybe = 0;
|
$coveredArray = array_merge($coveredArray, $notcoveredArray);
|
||||||
$totalnotcovered = 0;
|
|
||||||
|
|
||||||
|
//lets sort it.
|
||||||
|
uasort($coveredArray, array($this,"sortArray"));
|
||||||
|
|
||||||
|
//and flip if it perhaps?
|
||||||
|
if(isset($_GET["desc"]) && $_GET["desc"] == "true"){
|
||||||
|
$coveredArray = array_reverse($coveredArray, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//ugly code to print out the result:
|
||||||
|
echo "<tr><td>" . TOTAL . "</td><td>" . round((($this->totalcovered + $this->totalmaybe) / $this->totallines) * 100, 2) . " % </td><td>$this->totallines</td><td>$this->totalcovered</td><td>$this->totalmaybe</td><td>$this->totalnotcovered</td><td></td></tr>";
|
||||||
|
foreach($coveredArray as $class => $info){
|
||||||
|
$fileName = str_replace("_", "/", $class) . ".php";
|
||||||
|
echo "<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><a href=\"cc.php?file=" . $fileName . "\">coverage</a></td></tr>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateCoverage()
|
||||||
|
{
|
||||||
$coveredArray = array();
|
$coveredArray = array();
|
||||||
foreach ($this->coverage as $file => $lines) {
|
foreach ($this->coverage as $file => $lines) {
|
||||||
$pos = strpos($file, $this->path);
|
$pos = strpos($file, $this->path);
|
||||||
@ -142,22 +218,22 @@ class Doctrine_Coverage_Report
|
|||||||
$notcovered = 0;
|
$notcovered = 0;
|
||||||
foreach($lines as $result){
|
foreach($lines as $result){
|
||||||
switch($result){
|
switch($result){
|
||||||
case "1":
|
case self::COVERED:
|
||||||
$covered++;
|
$covered++;
|
||||||
break;
|
break;
|
||||||
case "-1":
|
case self::NOTCOVERED:
|
||||||
$notcovered++;
|
$notcovered++;
|
||||||
break;
|
break;
|
||||||
case "-2":
|
case self::MAYBE:
|
||||||
$maybe++;
|
$maybe++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$covered--; //again we have to remove that last line.
|
$covered--; //again we have to remove that last line.
|
||||||
$totallines += $total;
|
$this->totallines += $total;
|
||||||
$totalcovered += $covered;
|
$this->totalcovered += $covered;
|
||||||
$totalnotcovered += $notcovered;
|
$this->totalnotcovered += $notcovered;
|
||||||
$totalmaybe += $maybe;
|
$this->totalmaybe += $maybe;
|
||||||
|
|
||||||
if ($total === 0) {
|
if ($total === 0) {
|
||||||
$total = 1;
|
$total = 1;
|
||||||
@ -165,18 +241,7 @@ class Doctrine_Coverage_Report
|
|||||||
$percentage = round((($covered + $maybe) / $total) * 100, 2);
|
$percentage = round((($covered + $maybe) / $total) * 100, 2);
|
||||||
$coveredArray[$class] = array("covered" => $covered, "maybe" => $maybe, "notcovered"=>$notcovered, "total" => $total, "percentage" => $percentage);
|
$coveredArray[$class] = array("covered" => $covered, "maybe" => $maybe, "notcovered"=>$notcovered, "total" => $total, "percentage" => $percentage);
|
||||||
}
|
}
|
||||||
|
return $coveredArray;
|
||||||
|
|
||||||
//lets sort it
|
|
||||||
uasort($coveredArray, array($this,"sortArray"));
|
|
||||||
if(isset($_GET["desc"]) && $_GET["desc"] == "true"){
|
|
||||||
$coveredArray = array_reverse($coveredArray, true);
|
|
||||||
}
|
|
||||||
echo "<tr><td>" . TOTAL . "</td><td>" . round((($totalcovered + $totalmaybe) / $totallines) * 100, 2) . " % </td><td>$totallines</td><td>$totalcovered</td><td>$totalmaybe</td><td>$totalnotcovered</td><td></td></tr>";
|
|
||||||
foreach($coveredArray as $class => $info){
|
|
||||||
$fileName = str_replace("_", "/", $class) . ".php";
|
|
||||||
echo "<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><a href=\"cc.php?file=" . $fileName . "\">coverage</a></td></tr>";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sortArray($a, $b)
|
public function sortArray($a, $b)
|
||||||
|
File diff suppressed because one or more lines are too long
@ -419,18 +419,15 @@ if(PHP_SAPI === "cli"){
|
|||||||
$reporter = new MyReporter();
|
$reporter = new MyReporter();
|
||||||
}
|
}
|
||||||
|
|
||||||
//use this for normal testing
|
$argv = $_SERVER["argv"];
|
||||||
$test->run($reporter);
|
if(isset($argv[1]) && $argv[1] == "coverage"){
|
||||||
|
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
|
||||||
|
$test->run($reporter);
|
||||||
|
$result["path"] = Doctrine::getPath() . DIRECTORY_SEPARATOR;
|
||||||
|
$result["coverage"] = xdebug_get_code_coverage();
|
||||||
|
xdebug_stop_code_coverage();
|
||||||
|
file_put_contents("coverage.txt", serialize($result));
|
||||||
|
}else{
|
||||||
|
$test->run($reporter);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Use this code to get code coverage. Then goto cc.php file to see resul
|
|
||||||
*
|
|
||||||
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
|
|
||||||
$test->run($reporter);
|
|
||||||
$result["path"] = Doctrine::getPath() . DIRECTORY_SEPARATOR;
|
|
||||||
$result["coverage"] = xdebug_get_code_coverage();
|
|
||||||
xdebug_stop_code_coverage();
|
|
||||||
file_put_contents("coverage.txt", serialize($result));
|
|
||||||
//look at the cc.php file in a browser to see the report
|
|
||||||
//*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user