From 8002119b231293d91edefbd21431e45b113f812d Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Sun, 21 Jan 2024 21:12:46 +1100 Subject: [PATCH 01/15] First pass at toJson method --- src/Data/ProcessedCodeCoverageDataMapper.php | 30 +++++++++++++++++++ .../ProcessedCodeCoverageDataMapperTest.php | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/Data/ProcessedCodeCoverageDataMapper.php create mode 100644 tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php new file mode 100644 index 000000000..1287130dc --- /dev/null +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace SebastianBergmann\CodeCoverage\Data; + +/** + * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage + * + * @psalm-import-type XdebugFunctionCoverageType from \SebastianBergmann\CodeCoverage\Driver\XdebugDriver + * + * @psalm-type TestIdType = string + */ +final class ProcessedCodeCoverageDataMapper +{ + public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): string + { + $arrayMapping = [ + 'line_coverage' => $processedCodeCoverageData->lineCoverage(), + ]; + + return json_encode($arrayMapping); + } +} + diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php new file mode 100644 index 000000000..042efc0c5 --- /dev/null +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace SebastianBergmann\CodeCoverage\Data; + +use SebastianBergmann\CodeCoverage\TestCase; + +final class ProcessedCodeCoverageDataMapperTest extends TestCase +{ + public function testToJson(): void + { + $coverage = $this->getLineCoverageForBankAccountForFirstTwoTests()->getData(); + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $json = $dataMapper->toJson($coverage); + + $decodedJson = json_decode($json, true); + + $this->assertEquals( + $coverage->lineCoverage(), + $decodedJson['line_coverage'], + ); + } +} + From 45f8ddd14fe5e375313e93f6c4b4d44b5853d557 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Sun, 21 Jan 2024 21:15:31 +1100 Subject: [PATCH 02/15] First pass at fromJson method --- src/Data/ProcessedCodeCoverageDataMapper.php | 11 +++++++++++ .../ProcessedCodeCoverageDataMapperTest.php | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index 1287130dc..8d90da6eb 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -26,5 +26,16 @@ public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): st return json_encode($arrayMapping); } + + public function fromJson(string $json): ProcessedCodeCoverageData + { + $unserializedData = json_decode($json, true); + + $processedCodeCoverageData = new ProcessedCodeCoverageData(); + + $processedCodeCoverageData->setLineCoverage($unserializedData['line_coverage']); + + return $processedCodeCoverageData; + } } diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 042efc0c5..df3974faa 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -26,5 +26,24 @@ public function testToJson(): void $decodedJson['line_coverage'], ); } + + public function testFromJson(): void + { + // Doing it this way while the JSON format is being developed, though + // I expect we'd have a fixture file in the future + $coverage = $this->getLineCoverageForBankAccountForFirstTwoTests()->getData(); + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $json = $dataMapper->toJson($coverage); + + // Instantiate a new data mapper to ensure we have no persisted state + // from the setup step + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $unserializedCoverage = $dataMapper->fromJson($json); + + $this->assertEquals( + $coverage->lineCoverage(), + $unserializedCoverage->lineCoverage(), + ); + } } From 1aafb1be0a3ed59eb8e7559356de84e4384a9de5 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Sun, 21 Jan 2024 21:17:02 +1100 Subject: [PATCH 03/15] Extract string key to constant --- src/Data/ProcessedCodeCoverageDataMapper.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index 8d90da6eb..dbdbca8d3 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -18,10 +18,12 @@ */ final class ProcessedCodeCoverageDataMapper { + const KEY_LINE_COVERAGE = 'line_coverage'; + public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): string { $arrayMapping = [ - 'line_coverage' => $processedCodeCoverageData->lineCoverage(), + self::KEY_LINE_COVERAGE => $processedCodeCoverageData->lineCoverage(), ]; return json_encode($arrayMapping); @@ -33,7 +35,7 @@ public function fromJson(string $json): ProcessedCodeCoverageData $processedCodeCoverageData = new ProcessedCodeCoverageData(); - $processedCodeCoverageData->setLineCoverage($unserializedData['line_coverage']); + $processedCodeCoverageData->setLineCoverage($unserializedData[self::KEY_LINE_COVERAGE]); return $processedCodeCoverageData; } From 408ec716f9542041552dcf77aff274ed2459a701 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Mon, 22 Jan 2024 20:57:21 +1100 Subject: [PATCH 04/15] Use full coverage test setup methods --- tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index df3974faa..efc053b7b 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -15,7 +15,7 @@ final class ProcessedCodeCoverageDataMapperTest extends TestCase { public function testToJson(): void { - $coverage = $this->getLineCoverageForBankAccountForFirstTwoTests()->getData(); + $coverage = $this->getLineCoverageForBankAccount()->getData(); $dataMapper = new ProcessedCodeCoverageDataMapper(); $json = $dataMapper->toJson($coverage); @@ -31,7 +31,7 @@ public function testFromJson(): void { // Doing it this way while the JSON format is being developed, though // I expect we'd have a fixture file in the future - $coverage = $this->getLineCoverageForBankAccountForFirstTwoTests()->getData(); + $coverage = $this->getLineCoverageForBankAccount()->getData(); $dataMapper = new ProcessedCodeCoverageDataMapper(); $json = $dataMapper->toJson($coverage); From 00c1de240970d82ee36a635499fb10bd1de758dc Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Mon, 22 Jan 2024 21:01:25 +1100 Subject: [PATCH 05/15] Use constant for key --- tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index efc053b7b..b089f3f95 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -23,7 +23,7 @@ public function testToJson(): void $this->assertEquals( $coverage->lineCoverage(), - $decodedJson['line_coverage'], + $decodedJson[ProcessedCodeCoverageDataMapper::KEY_LINE_COVERAGE], ); } From e6e69bf4f605a7f9fa5bd830b68811103c053974 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Mon, 22 Jan 2024 21:01:52 +1100 Subject: [PATCH 06/15] Use camel case for keys --- src/Data/ProcessedCodeCoverageDataMapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index dbdbca8d3..459b13bfe 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -18,7 +18,7 @@ */ final class ProcessedCodeCoverageDataMapper { - const KEY_LINE_COVERAGE = 'line_coverage'; + const KEY_LINE_COVERAGE = 'lineCoverage'; public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): string { From 1bba5250dd6a9f17954fdd54d4bfe4c3aa1ff2e5 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Mon, 22 Jan 2024 21:07:44 +1100 Subject: [PATCH 07/15] Make test case names more specific --- tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index b089f3f95..9ed9f4800 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -13,7 +13,7 @@ final class ProcessedCodeCoverageDataMapperTest extends TestCase { - public function testToJson(): void + public function testToJsonCoverageForBankAccount(): void { $coverage = $this->getLineCoverageForBankAccount()->getData(); $dataMapper = new ProcessedCodeCoverageDataMapper(); @@ -27,7 +27,7 @@ public function testToJson(): void ); } - public function testFromJson(): void + public function testFromJsonCoverageForBankAccount(): void { // Doing it this way while the JSON format is being developed, though // I expect we'd have a fixture file in the future From 822dedc171e410a9757acad53fdde6cf186d2c75 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Mon, 22 Jan 2024 21:29:20 +1100 Subject: [PATCH 08/15] Include function coverage --- src/Data/ProcessedCodeCoverageDataMapper.php | 2 ++ .../tests/Data/ProcessedCodeCoverageDataMapperTest.php | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index 459b13bfe..8cef543f5 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -19,11 +19,13 @@ final class ProcessedCodeCoverageDataMapper { const KEY_LINE_COVERAGE = 'lineCoverage'; + const KEY_FUNCTION_COVERAGE = 'functionCoverage'; public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): string { $arrayMapping = [ self::KEY_LINE_COVERAGE => $processedCodeCoverageData->lineCoverage(), + self::KEY_FUNCTION_COVERAGE => $processedCodeCoverageData->functionCoverage(), ]; return json_encode($arrayMapping); diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 9ed9f4800..8a0b602e3 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -25,6 +25,11 @@ public function testToJsonCoverageForBankAccount(): void $coverage->lineCoverage(), $decodedJson[ProcessedCodeCoverageDataMapper::KEY_LINE_COVERAGE], ); + + $this->assertEquals( + $coverage->functionCoverage(), + $decodedJson[ProcessedCodeCoverageDataMapper::KEY_FUNCTION_COVERAGE], + ); } public function testFromJsonCoverageForBankAccount(): void @@ -44,6 +49,11 @@ public function testFromJsonCoverageForBankAccount(): void $coverage->lineCoverage(), $unserializedCoverage->lineCoverage(), ); + + $this->assertEquals( + $coverage->functionCoverage(), + $unserializedCoverage->functionCoverage(), + ); } } From 83112b5f7898f19a7c6a6ddd610dee039bfb5fb9 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Thu, 25 Jan 2024 20:38:10 +1100 Subject: [PATCH 09/15] Separate testing for path and function coverage - Ensure we are unserialising the function coverage --- src/Data/ProcessedCodeCoverageDataMapper.php | 1 + .../ProcessedCodeCoverageDataMapperTest.php | 45 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index 8cef543f5..521871d09 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -38,6 +38,7 @@ public function fromJson(string $json): ProcessedCodeCoverageData $processedCodeCoverageData = new ProcessedCodeCoverageData(); $processedCodeCoverageData->setLineCoverage($unserializedData[self::KEY_LINE_COVERAGE]); + $processedCodeCoverageData->setFunctionCoverage($unserializedData[self::KEY_FUNCTION_COVERAGE]); return $processedCodeCoverageData; } diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 8a0b602e3..56a2bf091 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -13,7 +13,7 @@ final class ProcessedCodeCoverageDataMapperTest extends TestCase { - public function testToJsonCoverageForBankAccount(): void + public function testToJsonLineCoverageForBankAccount(): void { $coverage = $this->getLineCoverageForBankAccount()->getData(); $dataMapper = new ProcessedCodeCoverageDataMapper(); @@ -32,6 +32,25 @@ public function testToJsonCoverageForBankAccount(): void ); } + public function testToJsonPathCoverageForBankAccount(): void + { + $coverage = $this->getPathCoverageForBankAccount()->getData(); + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $json = $dataMapper->toJson($coverage); + + $decodedJson = json_decode($json, true); + + $this->assertEquals( + $coverage->lineCoverage(), + $decodedJson[ProcessedCodeCoverageDataMapper::KEY_LINE_COVERAGE], + ); + + $this->assertEquals( + $coverage->functionCoverage(), + $decodedJson[ProcessedCodeCoverageDataMapper::KEY_FUNCTION_COVERAGE], + ); + } + public function testFromJsonCoverageForBankAccount(): void { // Doing it this way while the JSON format is being developed, though @@ -55,5 +74,29 @@ public function testFromJsonCoverageForBankAccount(): void $unserializedCoverage->functionCoverage(), ); } + + public function testFromJsonPathCoverageForBankAccount(): void + { + // Doing it this way while the JSON format is being developed, though + // I expect we'd have a fixture file in the future + $coverage = $this->getPathCoverageForBankAccount()->getData(); + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $json = $dataMapper->toJson($coverage); + + // Instantiate a new data mapper to ensure we have no persisted state + // from the setup step + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $unserializedCoverage = $dataMapper->fromJson($json); + + $this->assertEquals( + $coverage->lineCoverage(), + $unserializedCoverage->lineCoverage(), + ); + + $this->assertEquals( + $coverage->functionCoverage(), + $unserializedCoverage->functionCoverage(), + ); + } } From dfe7b551c94b83d043a7090b642f72a7a81ad28f Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Thu, 25 Jan 2024 21:01:54 +1100 Subject: [PATCH 10/15] Extract repeated setup steps to helper --- .../ProcessedCodeCoverageDataMapperTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 56a2bf091..98f9c1c70 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -16,10 +16,7 @@ final class ProcessedCodeCoverageDataMapperTest extends TestCase public function testToJsonLineCoverageForBankAccount(): void { $coverage = $this->getLineCoverageForBankAccount()->getData(); - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $json = $dataMapper->toJson($coverage); - - $decodedJson = json_decode($json, true); + $decodedJson = $this->getDecodedJsonForProcessedCodeCoverage($coverage); $this->assertEquals( $coverage->lineCoverage(), @@ -35,10 +32,7 @@ public function testToJsonLineCoverageForBankAccount(): void public function testToJsonPathCoverageForBankAccount(): void { $coverage = $this->getPathCoverageForBankAccount()->getData(); - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $json = $dataMapper->toJson($coverage); - - $decodedJson = json_decode($json, true); + $decodedJson = $this->getDecodedJsonForProcessedCodeCoverage($coverage); $this->assertEquals( $coverage->lineCoverage(), @@ -98,5 +92,13 @@ public function testFromJsonPathCoverageForBankAccount(): void $unserializedCoverage->functionCoverage(), ); } + + private function getDecodedJsonForProcessedCodeCoverage(ProcessedCodeCoverageData $processedCodeCoverage): array + { + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $json = $dataMapper->toJson($processedCodeCoverage); + + return json_decode($json, true); + } } From 4c867581ad31b97ea8e1aa31ff1b77c4fa08622a Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Thu, 25 Jan 2024 21:07:47 +1100 Subject: [PATCH 11/15] Extract repeated setup steps to private method --- .../ProcessedCodeCoverageDataMapperTest.php | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 98f9c1c70..56e7bb9a4 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -47,16 +47,8 @@ public function testToJsonPathCoverageForBankAccount(): void public function testFromJsonCoverageForBankAccount(): void { - // Doing it this way while the JSON format is being developed, though - // I expect we'd have a fixture file in the future - $coverage = $this->getLineCoverageForBankAccount()->getData(); - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $json = $dataMapper->toJson($coverage); - - // Instantiate a new data mapper to ensure we have no persisted state - // from the setup step - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $unserializedCoverage = $dataMapper->fromJson($json); + $coverage = $this->getPathCoverageForBankAccount()->getData(); + $unserializedCoverage = $this->serializeAndUnserializeToJson($coverage); $this->assertEquals( $coverage->lineCoverage(), @@ -71,16 +63,8 @@ public function testFromJsonCoverageForBankAccount(): void public function testFromJsonPathCoverageForBankAccount(): void { - // Doing it this way while the JSON format is being developed, though - // I expect we'd have a fixture file in the future $coverage = $this->getPathCoverageForBankAccount()->getData(); - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $json = $dataMapper->toJson($coverage); - - // Instantiate a new data mapper to ensure we have no persisted state - // from the setup step - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $unserializedCoverage = $dataMapper->fromJson($json); + $unserializedCoverage = $this->serializeAndUnserializeToJson($coverage); $this->assertEquals( $coverage->lineCoverage(), @@ -100,5 +84,21 @@ private function getDecodedJsonForProcessedCodeCoverage(ProcessedCodeCoverageDat return json_decode($json, true); } + + /** + * Doing it this way while the JSON format is being developed, though I expect we'd have a + * fixture file in the future + **/ + private function serializeAndUnserializeToJson(ProcessedCodeCoverageData $processedCodeCoverage): ProcessedCodeCoverageData + { + $dataMapper = new ProcessedCodeCoverageDataMapper(); + $json = $dataMapper->toJson($processedCodeCoverage); + + // Instantiate a new data mapper out of an abundance of caution to ensure we have no + // persisted state from the serializing instance. + $dataMapper = new ProcessedCodeCoverageDataMapper(); + + return $dataMapper->fromJson($json); + } } From 9b33999f903d5f66d30947cadce6ffc0e419a299 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Thu, 25 Jan 2024 21:19:51 +1100 Subject: [PATCH 12/15] Add a sanity check end-to-end test producing an XML file --- .../ProcessedCodeCoverageDataMapperTest.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 56e7bb9a4..2ee49c6f4 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -9,10 +9,31 @@ */ namespace SebastianBergmann\CodeCoverage\Data; +use SebastianBergmann\CodeCoverage\Report\Xml\Facade; use SebastianBergmann\CodeCoverage\TestCase; +use FilesystemIterator; final class ProcessedCodeCoverageDataMapperTest extends TestCase { + private static string $TEST_REPORT_PATH_SOURCE; + + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'XML'; + } + + protected function tearDown(): void + { + parent::tearDown(); + + foreach (new FilesystemIterator(self::$TEST_TMP_PATH) as $fileInfo) { + /* @var \SplFileInfo $fileInfo */ + unlink($fileInfo->getPathname()); + } + } + public function testToJsonLineCoverageForBankAccount(): void { $coverage = $this->getLineCoverageForBankAccount()->getData(); @@ -77,6 +98,25 @@ public function testFromJsonPathCoverageForBankAccount(): void ); } + /** + * I don't expect this test to survive in the PR, but I am trying to + * produce the final XML format via the JSON serialization to ensure + * that I have everything I need in the JSON format. + */ + public function testFromJsonLineCoverageForBankAccountToXml(): void + { + $coverage = $this->getLineCoverageForBankAccount(); + $unserializedCoverage = $this->serializeAndUnserializeToJson($coverage->getData()); + $coverage->setData($unserializedCoverage); + + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount'; + + $xml = new Facade('1.0.0'); + $xml->process($coverage, self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + private function getDecodedJsonForProcessedCodeCoverage(ProcessedCodeCoverageData $processedCodeCoverage): array { $dataMapper = new ProcessedCodeCoverageDataMapper(); @@ -100,5 +140,32 @@ private function serializeAndUnserializeToJson(ProcessedCodeCoverageData $proces return $dataMapper->fromJson($json); } + + private function assertFilesEquals(string $expectedFilesPath, string $actualFilesPath): void + { + $expectedFilesIterator = new FilesystemIterator($expectedFilesPath); + $actualFilesIterator = new FilesystemIterator($actualFilesPath); + + $this->assertEquals( + iterator_count($expectedFilesIterator), + iterator_count($actualFilesIterator), + 'Generated files and expected files not match', + ); + + foreach ($expectedFilesIterator as $fileInfo) { + /* @var \SplFileInfo $fileInfo */ + $filename = $fileInfo->getFilename(); + + $actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename; + + $this->assertFileExists($actualFile); + + $this->assertStringMatchesFormatFile( + $fileInfo->getPathname(), + file_get_contents($actualFile), + "{$filename} not match", + ); + } + } } From 6b09e8fc5a9ebe5349a85502e5b5f84d639b1d39 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Thu, 25 Jan 2024 21:27:26 +1100 Subject: [PATCH 13/15] Add missing psalm annotiation --- src/Data/ProcessedCodeCoverageDataMapper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index 521871d09..75f8582ec 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -33,6 +33,7 @@ public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): st public function fromJson(string $json): ProcessedCodeCoverageData { + /** @var array> */ $unserializedData = json_decode($json, true); $processedCodeCoverageData = new ProcessedCodeCoverageData(); From 51e5faceda8a1d072c121ede956d7fed50c5a35b Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Thu, 25 Jan 2024 21:28:12 +1100 Subject: [PATCH 14/15] Code style fixes --- src/Data/ProcessedCodeCoverageDataMapper.php | 14 +++--- .../ProcessedCodeCoverageDataMapperTest.php | 43 ++++++++++--------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Data/ProcessedCodeCoverageDataMapper.php b/src/Data/ProcessedCodeCoverageDataMapper.php index 75f8582ec..24e927430 100644 --- a/src/Data/ProcessedCodeCoverageDataMapper.php +++ b/src/Data/ProcessedCodeCoverageDataMapper.php @@ -9,6 +9,9 @@ */ namespace SebastianBergmann\CodeCoverage\Data; +use function json_decode; +use function json_encode; + /** * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage * @@ -18,13 +21,13 @@ */ final class ProcessedCodeCoverageDataMapper { - const KEY_LINE_COVERAGE = 'lineCoverage'; - const KEY_FUNCTION_COVERAGE = 'functionCoverage'; + public const KEY_LINE_COVERAGE = 'lineCoverage'; + public const KEY_FUNCTION_COVERAGE = 'functionCoverage'; public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): string { $arrayMapping = [ - self::KEY_LINE_COVERAGE => $processedCodeCoverageData->lineCoverage(), + self::KEY_LINE_COVERAGE => $processedCodeCoverageData->lineCoverage(), self::KEY_FUNCTION_COVERAGE => $processedCodeCoverageData->functionCoverage(), ]; @@ -35,8 +38,8 @@ public function fromJson(string $json): ProcessedCodeCoverageData { /** @var array> */ $unserializedData = json_decode($json, true); - - $processedCodeCoverageData = new ProcessedCodeCoverageData(); + + $processedCodeCoverageData = new ProcessedCodeCoverageData; $processedCodeCoverageData->setLineCoverage($unserializedData[self::KEY_LINE_COVERAGE]); $processedCodeCoverageData->setFunctionCoverage($unserializedData[self::KEY_FUNCTION_COVERAGE]); @@ -44,4 +47,3 @@ public function fromJson(string $json): ProcessedCodeCoverageData return $processedCodeCoverageData; } } - diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 2ee49c6f4..9e8cf3342 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -9,9 +9,13 @@ */ namespace SebastianBergmann\CodeCoverage\Data; +use function file_get_contents; +use function iterator_count; +use function json_decode; +use function unlink; +use FilesystemIterator; use SebastianBergmann\CodeCoverage\Report\Xml\Facade; use SebastianBergmann\CodeCoverage\TestCase; -use FilesystemIterator; final class ProcessedCodeCoverageDataMapperTest extends TestCase { @@ -36,7 +40,7 @@ protected function tearDown(): void public function testToJsonLineCoverageForBankAccount(): void { - $coverage = $this->getLineCoverageForBankAccount()->getData(); + $coverage = $this->getLineCoverageForBankAccount()->getData(); $decodedJson = $this->getDecodedJsonForProcessedCodeCoverage($coverage); $this->assertEquals( @@ -52,7 +56,7 @@ public function testToJsonLineCoverageForBankAccount(): void public function testToJsonPathCoverageForBankAccount(): void { - $coverage = $this->getPathCoverageForBankAccount()->getData(); + $coverage = $this->getPathCoverageForBankAccount()->getData(); $decodedJson = $this->getDecodedJsonForProcessedCodeCoverage($coverage); $this->assertEquals( @@ -68,7 +72,7 @@ public function testToJsonPathCoverageForBankAccount(): void public function testFromJsonCoverageForBankAccount(): void { - $coverage = $this->getPathCoverageForBankAccount()->getData(); + $coverage = $this->getPathCoverageForBankAccount()->getData(); $unserializedCoverage = $this->serializeAndUnserializeToJson($coverage); $this->assertEquals( @@ -84,7 +88,7 @@ public function testFromJsonCoverageForBankAccount(): void public function testFromJsonPathCoverageForBankAccount(): void { - $coverage = $this->getPathCoverageForBankAccount()->getData(); + $coverage = $this->getPathCoverageForBankAccount()->getData(); $unserializedCoverage = $this->serializeAndUnserializeToJson($coverage); $this->assertEquals( @@ -99,13 +103,13 @@ public function testFromJsonPathCoverageForBankAccount(): void } /** - * I don't expect this test to survive in the PR, but I am trying to - * produce the final XML format via the JSON serialization to ensure - * that I have everything I need in the JSON format. - */ + * I don't expect this test to survive in the PR, but I am trying to + * produce the final XML format via the JSON serialization to ensure + * that I have everything I need in the JSON format. + */ public function testFromJsonLineCoverageForBankAccountToXml(): void { - $coverage = $this->getLineCoverageForBankAccount(); + $coverage = $this->getLineCoverageForBankAccount(); $unserializedCoverage = $this->serializeAndUnserializeToJson($coverage->getData()); $coverage->setData($unserializedCoverage); @@ -119,24 +123,24 @@ public function testFromJsonLineCoverageForBankAccountToXml(): void private function getDecodedJsonForProcessedCodeCoverage(ProcessedCodeCoverageData $processedCodeCoverage): array { - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $json = $dataMapper->toJson($processedCodeCoverage); + $dataMapper = new ProcessedCodeCoverageDataMapper; + $json = $dataMapper->toJson($processedCodeCoverage); return json_decode($json, true); } /** - * Doing it this way while the JSON format is being developed, though I expect we'd have a - * fixture file in the future - **/ + * Doing it this way while the JSON format is being developed, though I expect we'd have a + * fixture file in the future. + */ private function serializeAndUnserializeToJson(ProcessedCodeCoverageData $processedCodeCoverage): ProcessedCodeCoverageData { - $dataMapper = new ProcessedCodeCoverageDataMapper(); - $json = $dataMapper->toJson($processedCodeCoverage); + $dataMapper = new ProcessedCodeCoverageDataMapper; + $json = $dataMapper->toJson($processedCodeCoverage); - // Instantiate a new data mapper out of an abundance of caution to ensure we have no + // Instantiate a new data mapper out of an abundance of caution to ensure we have no // persisted state from the serializing instance. - $dataMapper = new ProcessedCodeCoverageDataMapper(); + $dataMapper = new ProcessedCodeCoverageDataMapper; return $dataMapper->fromJson($json); } @@ -168,4 +172,3 @@ private function assertFilesEquals(string $expectedFilesPath, string $actualFile } } } - From d551ba9cdb731cb993b36065bdaf318378bf67d0 Mon Sep 17 00:00:00 2001 From: Andrew Feeney Date: Mon, 29 Jan 2024 20:58:53 +1100 Subject: [PATCH 15/15] Skip file cleanup if the tmp directory has not been created - This issue presents when running the test suite for the first time (e.g. in CI) since there is no `tests/_files/tmp` directory created. - In the one test case which requires the cleanup of these files because it creates them it does not fail because the directory is created. - The other test cases don't need this tear down, but they fail when it tries to run as this directory has not yet been created. - In future we'll probably remove both the tear down and the test case that needs it as this is probably not an appropriately direct way to test this. --- tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php index 9e8cf3342..281318702 100644 --- a/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php +++ b/tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php @@ -32,7 +32,11 @@ protected function tearDown(): void { parent::tearDown(); - foreach (new FilesystemIterator(self::$TEST_TMP_PATH) as $fileInfo) { + if (!file_exists(self::$TEST_TMP_PATH)) { + return; + } + + foreach (new FilesystemIterator(self::$TEST_TMP_PATH) as $path => $fileInfo) { /* @var \SplFileInfo $fileInfo */ unlink($fileInfo->getPathname()); }