/** * @param PHPUnit_Framework_TestResult $result */ public function process(PHPUnit_Framework_TestResult $result, $minLines = 5, $minMatches = 70) { $codeCoverage = $result->getCodeCoverageInformation(); $summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage); $files = array_keys($summary); $metrics = new PHPUnit_Util_Metrics_Project($files, $summary, TRUE, $minLines, $minMatches); $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $cpd = $document->createElement('pmd-cpd'); $cpd->setAttribute('version', 'PHPUnit ' . PHPUnit_Runner_Version::id()); $document->appendChild($cpd); foreach ($metrics->getDuplicates() as $duplicate) { $xmlDuplication = $cpd->appendChild($document->createElement('duplication')); $xmlDuplication->setAttribute('lines', $duplicate['numLines']); $xmlDuplication->setAttribute('tokens', $duplicate['numTokens']); $xmlFile = $xmlDuplication->appendChild($document->createElement('file')); $xmlFile->setAttribute('path', $duplicate['fileA']->getPath()); $xmlFile->setAttribute('line', $duplicate['firstLineA']); $xmlFile = $xmlDuplication->appendChild($document->createElement('file')); $xmlFile->setAttribute('path', $duplicate['fileB']->getPath()); $xmlFile->setAttribute('line', $duplicate['firstLineB']); $xmlDuplication->appendChild($document->createElement('codefragment', PHPUnit_Util_XML::prepareString(join('', array_slice($duplicate['fileA']->getLines(), $duplicate['firstLineA'] - 1, $duplicate['numLines']))))); } $this->write($document->saveXML()); $this->flush(); }
/** * Creates a new Code Coverage information tree. * * @param PHPUnit_Framework_TestResult $result * @param PHPUnit_Util_Report_Test_Node_TestSuite $testSuite * @return PHPUnit_Util_Report_Coverage_Node_Directory * @access public * @static */ public static function create(PHPUnit_Framework_TestResult $result, PHPUnit_Util_Report_Test_Node_TestSuite $testSuite) { $codeCoverageInformation = $result->getCodeCoverageInformation(); $files = PHPUnit_Util_CodeCoverage::getSummary($codeCoverageInformation); $commonPath = self::reducePaths($files); $items = self::buildDirectoryStructure($files); $root = new PHPUnit_Util_Report_Coverage_Node_Directory($commonPath); self::addItems($root, $items, $testSuite, $files); return $root; }
/** * Renders the report. * * @param PHPUnit_Framework_TestResult $result * @param string $target * @param string $charset * @param boolean $yui * @param boolean $highlight * @param integer $lowUpperBound * @param integer $highLowerBound * @access public * @static */ public static function render(PHPUnit_Framework_TestResult $result, $target, $charset = 'ISO-8859-1', $yui = TRUE, $highlight = FALSE, $lowUpperBound = 35, $highLowerBound = 70) { self::$templatePath = sprintf('%s%sReport%sTemplate%s', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR); $codeCoverageInformation = $result->getCodeCoverageInformation(); $files = PHPUnit_Util_CodeCoverage::getSummary($codeCoverageInformation); $commonPath = self::reducePaths($files); $items = self::buildDirectoryStructure($files); $root = new PHPUnit_Util_Report_Node_Directory($commonPath, NULL); self::addItems($root, $items, $files, $yui, $highlight); self::copyFiles($target); $root->render($target, $result->topTestSuite()->getName(), $charset, $highlight, $lowUpperBound, $highLowerBound); }
/** * Appends code coverage information to the test * * @param PHPUnit_Framework_Test $test * @param array $data * @since Method available since Release 3.2.0 */ public function appendCodeCoverageInformation(PHPUnit_Framework_Test $test, $data) { $deadCode = array(); $executableCode = array(); foreach (array_keys($data) as $file) { if (PHPUnit_Util_Filter::isFiltered($file, FALSE)) { unset($data[$file]); } } $newFilesToCollect = array_diff_key($data, PHPUnit_Util_Filter::getCoveredFiles()); if (count($newFilesToCollect) > 0) { $deadCode = PHPUnit_Util_CodeCoverage::getDeadLines($newFilesToCollect); $executableCode = PHPUnit_Util_CodeCoverage::getExecutableLines($newFilesToCollect); foreach (array_keys($newFilesToCollect) as $file) { PHPUnit_Util_Filter::addCoveredFile($file); } unset($newFilesToCollect); } if ($test instanceof PHPUnit_Framework_TestCase) { $linesToBeCovered = PHPUnit_Util_Test::getLinesToBeCovered(get_class($test), $test->getName()); if (!empty($linesToBeCovered)) { $data = array_intersect_key($data, $linesToBeCovered); foreach (array_keys($data) as $file) { $data[$file] = array_intersect_key($data[$file], array_flip($linesToBeCovered[$file])); } } } $executed = PHPUnit_Util_CodeCoverage::getExecutedLines($data); unset($data); $this->codeCoverageInformation[] = array('test' => $test, 'files' => $executed, 'dead' => $deadCode, 'executable' => $executableCode); }
/** * Calcualtes stats for each file covered by the code testing * * Each member of the returned array is formatted like so: * * <code> * array( * 'coverage' => $coverage_percent_for_file, * 'loc' => $lines_of_code, * 'locExecutable' => $lines_of_executable_code, * 'locExecuted' => $lines_of_code_executed * ); * </code> * * @return array Statistics for code coverage of each file */ public function calculate_cc() { if ($this->result->getCollectCodeCoverageInformation()) { $coverage = $this->result->getCodeCoverageInformation(); $coverage_summary = PHPUnit_Util_CodeCoverage::getSummary($coverage); $stats = array(); foreach ($coverage_summary as $file => $_lines) { $stats[$file] = PHPUnit_Util_CodeCoverage::getStatistics($coverage_summary, $file); } return $stats; } return FALSE; }
/** * Calculates the Code Coverage for the class. * * @param array $codeCoverage */ protected function calculateCodeCoverage(&$codeCoverage) { $statistics = PHPUnit_Util_CodeCoverage::getStatistics($codeCoverage, $this->class->getFileName(), $this->class->getStartLine(), $this->class->getEndLine()); $this->coverage = $statistics['coverage']; $this->loc = $statistics['loc']; $this->locExecutable = $statistics['locExecutable']; $this->locExecuted = $statistics['locExecuted']; }
/** * @param PHPUnit_Framework_TestResult $result */ public function process(PHPUnit_Framework_TestResult $result) { $codeCoverage = $result->getCodeCoverageInformation(); $summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage); $files = array_keys($summary); $metrics = new PHPUnit_Util_Metrics_Project($files, $summary); $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $pmd = $document->createElement('pmd'); $pmd->setAttribute('version', 'PHPUnit ' . PHPUnit_Runner_Version::id()); $document->appendChild($pmd); foreach ($this->rules['project'] as $ruleName => $rule) { $result = $rule->apply($metrics); if ($result !== NULL) { $this->addViolation($result, $pmd, $rule); } } foreach ($metrics->getFiles() as $fileName => $fileMetrics) { $xmlFile = $document->createElement('file'); $xmlFile->setAttribute('name', $fileName); $this->added = FALSE; foreach ($this->rules['file'] as $ruleName => $rule) { $result = $rule->apply($fileMetrics); if ($result !== NULL) { $this->addViolation($result, $xmlFile, $rule); $this->added = TRUE; } } foreach ($fileMetrics->getClasses() as $className => $classMetrics) { if (!$classMetrics->getClass()->isInterface()) { $classStartLine = $classMetrics->getClass()->getStartLine(); $classEndLine = $classMetrics->getClass()->getEndLine(); $classPackage = $classMetrics->getPackage(); foreach ($this->rules['class'] as $ruleName => $rule) { $result = $rule->apply($classMetrics); if ($result !== NULL) { $this->addViolation($result, $xmlFile, $rule, $classStartLine, $classEndLine, $classPackage, $className); $this->added = TRUE; } } foreach ($classMetrics->getMethods() as $methodName => $methodMetrics) { if (!$methodMetrics->getMethod()->isAbstract()) { $this->processFunctionOrMethod($xmlFile, $methodMetrics, $classPackage); } } } } foreach ($fileMetrics->getFunctions() as $functionName => $functionMetrics) { $this->processFunctionOrMethod($xmlFile, $functionMetrics); } if ($this->added) { $pmd->appendChild($xmlFile); } } $this->write($document->saveXML()); $this->flush(); }
/** * @param PHPUnit_Framework_TestResult $result * @todo Count conditionals. */ public function process(PHPUnit_Framework_TestResult $result) { $time = time(); $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $coverage = $document->createElement('coverage'); $coverage->setAttribute('generated', $time); $coverage->setAttribute('phpunit', PHPUnit_Runner_Version::id()); $document->appendChild($coverage); $project = $document->createElement('project'); $project->setAttribute('name', $result->topTestSuite()->getName()); $project->setAttribute('timestamp', $time); $coverage->appendChild($project); $codeCoverageInformation = $result->getCodeCoverageInformation(); $files = PHPUnit_Util_CodeCoverage::getSummary($codeCoverageInformation); $packages = array(); $projectFiles = 0; $projectLoc = 0; $projectNcloc = 0; $projectClasses = 0; $projectMethods = 0; $projectCoveredMethods = 0; $projectConditionals = 0; $projectCoveredConditionals = 0; $projectStatements = 0; $projectCoveredStatements = 0; foreach ($files as $filename => $data) { $projectFiles++; $fileClasses = 0; $fileConditionals = 0; $fileCoveredConditionals = 0; $fileStatements = 0; $fileCoveredStatements = 0; $fileMethods = 0; $fileCoveredMethods = 0; $file = $document->createElement('file'); $file->setAttribute('name', $filename); $namespace = 'global'; $classes = PHPUnit_Util_Class::getClassesInFile($filename); $lines = array(); foreach ($classes as $class) { if ($class->isInterface()) { continue; } $className = $class->getName(); $methods = $class->getMethods(); $packageInformation = PHPUnit_Util_Class::getPackageInformation($className); $numMethods = 0; $fileClasses++; $projectClasses++; if (!empty($packageInformation['namespace'])) { $namespace = $packageInformation['namespace']; } $classConditionals = 0; $classCoveredConditionals = 0; $classStatements = 0; $classCoveredStatements = 0; $classCoveredMethods = 0; foreach ($methods as $method) { if ($method->getDeclaringClass()->getName() == $class->getName()) { $startLine = $method->getStartLine(); $endLine = $method->getEndLine(); $tests = array(); for ($i = $startLine; $i <= $endLine; $i++) { if (isset($files[$filename][$i])) { if (is_array($files[$filename][$i])) { foreach ($files[$filename][$i] as $_test) { $add = TRUE; foreach ($tests as $test) { if ($test === $_test) { $add = FALSE; break; } } if ($add) { $tests[] = $_test; } } $classCoveredStatements++; } $classStatements++; } } $count = count($tests); $lines[$startLine] = array('count' => $count, 'type' => 'method'); if ($count > 0) { $classCoveredMethods++; $fileCoveredMethods++; $projectCoveredMethods++; } $classStatements--; $numMethods++; $fileMethods++; $projectMethods++; } } $classXML = $document->createElement('class'); $classXML->setAttribute('name', $className); $classXML->setAttribute('namespace', $namespace); if (!empty($packageInformation['fullPackage'])) { $classXML->setAttribute('fullPackage', $packageInformation['fullPackage']); } if (!empty($packageInformation['category'])) { $classXML->setAttribute('category', $packageInformation['category']); } if (!empty($packageInformation['package'])) { $classXML->setAttribute('package', $packageInformation['package']); } if (!empty($packageInformation['subpackage'])) { $classXML->setAttribute('subpackage', $packageInformation['subpackage']); } $file->appendChild($classXML); $classMetricsXML = $document->createElement('metrics'); $classMetricsXML->setAttribute('methods', $numMethods); $classMetricsXML->setAttribute('coveredmethods', $classCoveredMethods); //$classMetricsXML->setAttribute('conditionals', $classConditionals); //$classMetricsXML->setAttribute('coveredconditionals', $classCoveredConditionals); $classMetricsXML->setAttribute('statements', $classStatements); $classMetricsXML->setAttribute('coveredstatements', $classCoveredStatements); $classMetricsXML->setAttribute('elements', $classConditionals + $classStatements + $numMethods); $classMetricsXML->setAttribute('coveredelements', $classCoveredConditionals + $classCoveredStatements + $classCoveredMethods); $classXML->appendChild($classMetricsXML); } foreach ($data as $_line => $_data) { if (is_array($_data)) { $count = count($_data); } else { if ($_data == -1) { $count = 0; } else { if ($_data == -2) { continue; } } } $lines[$_line] = array('count' => $count, 'type' => 'stmt'); } ksort($lines); foreach ($lines as $_line => $_data) { $line = $document->createElement('line'); $line->setAttribute('num', $_line); $line->setAttribute('type', $_data['type']); $line->setAttribute('count', $_data['count']); if ($_data['type'] == 'stmt') { if ($_data['count'] != 0) { $fileCoveredStatements++; } $fileStatements++; } $file->appendChild($line); } if (file_exists($filename)) { $fileMetrics = PHPUnit_Util_Metrics_File::factory($filename, $files); $fileLoc = $fileMetrics->getLoc(); $fileNcloc = $fileMetrics->getNcloc(); $fileMetricsXML = $document->createElement('metrics'); $fileMetricsXML->setAttribute('loc', $fileLoc); $fileMetricsXML->setAttribute('ncloc', $fileNcloc); $fileMetricsXML->setAttribute('classes', $fileClasses); $fileMetricsXML->setAttribute('methods', $fileMethods); $fileMetricsXML->setAttribute('coveredmethods', $fileCoveredMethods); //$fileMetricsXML->setAttribute('conditionals', $fileConditionals); //$fileMetricsXML->setAttribute('coveredconditionals', $fileCoveredConditionals); $fileMetricsXML->setAttribute('statements', $fileStatements); $fileMetricsXML->setAttribute('coveredstatements', $fileCoveredStatements); $fileMetricsXML->setAttribute('elements', $fileConditionals + $fileStatements + $fileMethods); $fileMetricsXML->setAttribute('coveredelements', $fileCoveredConditionals + $fileCoveredStatements + $fileCoveredMethods); $file->appendChild($fileMetricsXML); if ($namespace == 'global') { $project->appendChild($file); } else { if (!isset($packages[$namespace])) { $packages[$namespace] = $document->createElement('package'); $packages[$namespace]->setAttribute('name', $namespace); $project->appendChild($packages[$namespace]); } $packages[$namespace]->appendChild($file); } $projectLoc += $fileLoc; $projectNcloc += $fileNcloc; $projectStatements += $fileStatements; $projectCoveredStatements += $fileCoveredStatements; } } $projectMetricsXML = $document->createElement('metrics'); $projectMetricsXML->setAttribute('files', $projectFiles); $projectMetricsXML->setAttribute('loc', $projectLoc); $projectMetricsXML->setAttribute('ncloc', $projectNcloc); $projectMetricsXML->setAttribute('classes', $projectClasses); $projectMetricsXML->setAttribute('methods', $projectMethods); $projectMetricsXML->setAttribute('coveredmethods', $projectCoveredMethods); //$projectMetricsXML->setAttribute('conditionals', $projectConditionals); //$projectMetricsXML->setAttribute('coveredconditionals', $projectCoveredConditionals); $projectMetricsXML->setAttribute('statements', $projectStatements); $projectMetricsXML->setAttribute('coveredstatements', $projectCoveredStatements); $projectMetricsXML->setAttribute('elements', $projectConditionals + $projectStatements + $projectMethods); $projectMetricsXML->setAttribute('coveredelements', $projectCoveredConditionals + $projectCoveredStatements + $projectCoveredMethods); $project->appendChild($projectMetricsXML); $this->write($document->saveXML()); $this->flush(); }
/** * @param PHPUnit_Framework_TestResult $result * @todo Count conditionals. */ public function process(PHPUnit_Framework_TestResult $result) { $time = time(); $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $coverage = $document->createElement('coverage'); $coverage->setAttribute('generated', $time); $coverage->setAttribute('phpunit', PHPUnit_Runner_Version::id()); $document->appendChild($coverage); $project = $document->createElement('project'); $project->setAttribute('name', $result->topTestSuite()->getName()); $project->setAttribute('timestamp', $time); $coverage->appendChild($project); $codeCoverageInformation = $result->getCodeCoverageInformation(); $files = PHPUnit_Util_CodeCoverage::getSummary($codeCoverageInformation); $packages = array(); $projectStatistics = array('files' => 0, 'loc' => 0, 'ncloc' => 0, 'classes' => 0, 'methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0); foreach ($files as $filename => $data) { $namespace = 'global'; if (file_exists($filename)) { $fileStatistics = array('classes' => 0, 'methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0); $file = $document->createElement('file'); $file->setAttribute('name', $filename); $classesInFile = PHPUnit_Util_File::getClassesInFile($filename); $lines = array(); foreach ($classesInFile as $className => $_class) { $classStatistics = array('methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0); foreach ($_class['methods'] as $methodName => $method) { $classStatistics['methods']++; $methodCount = 0; for ($i = $method['startLine']; $i <= $method['endLine']; $i++) { $add = TRUE; $count = 0; if (isset($files[$filename][$i])) { if ($files[$filename][$i] != -2) { $classStatistics['statements']++; } if (is_array($files[$filename][$i])) { $classStatistics['coveredStatements']++; $count = count($files[$filename][$i]); } else { if ($files[$filename][$i] == -2) { $add = FALSE; } } } else { $add = FALSE; } $methodCount = max($methodCount, $count); if ($add) { $lines[$i] = array('count' => $count, 'type' => 'stmt'); } } if ($methodCount > 0) { $classStatistics['coveredMethods']++; } $lines[$method['startLine']] = array('count' => $methodCount, 'type' => 'method', 'name' => $methodName); } $packageInformation = PHPUnit_Util_Class::getPackageInformation($className, $_class['docComment']); if (!empty($packageInformation['namespace'])) { $namespace = $packageInformation['namespace']; } $class = $document->createElement('class'); $class->setAttribute('name', $className); $class->setAttribute('namespace', $namespace); if (!empty($packageInformation['fullPackage'])) { $class->setAttribute('fullPackage', $packageInformation['fullPackage']); } if (!empty($packageInformation['category'])) { $class->setAttribute('category', $packageInformation['category']); } if (!empty($packageInformation['package'])) { $class->setAttribute('package', $packageInformation['package']); } if (!empty($packageInformation['subpackage'])) { $class->setAttribute('subpackage', $packageInformation['subpackage']); } $file->appendChild($class); $metrics = $document->createElement('metrics'); $metrics->setAttribute('methods', $classStatistics['methods']); $metrics->setAttribute('coveredmethods', $classStatistics['coveredMethods']); //$metrics->setAttribute('conditionals', $classStatistics['conditionals']); //$metrics->setAttribute('coveredconditionals', $classStatistics['coveredConditionals']); $metrics->setAttribute('statements', $classStatistics['statements']); $metrics->setAttribute('coveredstatements', $classStatistics['coveredStatements']); $metrics->setAttribute('elements', $classStatistics['conditionals'] + $classStatistics['statements'] + $classStatistics['methods']); $metrics->setAttribute('coveredelements', $classStatistics['coveredConditionals'] + $classStatistics['coveredStatements'] + $classStatistics['coveredMethods']); $class->appendChild($metrics); $fileStatistics['methods'] += $classStatistics['methods']; $fileStatistics['coveredMethods'] += $classStatistics['coveredMethods']; $fileStatistics['conditionals'] += $classStatistics['conditionals']; $fileStatistics['coveredConditionals'] += $classStatistics['coveredConditionals']; $fileStatistics['statements'] += $classStatistics['statements']; $fileStatistics['coveredStatements'] += $classStatistics['coveredStatements']; $fileStatistics['classes']++; } foreach ($data as $_line => $_data) { if (isset($lines[$_line])) { continue; } if ($_data != -2) { $fileStatistics['statements']++; if (is_array($_data)) { $count = count($_data); $fileStatistics['coveredStatements']++; } else { $count = 0; } $lines[$_line] = array('count' => $count, 'type' => 'stmt'); } } ksort($lines); foreach ($lines as $_line => $_data) { $line = $document->createElement('line'); $line->setAttribute('num', $_line); $line->setAttribute('type', $_data['type']); if (isset($_data['name'])) { $line->setAttribute('name', $_data['name']); } $line->setAttribute('count', $_data['count']); $file->appendChild($line); } $count = PHPUnit_Util_File::countLines($filename); $metrics = $document->createElement('metrics'); $metrics->setAttribute('loc', $count['loc']); $metrics->setAttribute('ncloc', $count['ncloc']); $metrics->setAttribute('classes', $fileStatistics['classes']); $metrics->setAttribute('methods', $fileStatistics['methods']); $metrics->setAttribute('coveredmethods', $fileStatistics['coveredMethods']); //$metrics->setAttribute('conditionals', $fileStatistics['conditionals']); //$metrics->setAttribute('coveredconditionals', $fileStatistics['coveredConditionals']); $metrics->setAttribute('statements', $fileStatistics['statements']); $metrics->setAttribute('coveredstatements', $fileStatistics['coveredStatements']); $metrics->setAttribute('elements', $fileStatistics['conditionals'] + $fileStatistics['statements'] + $fileStatistics['methods']); $metrics->setAttribute('coveredelements', $fileStatistics['coveredConditionals'] + $fileStatistics['coveredStatements'] + $fileStatistics['coveredMethods']); $file->appendChild($metrics); if ($namespace == 'global') { $project->appendChild($file); } else { if (!isset($packages[$namespace])) { $packages[$namespace] = $document->createElement('package'); $packages[$namespace]->setAttribute('name', $namespace); $project->appendChild($packages[$namespace]); } $packages[$namespace]->appendChild($file); } $projectStatistics['loc'] += $count['loc']; $projectStatistics['ncloc'] += $count['ncloc']; $projectStatistics['classes'] += $fileStatistics['classes']; $projectStatistics['methods'] += $fileStatistics['methods']; $projectStatistics['coveredMethods'] += $fileStatistics['coveredMethods']; $projectStatistics['conditionals'] += $fileStatistics['conditionals']; $projectStatistics['coveredConditionals'] += $fileStatistics['coveredConditionals']; $projectStatistics['statements'] += $fileStatistics['statements']; $projectStatistics['coveredStatements'] += $fileStatistics['coveredStatements']; $projectStatistics['files']++; } } $metrics = $document->createElement('metrics'); $metrics->setAttribute('files', $projectStatistics['files']); $metrics->setAttribute('loc', $projectStatistics['loc']); $metrics->setAttribute('ncloc', $projectStatistics['ncloc']); $metrics->setAttribute('classes', $projectStatistics['classes']); $metrics->setAttribute('methods', $projectStatistics['methods']); $metrics->setAttribute('coveredmethods', $projectStatistics['coveredMethods']); //$metrics->setAttribute('conditionals', $projectStatistics['conditionals']); //$metrics->setAttribute('coveredconditionals', $projectStatistics['coveredConditionals']); $metrics->setAttribute('statements', $projectStatistics['statements']); $metrics->setAttribute('coveredstatements', $projectStatistics['coveredStatements']); $metrics->setAttribute('elements', $projectStatistics['conditionals'] + $projectStatistics['statements'] + $projectStatistics['methods']); $metrics->setAttribute('coveredelements', $projectStatistics['coveredConditionals'] + $projectStatistics['coveredStatements'] + $projectStatistics['coveredMethods']); $project->appendChild($metrics); $this->write($document->saveXML()); $this->flush(); }
/** * Stores code coverage information. * * @param PHPUnit_Framework_TestResult $result * @param integer $revision * @access public */ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $revision) { $codeCoverage = $result->getCodeCoverageInformation(FALSE, TRUE); $summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage); $files = array_keys($summary); $commonPath = PHPUnit_Util_Filesystem::getCommonPath($files); $this->dbh->beginTransaction(); foreach ($files as $file) { $filename = str_replace($commonPath, '', $file); $fileId = FALSE; $lines = file($file); $numLines = count($lines); $stmt = $this->dbh->query(sprintf('SELECT code_file_id FROM code_file WHERE code_file_name = "%s" AND revision = %d;', $filename, $revision)); if ($stmt) { $fileId = (int) $stmt->fetchColumn(); } unset($stmt); if ($fileId == 0) { $this->dbh->exec(sprintf('INSERT INTO code_file (code_file_name, code_file_md5, revision) VALUES("%s", "%s", %d);', $filename, md5_file($file), $revision)); $fileId = $this->dbh->lastInsertId(); $classes = PHPUnit_Util_Class::getClassesInFile($file, $commonPath); foreach ($classes as $class) { $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())); $classId = $this->dbh->lastInsertId(); foreach ($class->getMethods() as $method) { if ($class->getName() != $method->getDeclaringClass()->getName()) { continue; } $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);', $classId, $method->getName(), $method->getStartLine(), $method->getEndLine())); } } $i = 1; foreach ($lines as $line) { $this->dbh->exec(sprintf('INSERT INTO code_line (code_file_id, code_line_number, code_line, code_line_covered) VALUES(%d, %d, "%s", %d);', $fileId, $i, trim($line), isset($summary[$file][$i]) ? $summary[$file][$i] : 0)); $i++; } } for ($lineNumber = 1; $lineNumber <= $numLines; $lineNumber++) { $coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests($codeCoverage, $file, $lineNumber); if (is_array($coveringTests)) { $stmt = $this->dbh->query(sprintf('SELECT code_line_id, code_line_covered FROM code_line WHERE code_file_id = %d AND code_line_number = %d;', $fileId, $lineNumber)); $codeLineId = (int) $stmt->fetchColumn(0); $oldCoverageFlag = (int) $stmt->fetchColumn(1); unset($stmt); $newCoverageFlag = $summary[$file][$lineNumber]; if ($oldCoverageFlag == 0 && $newCoverageFlag != 0 || $oldCoverageFlag < 0 && $newCoverageFlag > 0) { $this->dbh->exec(sprintf('UPDATE code_line SET code_line_covered = %d WHERE code_line_id = %d;', $newCoverageFlag, $codeLineId)); } 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())); $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(); }
/** * Stores code coverage information. * * @param PHPUnit_Framework_TestResult $result * @param integer $runId * @param integer $revision * @param string $commonPath */ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $runId, $revision, $commonPath = '') { $codeCoverage = $result->getCodeCoverageInformation(FALSE); $summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage); $files = array_keys($summary); $projectMetrics = new PHPUnit_Util_Metrics_Project($files, $summary); $storedClasses = array(); if (empty($commonPath)) { $commonPath = PHPUnit_Util_Filesystem::getCommonPath($files); } $this->dbh->beginTransaction(); foreach ($files as $fileName) { $shortenedFileName = str_replace($commonPath, '', $fileName); $fileId = FALSE; $fileMetrics = $projectMetrics->getFile($fileName); $lines = $fileMetrics->getLines(); $hash = md5_file($fileName); $stmt = $this->dbh->prepare('SELECT code_file_id FROM code_file WHERE code_file_name = :shortenedFileName AND revision = :revision;'); $stmt->bindParam(':shortenedFileName', $shortenedFileName, PDO::PARAM_STR); $stmt->bindParam(':revision', $revision, PDO::PARAM_INT); $stmt->execute(); if ($stmt) { $fileId = (int) $stmt->fetchColumn(); } unset($stmt); if ($fileId == 0) { $stmt = $this->dbh->prepare('INSERT INTO code_file (code_file_name, code_full_file_name, code_file_md5, revision) VALUES(:shortenedFileName, :fullFileName, :hash, :revision);'); $stmt->bindParam(':shortenedFileName', $shortenedFileName, PDO::PARAM_STR); $stmt->bindParam(':fullFileName', $fileName, PDO::PARAM_STR); $stmt->bindParam(':hash', $hash, PDO::PARAM_STR); $stmt->bindParam(':revision', $revision, PDO::PARAM_INT); $stmt->execute(); $fileId = $this->dbh->lastInsertId(); $stmt = $this->dbh->prepare('INSERT INTO code_class (code_file_id, code_class_name, code_class_start_line, code_class_end_line) VALUES(:fileId, :className, :startLine, :endLine);'); foreach ($fileMetrics->getClasses() as $classMetrics) { $className = $classMetrics->getClass()->getName(); $classStartLine = $classMetrics->getClass()->getStartLine(); $classEndLine = $classMetrics->getClass()->getEndLine(); $stmt->bindParam(':fileId', $fileId, PDO::PARAM_INT); $stmt->bindParam(':className', $className, PDO::PARAM_STR); $stmt->bindParam(':startLine', $classStartLine, PDO::PARAM_INT); $stmt->bindParam(':endLine', $classEndLine, PDO::PARAM_INT); $stmt->execute(); $classId = $this->dbh->lastInsertId(); $storedClasses[$className] = $classId; $stmt2 = $this->dbh->prepare('INSERT INTO code_method (code_class_id, code_method_name, code_method_start_line, code_method_end_line) VALUES(:classId, :methodName, :startLine, :endLine);'); foreach ($classMetrics->getMethods() as $methodMetrics) { $methodName = $methodMetrics->getMethod()->getName(); $methodStartLine = $methodMetrics->getMethod()->getStartLine(); $methodEndLine = $methodMetrics->getMethod()->getEndLine(); $stmt2->bindParam(':classId', $classId, PDO::PARAM_INT); $stmt2->bindParam(':methodName', $methodName, PDO::PARAM_STR); $stmt2->bindParam(':startLine', $methodStartLine, PDO::PARAM_INT); $stmt2->bindParam(':endLine', $methodEndLine, PDO::PARAM_INT); $stmt2->execute(); } unset($stmt2); } $stmt = $this->dbh->prepare('INSERT INTO code_line (code_file_id, code_line_number, code_line, code_line_covered) VALUES(:fileId, :lineNumber, :line, :covered);'); $i = 1; foreach ($lines as $line) { $covered = 0; if (isset($summary[$fileName][$i])) { if (is_int($summary[$fileName][$i])) { $covered = $summary[$fileName][$i]; } else { $covered = 1; } } $stmt->bindParam(':fileId', $fileId, PDO::PARAM_INT); $stmt->bindParam(':lineNumber', $i, PDO::PARAM_INT); $stmt->bindParam(':line', $line, PDO::PARAM_STR); $stmt->bindParam(':covered', $covered, PDO::PARAM_INT); $stmt->execute(); $i++; } } $stmt = $this->dbh->prepare('INSERT INTO metrics_file (run_id, code_file_id, metrics_file_coverage, metrics_file_loc, metrics_file_cloc, metrics_file_ncloc, metrics_file_loc_executable, metrics_file_loc_executed) VALUES(:runId, :fileId, :coverage, :loc, :cloc, :ncloc, :locExecutable, :locExecuted);'); $fileCoverage = $fileMetrics->getCoverage(); $fileLoc = $fileMetrics->getLoc(); $fileCloc = $fileMetrics->getCloc(); $fileNcloc = $fileMetrics->getNcloc(); $fileLocExecutable = $fileMetrics->getLocExecutable(); $fileLocExecuted = $fileMetrics->getLocExecuted(); $stmt->bindParam(':runId', $runId, PDO::PARAM_INT); $stmt->bindParam(':fileId', $fileId, PDO::PARAM_INT); $stmt->bindParam(':coverage', $fileCoverage); $stmt->bindParam(':loc', $fileLoc, PDO::PARAM_INT); $stmt->bindParam(':cloc', $fileCloc, PDO::PARAM_INT); $stmt->bindParam(':ncloc', $fileNcloc, PDO::PARAM_INT); $stmt->bindParam(':locExecutable', $fileLocExecutable, PDO::PARAM_INT); $stmt->bindParam(':locExecuted', $fileLocExecuted, PDO::PARAM_INT); $stmt->execute(); $stmtSelectFunctionId = $this->dbh->prepare('SELECT code_function_id FROM code_file, code_function WHERE code_function.code_file_id = code_file.code_file_id AND code_file.revision = :revision AND code_function.code_function_name = :functionName;'); $stmtInsertFunction = $this->dbh->prepare('INSERT INTO metrics_function (run_id, code_function_id, metrics_function_coverage, metrics_function_loc, metrics_function_loc_executable, metrics_function_loc_executed, metrics_function_ccn, metrics_function_crap, metrics_function_npath) VALUES(:runId, :functionId, :coverage, :loc, :locExecutable, :locExecuted, :ccn, :crap, :npath);'); $stmtSelectClassId = $this->dbh->prepare('SELECT code_class_id FROM code_file, code_class WHERE code_class.code_file_id = code_file.code_file_id AND code_file.revision = :revision AND code_class.code_class_name = :className;'); $stmtInsertClass = $this->dbh->prepare('INSERT INTO metrics_class (run_id, code_class_id, metrics_class_coverage, metrics_class_loc, metrics_class_loc_executable, metrics_class_loc_executed, metrics_class_aif, metrics_class_ahf, metrics_class_cis, metrics_class_csz, metrics_class_dit, metrics_class_impl, metrics_class_mif, metrics_class_mhf, metrics_class_noc, metrics_class_pf, metrics_class_vars, metrics_class_varsnp, metrics_class_varsi, metrics_class_wmc, metrics_class_wmcnp, metrics_class_wmci) VALUES(:runId, :classId, :coverage, :loc, :locExecutable, :locExecuted, :aif, :ahf, :cis, :csz, :dit, :impl, :mif, :mhf, :noc, :pf, :vars, :varsnp, :varsi, :wmc, :wmcnp, :wmci);'); $stmtSelectMethodId = $this->dbh->prepare('SELECT code_method_id FROM code_file, code_class, code_method WHERE code_class.code_file_id = code_file.code_file_id AND code_class.code_class_id = code_method.code_class_id AND code_file.revision = :revision AND code_class.code_class_name = :className AND code_method.code_method_name = :methodName;'); $stmtInsertMethod = $this->dbh->prepare('INSERT INTO metrics_method (run_id, code_method_id, metrics_method_coverage, metrics_method_loc, metrics_method_loc_executable, metrics_method_loc_executed, metrics_method_ccn, metrics_method_crap, metrics_method_npath) VALUES(:runId, :methodId, :coverage, :loc, :locExecutable, :locExecuted, :ccn, :crap, :npath);'); foreach ($fileMetrics->getFunctions() as $functionMetrics) { $functionName = $functionMetrics->getFunction()->getName(); $stmtSelectFunctionId->bindParam(':functionName', $functionName, PDO::PARAM_STR); $stmtSelectFunctionId->bindParam(':revision', $revision, PDO::PARAM_INT); $stmtSelectFunctionId->execute(); $functionId = (int) $stmtSelectFunctionId->fetchColumn(); $stmtSelectFunctionId->closeCursor(); $functionCoverage = $functionMetrics->getCoverage(); $functionLoc = $functionMetrics->getLoc(); $functionLocExecutable = $functionMetrics->getLocExecutable(); $functionLocExecuted = $functionMetrics->getLocExecuted(); $functionCcn = $functionMetrics->getCCN(); $functionCrap = $functionMetrics->getCrapIndex(); $functionNpath = $functionMetrics->getNPath(); $stmtInsertFunction->bindParam(':runId', $runId, PDO::PARAM_INT); $stmtInsertFunction->bindParam(':functionId', $functionId, PDO::PARAM_INT); $stmtInsertFunction->bindParam(':coverage', $functionCoverage); $stmtInsertFunction->bindParam(':loc', $functionLoc, PDO::PARAM_INT); $stmtInsertFunction->bindParam(':locExecutable', $functionLocExecutable, PDO::PARAM_INT); $stmtInsertFunction->bindParam(':locExecuted', $functionLocExecuted, PDO::PARAM_INT); $stmtInsertFunction->bindParam(':ccn', $functionCcn, PDO::PARAM_INT); $stmtInsertFunction->bindParam(':crap', $functionCrap); $stmtInsertFunction->bindParam(':npath', $functionNpath, PDO::PARAM_INT); $stmtInsertFunction->execute(); } foreach ($fileMetrics->getClasses() as $classMetrics) { $className = $classMetrics->getClass()->getName(); $stmtSelectClassId->bindParam(':className', $className, PDO::PARAM_STR); $stmtSelectClassId->bindParam(':revision', $revision, PDO::PARAM_INT); $stmtSelectClassId->execute(); $classId = (int) $stmtSelectClassId->fetchColumn(); $stmtSelectClassId->closeCursor(); $classCoverage = $classMetrics->getCoverage(); $classLoc = $classMetrics->getLoc(); $classLocExecutable = $classMetrics->getLocExecutable(); $classLocExecuted = $classMetrics->getLocExecuted(); $classAif = $classMetrics->getAIF(); $classAhf = $classMetrics->getAHF(); $classCis = $classMetrics->getCIS(); $classCsz = $classMetrics->getCSZ(); $classDit = $classMetrics->getDIT(); $classImpl = $classMetrics->getIMPL(); $classMif = $classMetrics->getMIF(); $classMhf = $classMetrics->getMHF(); $classNoc = $classMetrics->getNOC(); $classPf = $classMetrics->getPF(); $classVars = $classMetrics->getVARS(); $classVarsnp = $classMetrics->getVARSnp(); $classVarsi = $classMetrics->getVARSi(); $classWmc = $classMetrics->getWMC(); $classWmcnp = $classMetrics->getWMCnp(); $classWmci = $classMetrics->getWMCi(); $stmtInsertClass->bindParam(':runId', $runId, PDO::PARAM_INT); $stmtInsertClass->bindParam(':classId', $classId, PDO::PARAM_INT); $stmtInsertClass->bindParam(':coverage', $classCoverage); $stmtInsertClass->bindParam(':loc', $classLoc, PDO::PARAM_INT); $stmtInsertClass->bindParam(':locExecutable', $classLocExecutable, PDO::PARAM_INT); $stmtInsertClass->bindParam(':locExecuted', $classLocExecuted, PDO::PARAM_INT); $stmtInsertClass->bindParam(':aif', $classAif); $stmtInsertClass->bindParam(':ahf', $classAhf); $stmtInsertClass->bindParam(':cis', $classCis, PDO::PARAM_INT); $stmtInsertClass->bindParam(':csz', $classCsz, PDO::PARAM_INT); $stmtInsertClass->bindParam(':dit', $classDit, PDO::PARAM_INT); $stmtInsertClass->bindParam(':impl', $classImpl, PDO::PARAM_INT); $stmtInsertClass->bindParam(':mif', $classMif); $stmtInsertClass->bindParam(':mhf', $classMhf); $stmtInsertClass->bindParam(':noc', $classNoc, PDO::PARAM_INT); $stmtInsertClass->bindParam(':pf', $classPf); $stmtInsertClass->bindParam(':vars', $classVars, PDO::PARAM_INT); $stmtInsertClass->bindParam(':varsnp', $classVarsnp, PDO::PARAM_INT); $stmtInsertClass->bindParam(':varsi', $classVarsi, PDO::PARAM_INT); $stmtInsertClass->bindParam(':wmc', $classWmc, PDO::PARAM_INT); $stmtInsertClass->bindParam(':wmcnp', $classWmcnp, PDO::PARAM_INT); $stmtInsertClass->bindParam(':wmci', $classWmci, PDO::PARAM_INT); $stmtInsertClass->execute(); foreach ($classMetrics->getMethods() as $methodMetrics) { $methodName = $methodMetrics->getMethod()->getName(); $stmtSelectMethodId->bindParam(':className', $className, PDO::PARAM_STR); $stmtSelectMethodId->bindParam(':methodName', $methodName, PDO::PARAM_STR); $stmtSelectMethodId->bindParam(':revision', $revision, PDO::PARAM_INT); $stmtSelectMethodId->execute(); $methodId = (int) $stmtSelectMethodId->fetchColumn(); $stmtSelectMethodId->closeCursor(); $methodCoverage = $methodMetrics->getCoverage(); $methodLoc = $methodMetrics->getLoc(); $methodLocExecutable = $methodMetrics->getLocExecutable(); $methodLocExecuted = $methodMetrics->getLocExecuted(); $methodCcn = $methodMetrics->getCCN(); $methodCrap = $methodMetrics->getCrapIndex(); $methodNpath = $methodMetrics->getNPath(); $stmtInsertMethod->bindParam(':runId', $runId, PDO::PARAM_INT); $stmtInsertMethod->bindParam(':methodId', $methodId, PDO::PARAM_INT); $stmtInsertMethod->bindParam(':coverage', $methodCoverage); $stmtInsertMethod->bindParam(':loc', $methodLoc, PDO::PARAM_INT); $stmtInsertMethod->bindParam(':locExecutable', $methodLocExecutable, PDO::PARAM_INT); $stmtInsertMethod->bindParam(':locExecuted', $methodLocExecuted, PDO::PARAM_INT); $stmtInsertMethod->bindParam(':ccn', $methodCcn, PDO::PARAM_INT); $stmtInsertMethod->bindParam(':crap', $methodCrap); $stmtInsertMethod->bindParam(':npath', $methodNpath, PDO::PARAM_INT); $stmtInsertMethod->execute(); } } unset($stmtSelectFunctionId); unset($stmtInsertFunction); unset($stmtSelectClassId); unset($stmtInsertClass); unset($stmtSelectMethodId); unset($stmtInsertMethod); $stmt = $this->dbh->prepare('SELECT code_line_id, code_line_covered FROM code_line WHERE code_file_id = :fileId AND code_line_number = :lineNumber;'); $stmt2 = $this->dbh->prepare('UPDATE code_line SET code_line_covered = :lineCovered WHERE code_line_id = :lineId;'); $stmt3 = $this->dbh->prepare('INSERT INTO code_coverage (test_id, code_line_id) VALUES(:testId, :lineId);'); for ($lineNumber = 1; $lineNumber <= $fileLoc; $lineNumber++) { $coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests($codeCoverage, $fileName, $lineNumber); if (is_array($coveringTests)) { $stmt->bindParam(':fileId', $fileId, PDO::PARAM_INT); $stmt->bindParam(':lineNumber', $lineNumber, PDO::PARAM_INT); $stmt->execute(); $codeLineId = (int) $stmt->fetchColumn(0); $oldCoverageFlag = (int) $stmt->fetchColumn(1); $newCoverageFlag = isset($summary[$fileName][$lineNumber]) ? 1 : 0; if ($oldCoverageFlag == 0 && $newCoverageFlag != 0 || $oldCoverageFlag < 0 && $newCoverageFlag > 0) { $stmt2->bindParam(':lineCovered', $newCoverageFlag, PDO::PARAM_INT); $stmt2->bindParam(':lineId', $codeLineId, PDO::PARAM_INT); $stmt2->execute(); } foreach ($coveringTests as $test) { $stmt3->bindParam(':testId', $test->__db_id, PDO::PARAM_INT); $stmt3->bindParam(':lineId', $codeLineId, PDO::PARAM_INT); $stmt3->execute(); } } } } unset($stmt); unset($stmt2); unset($stmt3); $stmt = $this->dbh->prepare('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 = :className AND code_method.code_method_name = :methodName;'); $stmt2 = $this->dbh->prepare('UPDATE test SET code_method_id = :methodId WHERE test_id = :testId;'); foreach ($result->topTestSuite() as $test) { if ($test instanceof PHPUnit_Framework_TestCase) { $className = get_class($test); $methodName = $test->getName(); $stmt->bindParam(':className', $className, PDO::PARAM_STR); $stmt->bindParam(':methodName', $methodName, PDO::PARAM_STR); $stmt->execute(); $methodId = (int) $stmt->fetchColumn(); $stmt->closeCursor(); $stmt2->bindParam(':methodId', $methodId, PDO::PARAM_INT); $stmt2->bindParam(':testId', $test->__db_id, PDO::PARAM_INT); $stmt2->execute(); } } unset($stmt); unset($stmt2); $stmt = $this->dbh->prepare('INSERT INTO metrics_project (run_id, metrics_project_cls, metrics_project_clsa, metrics_project_clsc, metrics_project_roots, metrics_project_leafs, metrics_project_interfs, metrics_project_maxdit) VALUES(:runId, :cls, :clsa, :clsc, :roots, :leafs, :interfs, :maxdit);'); $cls = $projectMetrics->getCLS(); $clsa = $projectMetrics->getCLSa(); $clsc = $projectMetrics->getCLSc(); $interfs = $projectMetrics->getInterfs(); $roots = $projectMetrics->getRoots(); $leafs = $projectMetrics->getLeafs(); $maxDit = $projectMetrics->getMaxDit(); $stmt->bindParam(':runId', $runId, PDO::PARAM_INT); $stmt->bindParam(':cls', $cls, PDO::PARAM_INT); $stmt->bindParam(':clsa', $clsa, PDO::PARAM_INT); $stmt->bindParam(':clsc', $clsc, PDO::PARAM_INT); $stmt->bindParam(':roots', $roots, PDO::PARAM_INT); $stmt->bindParam(':leafs', $leafs, PDO::PARAM_INT); $stmt->bindParam(':interfs', $interfs, PDO::PARAM_INT); $stmt->bindParam(':maxdit', $maxDit, PDO::PARAM_INT); $stmt->execute(); unset($stmt); $stmt = $this->dbh->prepare('UPDATE code_class SET code_class_parent_id = :parentClassId WHERE code_class_id = :classId;'); $stmt2 = $this->dbh->prepare('SELECT code_class.code_class_id as code_class_id FROM code_class, code_file WHERE code_class.code_file_id = code_file.code_file_id AND code_file.revision = :revision AND code_class.code_class_name = :parentClassName;'); foreach ($storedClasses as $className => $classId) { $class = new ReflectionClass($className); $parentClass = $class->getParentClass(); if ($parentClass !== FALSE) { $parentClassName = $parentClass->getName(); $parentClassId = 0; if (isset($storedClasses[$parentClassName])) { $parentClassId = $storedClasses[$parentClassName]; } else { $stmt2->bindParam(':parentClassName', $parentClassName, PDO::PARAM_STR); $stmt2->bindParam(':revision', $revision, PDO::PARAM_INT); $stmt2->execute(); $parentClassId = (int) $stmt2->fetchColumn(); $stmt2->closeCursor(); } if ($parentClassId > 0) { $stmt->bindParam(':classId', $classId, PDO::PARAM_INT); $stmt->bindParam(':parentClassId', $parentClassId, PDO::PARAM_INT); $stmt->execute(); } } } unset($stmt); unset($stmt2); $this->dbh->commit(); }
/** * Clears the cached summary information. * * @since Method available since Release 3.3.0 */ public static function clearSummary() { self::$summary = array(); }
/** * Calculates the Code Coverage for the class. * * @param array $codeCoverage */ protected function calculateCodeCoverage(&$codeCoverage) { $statistics = PHPUnit_Util_CodeCoverage::getStatistics($codeCoverage, $this->filename, 1, count($this->lines)); $this->coverage = $statistics['coverage']; $this->loc = $statistics['loc']; $this->locExecutable = $statistics['locExecutable']; $this->locExecuted = $statistics['locExecuted']; }
/** * @param PHPUnit_Framework_TestResult $result */ public function process(PHPUnit_Framework_TestResult $result) { $sutData = $result->getCodeCoverageInformation(); $sutFiles = PHPUnit_Util_CodeCoverage::getSummary($sutData, TRUE); $allData = $result->getCodeCoverageInformation(FALSE); $allFiles = PHPUnit_Util_CodeCoverage::getSummary($allData, TRUE); $testFiles = array_diff(array_keys($allFiles), array_keys($sutFiles)); foreach (array_keys($allFiles) as $key) { if (!@in_array($key, $testFiles)) { unset($allFiles[$key]); } } $allCommonPath = PHPUnit_Util_Filesystem::reducePaths($allFiles); $sutCommonPath = PHPUnit_Util_Filesystem::reducePaths($sutFiles); $testFiles = $allFiles; unset($allData); unset($allFiles); unset($sutData); $testToCoveredLinesMap = array(); $time = time(); foreach ($sutFiles as $filename => $data) { $fullPath = $sutCommonPath . DIRECTORY_SEPARATOR . $filename; if (file_exists($fullPath)) { $fullPath = realpath($fullPath); $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $coveredFile = $document->createElement('coveredFile'); $coveredFile->setAttribute('fullPath', $fullPath); $coveredFile->setAttribute('shortenedPath', $filename); $coveredFile->setAttribute('generated', $time); $coveredFile->setAttribute('phpunit', PHPUnit_Runner_Version::id()); $document->appendChild($coveredFile); $lines = file($fullPath, FILE_IGNORE_NEW_LINES); $lineNum = 1; foreach ($lines as $line) { if (isset($data[$lineNum])) { if (is_array($data[$lineNum])) { $count = count($data[$lineNum]); } else { $count = $data[$lineNum]; } } else { $count = -3; } $xmlLine = $coveredFile->appendChild($document->createElement('line')); $xmlLine->setAttribute('lineNumber', $lineNum); $xmlLine->setAttribute('executed', $count); $xmlLine->appendChild($document->createElement('body', htmlspecialchars(PHPUnit_Util_XML::convertToUtf8($line), ENT_COMPAT, 'UTF-8'))); if (isset($data[$lineNum]) && is_array($data[$lineNum])) { $xmlTests = $document->createElement('tests'); $xmlLine->appendChild($xmlTests); foreach ($data[$lineNum] as $test) { $xmlTest = $xmlTests->appendChild($document->createElement('test')); if ($test instanceof PHPUnit_Framework_TestCase) { $xmlTest->setAttribute('name', $test->getName()); $xmlTest->setAttribute('status', $test->getStatus()); if ($test->hasFailed()) { $xmlTest->appendChild($document->createElement('message', htmlspecialchars(PHPUnit_Util_XML::convertToUtf8($test->getStatusMessage()), ENT_COMPAT, 'UTF-8'))); } $class = new ReflectionClass($test); $testFullPath = $class->getFileName(); $testShortenedPath = str_replace($allCommonPath, '', $testFullPath); $methodName = $test->getName(FALSE); if ($class->hasMethod($methodName)) { $method = $class->getMethod($methodName); $startLine = $method->getStartLine(); $xmlTest->setAttribute('class', $class->getName()); $xmlTest->setAttribute('fullPath', $testFullPath); $xmlTest->setAttribute('shortenedPath', $testShortenedPath); $xmlTest->setAttribute('line', $startLine); if (!isset($testToCoveredLinesMap[$testFullPath][$startLine])) { $testToCoveredLinesMap[$testFullPath][$startLine] = array(); } if (!isset($testToCoveredLinesMap[$testFullPath][$startLine][$fullPath])) { $testToCoveredLinesMap[$testFullPath][$startLine][$fullPath] = array('coveredLines' => array($lineNum), 'shortenedPath' => $filename); } else { $testToCoveredLinesMap[$testFullPath][$startLine][$fullPath]['coveredLines'][] = $lineNum; } } } } } $lineNum++; } $document->save(sprintf('%s%s.xml', $this->directory, PHPUnit_Util_Filesystem::getSafeFilename(str_replace(DIRECTORY_SEPARATOR, '_', $filename)))); } } foreach ($testFiles as $filename => $data) { $fullPath = $allCommonPath . DIRECTORY_SEPARATOR . $filename; if (file_exists($fullPath)) { $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $testFile = $document->createElement('testFile'); $testFile->setAttribute('fullPath', $fullPath); $testFile->setAttribute('shortenedPath', $filename); $testFile->setAttribute('generated', $time); $testFile->setAttribute('phpunit', PHPUnit_Runner_Version::id()); $document->appendChild($testFile); $lines = file($fullPath, FILE_IGNORE_NEW_LINES); $lineNum = 1; foreach ($lines as $line) { $xmlLine = $testFile->appendChild($document->createElement('line')); $xmlLine->setAttribute('lineNumber', $lineNum); $xmlLine->appendChild($document->createElement('body', htmlspecialchars(PHPUnit_Util_XML::convertToUtf8($line), ENT_COMPAT, 'UTF-8'))); if (isset($testToCoveredLinesMap[$fullPath][$lineNum])) { $xmlCoveredFiles = $xmlLine->appendChild($document->createElement('coveredFiles')); foreach ($testToCoveredLinesMap[$fullPath][$lineNum] as $coveredFileFullPath => $coveredFileData) { $xmlCoveredFile = $xmlCoveredFiles->appendChild($document->createElement('coveredFile')); $xmlCoveredFile->setAttribute('fullPath', $fullPath); $xmlCoveredFile->setAttribute('shortenedPath', $coveredFileData['shortenedPath']); foreach ($coveredFileData['coveredLines'] as $coveredLineNum) { $xmlCoveredLine = $xmlCoveredFile->appendChild($document->createElement('coveredLine', $coveredLineNum)); } } } $lineNum++; } $document->save(sprintf('%s%s.xml', $this->directory, PHPUnit_Util_Filesystem::getSafeFilename(str_replace(DIRECTORY_SEPARATOR, '_', $filename)))); } } }
/** * Run a test */ public function run(PHPUnit_Framework_TestSuite $suite) { $res = new PHPUnit_Framework_TestResult(); if ($this->codecoverage) { $res->collectCodeCoverageInformation(TRUE); } $res->addListener($this); foreach ($this->formatters as $formatter) { $res->addListener($formatter); } /* Set PHPUnit error handler */ if ($this->useCustomErrorHandler) { $oldErrorHandler = set_error_handler(array('PHPUnitTestRunner', 'handleError'), E_ALL | E_STRICT); } $suite->run($res, false, $this->groups, $this->excludeGroups); foreach ($this->formatters as $formatter) { $formatter->processResult($res); } /* Restore Phing error handler */ if ($this->useCustomErrorHandler) { restore_error_handler(); } if ($this->codecoverage) { $coverageInformation = $res->getCodeCoverageInformation(); PHPUnit_Util_CodeCoverage::clearSummary(); $summary = PHPUnit_Util_CodeCoverage::getSummary($coverageInformation); CoverageMerger::merge($this->project, $summary); } if ($res->errorCount() != 0) { $this->retCode = self::ERRORS; } else { if ($res->failureCount() != 0) { $this->retCode = self::FAILURES; } else { if ($res->notImplementedCount() != 0) { $this->retCode = self::INCOMPLETES; } else { if ($res->skippedCount() != 0) { $this->retCode = self::SKIPPED; } } } } }
/** * @param PHPUnit_Framework_TestResult $result */ public function process(PHPUnit_Framework_TestResult $result) { $codeCoverage = $result->getCodeCoverageInformation(); $summary = PHPUnit_Util_CodeCoverage::getSummary($codeCoverage); $files = array_keys($summary); $projectMetrics = new PHPUnit_Util_Metrics_Project($files, $summary); $document = new DOMDocument('1.0', 'UTF-8'); $document->formatOutput = TRUE; $metrics = $document->createElement('metrics'); $metrics->setAttribute('files', count($projectMetrics->getFiles())); $metrics->setAttribute('functions', count($projectMetrics->getFunctions())); $metrics->setAttribute('cls', $projectMetrics->getCLS()); $metrics->setAttribute('clsa', $projectMetrics->getCLSa()); $metrics->setAttribute('clsc', $projectMetrics->getCLSc()); $metrics->setAttribute('roots', $projectMetrics->getRoots()); $metrics->setAttribute('leafs', $projectMetrics->getLeafs()); $metrics->setAttribute('interfs', $projectMetrics->getInterfs()); $metrics->setAttribute('maxdit', $projectMetrics->getMaxDit()); $document->appendChild($metrics); foreach ($projectMetrics->getFiles() as $fileName => $fileMetrics) { $xmlFile = $metrics->appendChild($document->createElement('file')); $xmlFile->setAttribute('name', $fileName); $xmlFile->setAttribute('classes', count($fileMetrics->getClasses())); $xmlFile->setAttribute('functions', count($fileMetrics->getFunctions())); $xmlFile->setAttribute('loc', $fileMetrics->getLoc()); $xmlFile->setAttribute('cloc', $fileMetrics->getCloc()); $xmlFile->setAttribute('ncloc', $fileMetrics->getNcloc()); $xmlFile->setAttribute('locExecutable', $fileMetrics->getLocExecutable()); $xmlFile->setAttribute('locExecuted', $fileMetrics->getLocExecuted()); $xmlFile->setAttribute('coverage', sprintf('%F', $fileMetrics->getCoverage())); foreach ($fileMetrics->getClasses() as $className => $classMetrics) { if (!$classMetrics->getClass()->implementsInterface('PHPUnit_Framework_Test')) { $xmlClass = $document->createElement('class'); $xmlClass->setAttribute('name', $classMetrics->getClass()->getName()); $xmlClass->setAttribute('loc', $classMetrics->getLoc()); $xmlClass->setAttribute('locExecutable', $classMetrics->getLocExecutable()); $xmlClass->setAttribute('locExecuted', $classMetrics->getLocExecuted()); $xmlClass->setAttribute('aif', sprintf('%F', $classMetrics->getAIF())); $xmlClass->setAttribute('ahf', sprintf('%F', $classMetrics->getAHF())); $xmlClass->setAttribute('ca', $classMetrics->getCa()); $xmlClass->setAttribute('ce', $classMetrics->getCe()); $xmlClass->setAttribute('csz', $classMetrics->getCSZ()); $xmlClass->setAttribute('cis', $classMetrics->getCIS()); $xmlClass->setAttribute('coverage', sprintf('%F', $classMetrics->getCoverage())); $xmlClass->setAttribute('dit', $classMetrics->getDIT()); $xmlClass->setAttribute('i', sprintf('%F', $classMetrics->getI())); $xmlClass->setAttribute('impl', $classMetrics->getIMPL()); $xmlClass->setAttribute('mif', sprintf('%F', $classMetrics->getMIF())); $xmlClass->setAttribute('mhf', sprintf('%F', $classMetrics->getMHF())); $xmlClass->setAttribute('noc', $classMetrics->getNOC()); $xmlClass->setAttribute('pf', sprintf('%F', $classMetrics->getPF())); $xmlClass->setAttribute('vars', $classMetrics->getVARS()); $xmlClass->setAttribute('varsnp', $classMetrics->getVARSnp()); $xmlClass->setAttribute('varsi', $classMetrics->getVARSi()); $xmlClass->setAttribute('wmc', $classMetrics->getWMC()); $xmlClass->setAttribute('wmcnp', $classMetrics->getWMCnp()); $xmlClass->setAttribute('wmci', $classMetrics->getWMCi()); foreach ($classMetrics->getMethods() as $methodName => $methodMetrics) { $xmlMethod = $xmlClass->appendChild($document->createElement('method')); $this->processFunctionOrMethod($methodMetrics, $xmlMethod); } $xmlFile->appendChild($xmlClass); } } foreach ($fileMetrics->getFunctions() as $functionName => $functionMetrics) { $xmlFunction = $xmlFile->appendChild($document->createElement('function')); $this->processFunctionOrMethod($functionMetrics, $xmlFunction); } } $this->write($document->saveXML()); $this->flush(); }
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * @version SVN: $Id: phpunit_coverage.php 4568 2009-01-27 12:40:55Z sb $ * @link http://www.phpunit.de/ * @since File available since Release 3.2.10 */ require_once 'PHPUnit/Util/CodeCoverage.php'; require_once 'PHPUnit/Util/FilterIterator.php'; if (isset($_GET['PHPUNIT_SELENIUM_TEST_ID'])) { $files = new PHPUnit_Util_FilterIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(getcwd())), $_GET['PHPUNIT_SELENIUM_TEST_ID']); $coverage = array(); foreach ($files as $file) { $filename = $file->getPathName(); $data = unserialize(file_get_contents($filename)); @unlink($filename); unset($filename); foreach ($data as $filename => $lines) { if (PHPUnit_Util_CodeCoverage::isFile($filename)) { if (!isset($coverage[$filename])) { $coverage[$filename] = array('md5' => md5_file($filename), 'coverage' => $lines); } else { foreach ($lines as $line => $flag) { if (!isset($coverage[$filename]['coverage'][$line]) || $flag > $coverage[$filename]['coverage'][$line]) { $coverage[$filename]['coverage'][$line] = $flag; } } } } } } print serialize($coverage); }
static function merge($project, $codeCoverageInformation) { $database = new PhingFile($project->getProperty('coverage.database')); $props = new Properties(); $props->load($database); $coverageTotal = $codeCoverageInformation; foreach ($coverageTotal as $coverage) { foreach ($coverage as $filename => $coverageFile) { $filename = strtolower($filename); if ($props->getProperty($filename) != null) { $file = unserialize($props->getProperty($filename)); $left = $file['coverage']; $right = $coverageFile; if (!is_array($right)) { $right = array_shift(PHPUnit_Util_CodeCoverage::bitStringToCodeCoverage(array($right), 1)); } $coverageMerged = CoverageMerger::mergeCodeCoverage($left, $right); foreach ($coverageMerged as $key => $value) { if ($value == -2) { unset($coverageMerged[$key]); } } $file['coverage'] = $coverageMerged; $props->setProperty($filename, serialize($file)); } } } $props->store($database); }