From 8b0341bab99e9c14300545d1286c1395390b36b4 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Sat, 16 May 2020 13:43:18 +0100 Subject: [PATCH] Store captured branch/path function data --- src/RawCodeCoverageData.php | 29 ++++++++++++++++++------- tests/tests/RawCodeCoverageDataTest.php | 13 ++++++++++- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/RawCodeCoverageData.php b/src/RawCodeCoverageData.php index bd2ab653d..1eea86777 100644 --- a/src/RawCodeCoverageData.php +++ b/src/RawCodeCoverageData.php @@ -21,30 +21,43 @@ final class RawCodeCoverageData */ private $lineCoverage = []; + /** + * @var array + * + * @see https://xdebug.org/docs/code_coverage for format + */ + private $functionCoverage = []; + public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self { - return new self($rawCoverage); + return new self($rawCoverage, []); } public static function fromXdebugWithPathCoverage(array $rawCoverage): self { - $lineCoverage = []; + $lineCoverage = $functionCoverage = []; foreach ($rawCoverage as $file => $fileCoverageData) { - $lineCoverage[$file] = $fileCoverageData['lines']; + if (isset($fileCoverageData['functions'])) { + $lineCoverage[$file] = $fileCoverageData['lines']; + $functionCoverage[$file] = $fileCoverageData['functions']; + } else { // not every file has functions, Xdebug outputs just line data for these + $lineCoverage[$file] = $fileCoverageData; + } } - return new self($lineCoverage); + return new self($lineCoverage, $functionCoverage); } - private function __construct(array $lineCoverage) + private function __construct(array $lineCoverage, array $functionCoverage) { - $this->lineCoverage = $lineCoverage; + $this->lineCoverage = $lineCoverage; + $this->functionCoverage = $functionCoverage; } public function clear(): void { - $this->lineCoverage = []; + $this->lineCoverage = $this->functionCoverage = []; } public function getLineCoverage(): array @@ -54,7 +67,7 @@ public function getLineCoverage(): array public function removeCoverageDataForFile(string $filename): void { - unset($this->lineCoverage[$filename]); + unset($this->lineCoverage[$filename], $this->functionCoverage[$filename]); } /** diff --git a/tests/tests/RawCodeCoverageDataTest.php b/tests/tests/RawCodeCoverageDataTest.php index 81c66628f..e4e318096 100644 --- a/tests/tests/RawCodeCoverageDataTest.php +++ b/tests/tests/RawCodeCoverageDataTest.php @@ -30,7 +30,8 @@ public function testLineDataFromStandardXDebugFormat(): void } /** - * In the path-coverage XDebug format, the line data exists inside a "lines" array key. + * In the path-coverage XDebug format, the line data exists inside a "lines" array key where the file has + * classes or functions. For files without them, the data is stored in the line-only format. */ public function testLineDataFromPathCoverageXDebugFormat(): void { @@ -45,6 +46,11 @@ public function testLineDataFromPathCoverageXDebugFormat(): void ], ], + '/some/path/justAScript.php' => [ + 18 => 1, + 19 => -2, + 113 => -1, + ], ]; $lineData = [ @@ -53,6 +59,11 @@ public function testLineDataFromPathCoverageXDebugFormat(): void 9 => -2, 13 => -1, ], + '/some/path/justAScript.php' => [ + 18 => 1, + 19 => -2, + 113 => -1, + ], ]; $dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);