cc.php 10.1 KB
Newer Older
meus's avatar
meus committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  $Id: Doctrine.php 1976 2007-07-11 22:03:47Z zYne $
 *
 * 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>.
 */

meus's avatar
meus committed
22 23 24
require_once dirname(__FILE__) . '/../lib/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload'));

25 26 27 28 29 30 31 32 33
$reporter = new Doctrine_Coverage_Report("coverage.txt");
?>
<html>
<head>
<style type="text/css">
    .covered{ background: green;}
    .normal{ background: white;}
    .red{ background: red;}
    .orange{ background: #f90;}
meus's avatar
meus committed
34 35


36 37 38 39
</style>
</head>
<body>
<?php
meus's avatar
meus committed
40

41
if (isset($_GET["file"])){
42 43
    echo '<h1>Coverage for ' . $_GET["file"] . '</h1>';
    echo '<a href="cc.php">Back to coverage report</a>';
44
    echo "<br>detailed view down atm <br> Will be up later on<br>";
meus's avatar
meus committed
45 46
   

47
   // $reporter->showFile($_GET["file"]);
48
} else {
meus's avatar
meus committed
49
?>
50
    <h1>Coverage report for Doctrine</h1>
meus's avatar
meus committed
51
    <p>Default mode shows results sorted by percentage. This can be changed with GET variables:<br /> <ul><li>order = covered|total|maybe|notcovered|percentage</li><li>desc=true</li></ul></p>
52 53
    <table>
     <tr><th></th><th>Percentage</th><th>Total</th><th>Covered</th><th>Maybe</th><th>Not Covered</th><th></th></tr>
meus's avatar
meus committed
54
<?php
55 56
    $reporter->showSummary();
    echo "</table>";
meus's avatar
meus committed
57
}
58 59 60 61 62
?>
</body>
</html>

<?php
63 64 65
/**
 * Doctrine
 * the base class of Doctrine framework
66
 *
67 68 69 70 71 72 73
 * @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 $
74 75 76 77
 */
class Doctrine_Coverage_Report
{

78 79 80 81
    const COVERED = 1;
    const MAYBE = -2;
    const NOTCOVERED = -1;

82 83 84 85
    private $path;
    private $coverage;
    private $key;
    private $covered;
86 87 88 89
    private $totallines = 0;
    private $totalcovered = 0;
    private $totalmaybe = 0;
    private $totalnotcovered = 0;
90

91 92 93 94 95 96
    /*
     * Create a new coverage report
     *
     * @param string $file The name of the file where coverage data is stored
     *
     */
97 98 99 100 101 102 103
    public function __construct($file)
    {
        $result = unserialize(file_get_contents("coverage.txt"));
        $this->path = $result["path"];
        $this->coverage = $result["coverage"];

        $this->sortBy ="percentage"; // default sort
meus's avatar
meus committed
104 105
    }

106 107 108 109 110
    /*
     * Show graphical coverage report for a file
     *
     * @param string $fileName The name of the file to show
     */
111 112 113 114
    public function showFile($fileName)
    {
        $key = $this->path . $fileName;
        $html = '<div id="coverage">';
115
        if ( ! isset( $this->coverage[$key]))
116
        {
117
            echo '<h2>This file has not been tested!</h2>';
118 119 120
        }
        $coveredLines = $this->coverage[$key];
        $fileArray = file(Doctrine::getPath() . "/".$fileName);
121
        $html .= '<table>' . "\n";
122
        foreach ($fileArray as $num => $line){
123
            $linenum = $num+1;
124
            $html .= '<tr><td>' . $linenum . '</td>' . "\n";
125
            $class ="normal";
126
            if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == 1){
127
                $class = "covered";
128
            } else if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -1) {
129
                $class ="red";
130
            } else if (isset($coveredLines[$linenum]) && $coveredLines[$linenum] == -2) {
131 132
                $class ="orange";
            }
133 134 135
            
            $line = str_replace(" ", "&nbsp;", htmlspecialchars($line));
            $html .= '<td class="' . $class . '">' . $line . '</td></tr>' . "\n";
136
        }
137
        $html .='</table></div>';
138 139 140
        echo $html;
    }

141 142 143 144 145 146 147 148
    /*
     * 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
     */
