Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- Refactor test database schema.

  • Loading branch information...
commit 0d9f4ae9d401bf131072da3680ab28b81c44f8d4 1 parent a04e282
@sebastianbergmann authored
View
4 PHPUnit/TextUI/TestRunner.php
@@ -287,7 +287,9 @@ public function doRun(PHPUnit_Framework_Test $suite, array $parameters = array()
$this->printer->write("\nStoring code coverage data in database, this may take a moment.");
$testDb = new PHPUnit_Util_Database($dbh);
- $testDb->storeCodeCoverage($result, $dbListener->getRunId());
+ $testDb->storeCodeCoverage(
+ $result, $parameters['testDatabaseLogRevision']
+ );
$this->printer->write("\n");
}
View
240 PHPUnit/Util/Database.php
@@ -88,10 +88,10 @@ public function __construct(PDO $dbh)
* Stores code coverage information.
*
* @param PHPUnit_Framework_TestResult $result
- * @param integer $runId
+ * @param integer $revision
* @access public
*/
- public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $runId)
+ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revision)
{
if (defined('PHPUnit_INSIDE_OWN_TESTSUITE')) {
$filterPHPUnit = FALSE;
@@ -105,152 +105,186 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $runId)
$files = array_keys($summary);
$commonPath = PHPUnit_Util_Filesystem::getCommonPath($files);
+ $this->dbh->beginTransaction();
+
foreach ($files as $file) {
- $this->dbh->beginTransaction();
+ $filename = str_replace($commonPath, '', $file);
+ $fileId = FALSE;
+ $lines = file($file);
+ $numLines = count($lines);
- $this->dbh->exec(
+ $stmt = $this->dbh->query(
sprintf(
- 'INSERT INTO code_file
- (run_id, code_file_name, code_file_md5)
- VALUES(%d, "%s", "%s");',
+ 'SELECT code_file_id
+ FROM code_file
+ WHERE code_file_name = "%s"
+ AND revision = %d;',
- $runId,
- str_replace($commonPath, '', $file),
- md5_file($file)
+ $filename,
+ $revision
)
);
- $fileId = $this->dbh->lastInsertId();
- $classes = PHPUnit_Util_Class::getClassesInFile($file, $commonPath);
- $methodMap = array();
+ if ($stmt) {
+ $fileId = (int)$stmt->fetchColumn();
+ }
+
+ unset($stmt);
- foreach ($classes as $class) {
+ if ($fileId == 0) {
$this->dbh->exec(
sprintf(
- 'INSERT INTO code_class
- (code_file_id, code_class_name,
- code_class_start_line, code_class_end_line)
- VALUES(%d, "%s", %d, %d);',
-
- $fileId,
- $class->getName(),
- $class->getStartLine(),
- $class->getEndLine()
+ 'INSERT INTO code_file
+ (code_file_name, code_file_md5, revision)
+ VALUES("%s", "%s", %d);',
+
+ $filename,
+ md5_file($file),
+ $revision
)
);
- $classId = $this->dbh->lastInsertId();
-
- foreach ($class->getMethods() as $method) {
- if ($class->getName() != $method->getDeclaringClass()->getName()) {
- continue;
- }
-
- $startLine = $method->getStartLine();
- $endLine = $method->getEndLine();
+ $fileId = $this->dbh->lastInsertId();
+ $classes = PHPUnit_Util_Class::getClassesInFile($file, $commonPath);
+ $methodMap = array();
+ foreach ($classes as $class) {
$this->dbh->exec(
sprintf(
- 'INSERT INTO code_method
- (code_class_id, code_method_name,
- code_method_start_line, code_method_end_line)
+ 'INSERT INTO code_class
+ (code_file_id, code_class_name,
+ code_class_start_line, code_class_end_line)
VALUES(%d, "%s", %d, %d);',
- $classId,
- $method->getName(),
- $startLine,
- $endLine
+ $fileId,
+ $class->getName(),
+ $class->getStartLine(),
+ $class->getEndLine()
)
);
- $methodId = $this->dbh->lastInsertId();
-
- for ($i = $startLine; $i <= $endLine; $i++) {
- $methodMap[$i] = $methodId;
- }
- }
- }
+ $classId = $this->dbh->lastInsertId();
- $i = 1;
- $lines = file($file);
+ foreach ($class->getMethods() as $method) {
+ if ($class->getName() != $method->getDeclaringClass()->getName()) {
+ continue;
+ }
- foreach ($lines as $line) {
- $covered = isset($summary[$file][$i]) ? $summary[$file][$i] : 0;
+ $startLine = $method->getStartLine();
+ $endLine = $method->getEndLine();
- $this->dbh->exec(
- sprintf(
- 'INSERT INTO code_line
- (code_file_id, code_method_id, code_line_number,
- code_line, code_line_covered)
- VALUES(%d, %d, %d, "%s", %d);',
-
- $fileId,
- isset($methodMap[$i]) ? $methodMap[$i] : 0,
- $i,
- trim($line),
- $covered
- )
- );
+ $this->dbh->exec(
+ sprintf(
+ 'INSERT INTO code_method
+ (code_class_id, code_method_name,
+ code_method_start_line, code_method_end_line)
+ VALUES(%d, "%s", %d, %d);',
- if ($covered > 0) {
- $lineId = $this->dbh->lastInsertId();
+ $classId,
+ $method->getName(),
+ $startLine,
+ $endLine
+ )
+ );
- $coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests(
- $codeCoverage, $file, $i
- );
+ $methodId = $this->dbh->lastInsertId();
- if (is_array($coveringTests)) {
- foreach ($coveringTests as $test) {
- $this->dbh->exec(
- sprintf(
- 'INSERT INTO code_coverage
- (test_id, code_line_id)
- VALUES(%d, %d);',
-
- $test->__db_id,
- $lineId
- )
- );
+ for ($i = $startLine; $i <= $endLine; $i++) {
+ $methodMap[$i] = $methodId;
}
}
}
- $i++;
- }
+ $i = 1;
- foreach ($result->topTestSuite() as $test) {
- if ($test instanceof PHPUnit_Framework_TestCase) {
- $stmt = $this->dbh->query(
+ foreach ($lines as $line) {
+ $this->dbh->exec(
sprintf(
- 'SELECT code_method.code_method_id
- FROM code_class, code_method
- WHERE code_class.code_class_id = code_method.code_class_id
- AND code_class.code_class_name = "%s"
- AND code_method.code_method_name = "%s";',
-
- get_class($test),
- $test->getName()
+ 'INSERT INTO code_line
+ (code_file_id, code_method_id,
+ code_line_number, code_line)
+ VALUES(%d, %d, %d, "%s");',
+
+ $fileId,
+ isset($methodMap[$i]) ? $methodMap[$i] : 0,
+ $i,
+ trim($line)
)
);
- $methodId = (int)$stmt->fetchColumn();
- unset($stmt);
+ $i++;
+ }
+ }
- $this->dbh->exec(
+ for ($lineNumber = 1; $lineNumber <= $numLines; $lineNumber++) {
+ $coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests(
+ $codeCoverage, $file, $lineNumber
+ );
+
+ if (is_array($coveringTests)) {
+ $stmt = $this->dbh->query(
sprintf(
- 'UPDATE test
- SET code_method_id = %d
- WHERE test_id = %d;',
+ 'SELECT code_line_id
+ FROM code_line
+ WHERE code_file_id = %d
+ AND code_line_number = %d;',
- $methodId,
- $test->__db_id
+ $fileId,
+ $lineNumber
)
);
+
+ $codeLineId = (int)$stmt->fetchColumn();
+ unset($stmt);
+
+ foreach ($coveringTests as $test) {
+ $this->dbh->exec(
+ sprintf(
+ 'INSERT INTO code_coverage
+ (test_id, code_line_id)
+ VALUES(%d, %d);',
+
+ $test->__db_id,
+ $codeLineId
+ )
+ );
+ }
}
}
+ }
+
+ foreach ($result->topTestSuite() as $test) {
+ if ($test instanceof PHPUnit_Framework_TestCase) {
+ $stmt = $this->dbh->query(
+ sprintf(
+ 'SELECT code_method.code_method_id
+ FROM code_class, code_method
+ WHERE code_class.code_class_id = code_method.code_class_id
+ AND code_class.code_class_name = "%s"
+ AND code_method.code_method_name = "%s";',
+
+ get_class($test),
+ $test->getName()
+ )
+ );
- $this->dbh->commit();
+ $methodId = (int)$stmt->fetchColumn();
+ unset($stmt);
+
+ $this->dbh->exec(
+ sprintf(
+ 'UPDATE test
+ SET code_method_id = %d
+ WHERE test_id = %d;',
+
+ $methodId,
+ $test->__db_id
+ )
+ );
+ }
}
+
+ $this->dbh->commit();
}
}
?>
View
5 PHPUnit/Util/Database/MySQL.sql
@@ -64,12 +64,10 @@ CREATE TABLE IF NOT EXISTS test(
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS code_file(
- run_id INTEGER UNSIGNED NOT NULL REFERENCES run.run_id,
code_file_id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
code_file_name CHAR(255),
code_file_md5 CHAR(32),
-
- INDEX (run_id)
+ revision INTEGER UNSIGNED NOT NULL
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS code_class(
@@ -98,7 +96,6 @@ CREATE TABLE IF NOT EXISTS code_line(
code_line_id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
code_line_number INTEGER UNSIGNED NOT NULL,
code_line TEXT,
- code_line_covered TINYINT UNSIGNED NOT NULL,
INDEX (code_file_id),
INDEX (code_method_id)
View
9 PHPUnit/Util/Database/SQLite3.sql
@@ -64,14 +64,12 @@ CREATE INDEX IF NOT EXISTS test_node_left ON test (node_left);
CREATE INDEX IF NOT EXISTS test_node_right ON test (node_right);
CREATE TABLE IF NOT EXISTS code_file(
- run_id INTEGER,
code_file_id INTEGER PRIMARY KEY AUTOINCREMENT,
code_file_name TEXT,
- code_file_md5 TEXT
+ code_file_md5 TEXT,
+ revision INTEGER
);
-CREATE INDEX IF NOT EXISTS code_file_run_id ON code_file (run_id);
-
CREATE TABLE IF NOT EXISTS code_class(
code_file_id INTEGER,
code_class_id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -97,8 +95,7 @@ CREATE TABLE IF NOT EXISTS code_line(
code_method_id INTEGER,
code_line_id INTEGER PRIMARY KEY AUTOINCREMENT,
code_line_number INTEGER,
- code_line TEXT,
- code_line_covered INTEGER
+ code_line TEXT
);
CREATE INDEX IF NOT EXISTS code_line_code_file_id ON code_line (code_file_id);
Please sign in to comment.
Something went wrong with that request. Please try again.