Coverage.php 11.3 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
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_UnitTestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @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$
 */

class DoctrineTest_Coverage
{

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

    private $covered;
    private $totallines = 0;
    private $totalcovered = 0;
    private $totalmaybe = 0;
    private $totalnotcovered = 0;
    private $result;

    /*
     * Create a new Coverage object. We read data from a fixed file. 
     */
    public function __construct()
    {
        $this->result = unserialize(file_get_contents($this->getCoverageDir() . "coverage.txt"));
        $this->sortBy ="percentage"; // default sort
    }

    /**
     * Get the directory to store coverage report in
     *
     * @return string The path to store the coverage in
     */
    public function getCoverageDir(){
        $dir = Doctrine::getPath() . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "tests" . DIRECTORY_SEPARATOR . "coverage" . DIRECTORY_SEPARATOR;
        return $dir;
    }

    /*
     * Show a summary of all files in Doctrine and their coverage data
     *
     */
    public function showSummary()
    {
        if ( isset($_GET['order'])){
            $this->sortBy = $_GET['order'];
        }

        if ( ! isset($this->result['data'])){
            die("Impropper coverage report. Please regenerate");
        }

        $coveredArray = $this->result["data"];
        //lets sort it.
        uasort($coveredArray, array($this,"sortArray"));

        //and flip if it perhaps?
87
        if (isset($_GET["flip"]) && $_GET["flip"] == "true"){
88 89 90 91 92 93 94 95 96 97 98 99 100
            $coveredArray = array_reverse($coveredArray, true);
        }

        $totals = $this->result["totals"];

        echo '<tr><td>TOTAL</td><td>' , 
            $totals['percentage'] , '%</td><td>' , 
            $totals['lines'] , '</td><td>' , 
            $totals['covered'] , '</td><td>', 
            $totals['maybe'] , '</td><td>',
            $totals['notcovered'] , '</td><td></tr>';

        foreach($coveredArray as $class => $info){
101 102

            echo '<tr><td>';
103
            if ( $info['type'] == "covered") {
104 105 106
                echo '<a href="' , $class , '.html">', $class , '</a>';
            }else{
                echo $class;
107
            }
108
            echo '<td>' . $info['percentage'] . ' % </td><td>' . $info['total'] . '</td><td>' . $info['covered'] . '</td><td>' . $info['maybe'] . '</td><td>' . $info['notcovered']. '</td></tr>';
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
        }
    }


    /**
     * Return the revision the coverage was made against
     *
     *@param int The revision number
     */
    public function getRevision(){
        return $this->result["revision"];
    }

    /**
     * Generate the report.
     *
     * This method will analyze the coverage data and create a data array that 
     * contains information about each of the classes in Doctrine/lib. It will 
     * also generate html files for each file that has coverage data with 
     * information about what lines that are covered. 
     *
     *
     * @uses generateCoverageInfoCoveredFile
     * @uses saveFile
     * @uses generateCoverageInfoNotCoveredFile
     * @uses getCoverageDir
     * @uses calculateTotalPercentage
     *
     */
    public function generateReport(){
        $svn_info = explode(" ", exec("svn info | grep Revision"));
        $this->result["revision"] = $svn_info[1];

        //loop through all files and generate coverage files for them
        $it = new RecursiveDirectoryIterator(Doctrine::getPath());
        $notCoveredArray = array();
        foreach (new RecursiveIteratorIterator($it) as $file){

147
            if(strpos($file->getPathname(), "config.php")){
148 149 150
                continue;
            }

151 152 153 154
            if (strpos($file->getPathname(), ".svn")){
                continue;
            } 
            
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
            $class = $this->getClassNameFromFileName($file->getPathname());

            if (strpos($class, '_Interface')) {
                continue;
            }

            if ( ! class_exists($class)){
                continue;
            }
            if (isset($this->result['coverage'][$file->getPathname()])){
                $coverageInfo[$class] = $this->generateCoverageInfoCoveredFile($file->getPathname());
                $this->saveFile($file->getPathname());
            }else{
                $coverageInfo[$class] = $this->generateCoverageInfoNotCoveredFile($class);
            }
        }
        $this->result["totals"] = array(
            "lines" => $this->totallines, 
            "notcovered" => $this->totalnotcovered,
            "covered" => $this->totalcovered, 
            "maybe" => $this->totalmaybe, 
            "percentage" => $this->calculateTotalPercentage());  

        $this->result["data"] = $coverageInfo;

        file_put_contents($this->getCoverageDir() . "coverage.txt", serialize($this->result));
181
        return true;
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    }