149 150 151 152 153
    public function generateNotCoveredFiles()
    {
        $it = new RecursiveDirectoryIterator(Doctrine::getPath());

        $notCoveredArray = array();
154 155
        foreach (new RecursiveIteratorIterator($it) as $file){
            if (strpos($file->getPathname(), ".svn")){
156 157
                continue;
            }
meus's avatar
meus committed
158
            $path = Doctrine::getPath() . DIRECTORY_SEPARATOR;
159
            $coveredPath = str_replace($path, $this->path, $file->getPathname());
160
            if (isset($this->coverage[$coveredPath])){
161
                continue;
162
            }
163

164 165 166
            $class = str_replace($path, "", $file->getPathname());
            $class = str_replace(DIRECTORY_SEPARATOR, "_", $class);
            $class = substr($class, 0,-4);
167 168 169 170
            if (strpos($class, '_Interface')) {
                continue;
            }

171
            if ( ! class_exists($class)){
172 173 174 175 176
                continue;
            }

            try{
                $refClass = new ReflectionClass($class);
177
            } catch (Exception $e){
178 179 180 181 182
                echo $e->getMessage();
                continue;
            }
            $lines = 0;
            $methodLines = 0;
183
            foreach ($refClass->getMethods() as $refMethod){
184

185
                if ($refMethod->getDeclaringClass() != $refClass){
186 187 188 189 190
                    continue;
                }
                $methodLines = $refMethod->getEndLine() - $refMethod->getStartLine();
                $lines += $methodLines;
            }
191
            if ($methodLines == 0){
192
                $notCoveredArray[$class] = array("covered" => 0, "maybe" => 0, "notcovered"=>$lines, "total" => $lines, "percentage" => 100);
193
            } else {
194
                $notCoveredArray[$class] = array("covered" => 0, "maybe" => 0, "notcovered"=>$lines, "total" => $lines, "percentage" => 0);
195
            }
196 197 198 199 200 201
            $this->totallines += $lines;
            $this->totalnotcovered += $lines;
        }
        return $notCoveredArray;
    }

202 203 204 205 206 207
    /*
     * Show a summary of all files in Doctrine and their coverage data
     *
     * @uses generateNonCoveredFiles
     * @uses generateCoverage
     */
208 209
    public function showSummary()
    {
210
        if (isset($_GET["order"])){
211 212
            $this->sortBy = $_GET["order"];
        }
213 214 215 216 217 218 219 220
        $coveredArray = $this->generateCoverage();
        $notcoveredArray = $this->generateNotCoveredFiles();
        $coveredArray = array_merge($coveredArray, $notcoveredArray);

        //lets sort it.
        uasort($coveredArray, array($this,"sortArray"));

        //and flip if it perhaps?
221
        if (isset($_GET["desc"]) && $_GET["desc"] == "true"){
222 223 224 225 226 227 228 229 230 231
            $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>";
        }
    }
232

233 234 235 236 237
    /*
     * Generate coverage data for tested files
     *
     *@return array An array of coverage data
     */
238 239
    public function generateCoverage()
    {
240 241 242
        $coveredArray = array();
        foreach ($this->coverage as $file => $lines) {
            $pos = strpos($file, $this->path);
243
            if ($pos === false && $pos !== 0){
244 245 246 247 248 249 250 251 252
                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;
            }

253
            if ( ! class_exists($class)){
254 255 256 257 258 259 260
                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;
261
            foreach ($lines as $result){
262
                switch($result){
263
                case self::COVERED:
264 265
                    $covered++;
                    break;
266
                case self::NOTCOVERED:
267 268
                    $notcovered++;
                    break;
269
                case self::MAYBE:
270 271 272 273 274
                    $maybe++;
                    break;
                }
            }
            $covered--; //again we have to remove that last line.
275 276 277 278
            $this->totallines += $total;
            $this->totalcovered += $covered;
            $this->totalnotcovered += $notcovered;
            $this->totalmaybe += $maybe;
meus's avatar
meus committed
279

280 281 282 283 284 285
            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);
        }
286
        return $coveredArray;
287 288
    }

289 290 291 292
   /*
    * Uasort function to sort the array by key
    *
    */
293 294 295 296 297
    public function sortArray($a, $b)
    {
        if ($a[$this->sortBy] == $b[$this->sortBy]) {
            return 0;
        }
298
        return ( $a[$this->sortBy] < $b[$this->sortBy]) ? -1 : 1;
meus's avatar
meus committed
299 300
    }
}
301