Code coverage

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@514 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2008-10-02 16:42:55 +00:00
parent c49331e594
commit bb1ae0ba54
6 changed files with 108 additions and 1 deletions

View file

@ -101,6 +101,7 @@ if ($_SERVER["argc"] > 1) {
$filename = "phpMinAdmin" . ($_COOKIE["lang"] ? "-$_COOKIE[lang]" : "") . ".php";
$file = file_get_contents("index.php");
$file = preg_replace_callback('~(<\\?php)?\\s*(include|require) "([^"]*)";(\\s*\\?>)?~', 'put_file', $file);
$file = preg_replace("~if \\(isset\\(\\\$_SESSION\\[\"coverage.*\n}\n| && !isset\\(\\\$_SESSION\\[\"coverage\"\\]\\)~sU", '', $file);
if ($_COOKIE["lang"]) {
$file = preg_replace_callback("~(<\\?php\\s*echo )?lang\\('((?:[^\\\\']+|\\\\.)*)'([,)])(;\\s*\\?>)?~s", 'remove_lang', $file);
$file = str_replace("<?php switch_lang(); ?>\n", "", $file);

66
_coverage.php Normal file
View file

@ -0,0 +1,66 @@
<?php
error_reporting(E_ALL & ~E_NOTICE);
if (!ini_get("session.auto_start")) {
session_name("phpMinAdmin_SID");
session_set_cookie_params(ini_get("session.cookie_lifetime"), preg_replace('~_coverage\\.php(\\?.*)?$~', '', $_SERVER["REQUEST_URI"]));
session_start();
}
function xhtml_open_tags($s) {
$return = array();
preg_match_all('~<([^>]+)~', $s, $matches);
foreach ($matches[1] as $val) {
if ($val{0} == "/") {
array_pop($return);
} elseif (substr($val, -1) != "/") {
$return[] = $val;
}
}
return $return;
}
if ($_GET["start"]) {
$_SESSION["coverage"] = array();
header("Location: .");
exit;
} elseif ($_GET["filename"]) {
$filename = basename($_GET["filename"]);
$coverage = $_SESSION["coverage"][realpath($filename)];
$file = explode("<br />", highlight_file($filename, true));
unset($prev_color);
$s = "";
for ($l=0; $l <= count($file); $l++) {
$line = $file[$l];
$color = "#C0FFC0"; // tested
switch ($coverage[$l+1]) {
case -1: $color = "#FFC0C0"; break; // untested
case -2: $color = "Silver"; break; // dead code
case null: $color = ""; break; // not executable
}
if (!isset($prev_color)) {
$prev_color = $color;
}
if ($prev_color != $color || !isset($line)) {
echo "<div" . ($prev_color ? " style='background-color: $prev_color;'" : "") . ">" . $s;
$open_tags = xhtml_open_tags($s);
foreach (array_reverse($open_tags) as $tag) {
echo "</" . preg_replace('~ .*~', '', $tag) . ">";
}
echo "</div>\n";
$s = ($open_tags ? "<" . implode("><", $open_tags) . ">" : "");
$prev_color = $color;
}
$s .= "$line<br />\n";
}
} elseif (isset($_SESSION["coverage"])) {
echo "<ul>\n";
foreach (glob("*.php") as $filename) {
if ($filename{0} != "_") {
$coverage = $_SESSION["coverage"][realpath($filename)];
echo "<li><a href='_coverage.php?filename=$filename'>$filename</a> (" . (isset($coverage) ? "tested" : "untested") . ")</li>\n";
}
}
echo "</ul>\n";
}
?>
<p><a href="_coverage.php?start=1">Start new coverage</a> (requires <a href="http://www.xdebug.org">Xdebug</a>)</p>

View file

@ -53,7 +53,7 @@ function toggle(id) {
if (strlen($_GET["db"]) && $databases && !in_array($_GET["db"], $databases, true)) {
$databases = null;
}
if (isset($databases) && !isset($_GET["sql"])) {
if (isset($databases) && !isset($_GET["sql"]) && !isset($_SESSION["coverage"])) {
session_write_close();
}
if ($error) {

View file

@ -12,6 +12,19 @@ if (!ini_get("session.auto_start")) {
session_set_cookie_params(ini_get("session.cookie_lifetime"), preg_replace('~\\?.*~', '', $_SERVER["REQUEST_URI"]));
session_start();
}
if (isset($_SESSION["coverage"])) {
function save_coverage() {
foreach (xdebug_get_code_coverage() as $filename => $lines) {
foreach ($lines as $l => $val) {
if (!$_SESSION["coverage"][$filename][$l] || $val > 0) {
$_SESSION["coverage"][$filename][$l] = $val;
}
}
}
}
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
register_shutdown_function('save_coverage');
}
if (get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST);
while (list($key, $val) = each($process)) {

View file

@ -11,6 +11,11 @@
<thead>
<tr><td rowspan="1" colspan="3">Create database</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/phpMinAdmin/_coverage.php?start=1</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>/phpMinAdmin/?lang=en&amp;username=</td>

22
tests/coverage.html Normal file
View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="" />
<title>Coverage</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Coverage</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/phpMinAdmin/_coverage.php</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>