Doctrine_TestCoverage.php 8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
<?php
/**
 * Doctrine
 * the base class of Doctrine framework
 *
 * @package     Doctrine
 * @author      Bjarte S. Karlsen <bjartka@pvv.ntnu.no>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision: 1976 $
 */
class Doctrine_TestCoverage
{

    const COVERED = 1;
    const MAYBE = -2;
    const NOTCOVERED = -1;

    private $path;
    private $coverage;
    private $key;
    private $covered;
    private $totallines = 0;
    private $totalcovered = 0;
    private $totalmaybe = 0;
    private $totalnotcovered = 0;

    /*
     * Create a new coverage report
     *
     * @param string $file The name of the file where coverage data is stored
     *
     */
    public function __construct()
        $result = unserialize(file_get_contents(__FILE__ . "/coverage.txt"));
        $this->path = $result["path"];
        $this->coverage = $result["coverage"];
        $this->sortBy ="percentage"; // default sort
    }

    /*
     * Show graphical coverage report for a file
     *
     * @param string $fileName The name of the file to show
     */
    public function showFile($fileName)
    {
        $key = $this->path . $fileName;
        $html = '<div id="coverage">';
        if ( ! isset( $this->coverage[$key]))
        {
            echo '<h2>This file has not been tested!</h2>';
        }
        $coveredLines = $this->coverage[$key];
        $fileArray = file(Doctrine::getPath() . "/".$fileName);
        $html .= '<table>' . "\n";
        foreach ($fileArray as $num => $line){
            $linenum = $num+1;
            $html .= '<tr><td>' . $linenum . '</td>' . "\n";
            $class ="normal";
            if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == 1){
                $class = "covered";
            } else if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -1) {
                $class ="red";
            } else if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -2) {
                $class ="orange";
            }
            
            $line = str_replace(" ", "&nbsp;", htmlspecialchars($line));
            $html .= '<td class="' . $class . '">' . $line . '</td></tr>' . "\n";
        }
        $html .='</table></div>';
        echo $html;
    }

    /*
     * Generate coverage data for non tested files
     *
     * Scans all files and records data for those that are not in the coverage 
     * record.
     *
     * @return array An array with coverage data
     */
    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_SEPARATOR;
            $coveredPath = str_replace($path, $this->path, $file->getPathname());
            if (isset($this->coverage[$coveredPath])){
                continue;
            }

            $class = str_replace($path, "", $file->getPathname());
            $class = str_replace(DIRECTORY_SEPARATOR, "_", $class);
            $class = substr($class, 0,-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;
    }

    /*
     * Show a summary of all files in Doctrine and their coverage data
     *
     * @uses generateNonCoveredFiles
     * @uses generateCoverage
     */
    public function showSummary()
    {
        if (isset($_GET["order"])){
            $this->sortBy = $_GET["order"];
        }
        $coveredArray = $this->generateCoverage();
        $notcoveredArray = $this->generateNotCoveredFiles();
        $coveredArray = array_merge($coveredArray, $notcoveredArray);

        //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>";
        }
    }

    /*
     * Generate coverage data for tested files
     *
     *@return array An array of coverage data
     */
    public function generateCoverage()
    {
        $coveredArray = array();
        foreach ($this->coverage as $file => $lines) {
            $pos = strpos($file, $this->path);
            if ($pos === false && $pos !== 0){
                continue;
            }

            $class = str_replace(DIRECTORY_SEPARATOR, '_', substr($file, strlen($this->path), -4));
            $class = str_replace($this->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 self::COVERED:
                    $covered++;
                    break;
                case self::NOTCOVERED:
                    $notcovered++;
                    break;
                case self::MAYBE:
                    $maybe++;
                    break;
                }
            }
            $covered--; //again we have to remove that last line.
            $this->totallines += $total;
            $this->totalcovered += $covered;
            $this->totalnotcovered += $notcovered;
            $this->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);
        }
        return $coveredArray;
    }

   /*
    * Uasort function to sort the array by key
    *
    */
    public function sortArray($a, $b)
    {
        if ($a[$this->sortBy] == $b[$this->sortBy]) {
            return 0;
        }
        return ( $a[$this->sortBy] < $b[$this->sortBy]) ? -1 : 1;
    }
}