    /**
     *
     * Return the name of a class from its filename.
     *
     * This method simply removes the Doctrine Path and raplces _ with / and 
     * removes .php to get the classname for a file
     *
     * @param string $fileName The name of the file
     * @return string The name of the class
     */
    public function getClassNameFromFileName($fileName){
        $path = Doctrine::getPath() . DIRECTORY_SEPARATOR;
        $class = str_replace($path, "", $fileName);
        $class = str_replace(DIRECTORY_SEPARATOR, "_", $class);
        $class = substr($class, 0,-4);
        return $class;
    }

    /**
     * Calculate total coverage percentage
     *
     *@return double The percetage as a double
     */
    public function calculateTotalPercentage(){
        return round((($this->totalcovered + $this->totalmaybe) / $this->totallines) * 100, 2);
    }

    /**
     * Generate Coverage for a class that is not in the coverage report.
     *
     * This method will simply check if the method has no lines that should be 
     * tested or not. Then it will return data to be stored for later use. 
     *
     * @param string $class The name of a class
     * @return array An associative array with coverage information
     */
    public function generateCoverageInfoNotCoveredFile($class){
        try{
            $refClass = new ReflectionClass($class);
        } catch (Exception $e){
            echo $e->getMessage();
        }
        $lines = 0;
        $methodLines = 0;
        foreach ($refClass->getMethods() as $refMethod){
            if ($refMethod->getDeclaringClass() != $refClass){
                continue;
            }
            $methodLines = $refMethod->getEndLine() - $refMethod->getStartLine();
            $lines += $methodLines;
        }
        $this->totallines += $lines;
        $this->totalnotcovered += $lines;
        if ($lines == 0){
            return array("covered" => 0, "maybe" => 0, "notcovered"=>$lines, "total" => $lines, "percentage" => 100, "type" => "notcovered");
        } else {
            return  array("covered" => 0, "maybe" => 0, "notcovered"=>$lines, "total" => $lines, "percentage" => 0, "type" => "notcovered");
        }
    }


    /*
     * Save a html report for the given filename 
     *
     * @param string $fileName The name of the file 
     */
    public function saveFile($fileName)
    {
        $className = $this->getClassNameFromFileName($fileName);
        $title = "Coverage for " . $className;
        
        $html = '<html>
    <head>
        <title>' . $title . '</title>
        <style type="text/css">
            .covered{ background: green;}
            .normal{ background: white;}
            .red{ background: red;}
            .orange{ background: #f90;}
       </style>
</head>
<body><h1>' . $title . '</h1><p><a href="index.php">Back to coverage report</a></p>';
        $coveredLines = $this->result["coverage"][$fileName];
        $fileArray = file($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] == self::COVERED){
                $class = "covered";
            } else if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == self::NOTCOVERED) {
                $class ="red";
            } else if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == self::MAYBE) {
                $class ="orange";
            }

            $line = str_replace(" ", "&nbsp;", htmlspecialchars($line));
            $html .= '<td class="' . $class . '">' . $line . '</td></tr>' . "\n";
        }
        $html .='</table></body></html>';
        file_put_contents($this->getCoverageDir() . $className . ".html",$html);
    }

    /*
     * Generate coverage data for tested file
     *
     *@return array An array of coverage data
     */
    public function generateCoverageInfoCoveredFile($file)
    {
        $lines = $this->result["coverage"][$file];

        $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);
        return array("covered" => $covered, "maybe" => $maybe, "notcovered"=>$notcovered, "total" => $total, "percentage" => $percentage, "type" => "covered");
    }

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