From 6da91b67e756ef8f6966a507780656c7bb367387 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 12 Sep 2023 16:23:28 +0200 Subject: [PATCH 1/2] Avoid serialization of cache data in PHP report --- src/CodeCoverage.php | 8 ++++++++ src/Report/PHP.php | 2 ++ tests/tests/Report/PhpTest.php | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index bdf9c64c3..ba4f1f8c3 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -150,6 +150,14 @@ public function clear(): void $this->cachedReport = null; } + /** + * @internal + */ + public function clearCache(): void + { + $this->cachedReport = null; + } + /** * Returns the filter object used. */ diff --git a/src/Report/PHP.php b/src/Report/PHP.php index 2058fb39a..1f8186d04 100644 --- a/src/Report/PHP.php +++ b/src/Report/PHP.php @@ -21,6 +21,8 @@ final class PHP { public function process(CodeCoverage $coverage, ?string $target = null): string { + $coverage->clearCache(); + $buffer = "assertEquals($coverage, $unserialized); } + + public function testCacheDataNeverGetSaved(): void + { + $coverage = $this->getLineCoverageForBankAccount(); + + // Warm up cache + $coverage->getReport(); + + $refProperty = new ReflectionProperty($coverage, 'cachedReport'); + + $this->assertNotNull($refProperty->getValue($coverage)); + + /* @noinspection UnusedFunctionResultInspection */ + (new PHP)->process($coverage, self::$TEST_TMP_PATH . '/serialized.php'); + + $this->assertNull($refProperty->getValue($coverage)); + } } From 22c0d8251cccd9ebaf15ee67175222f2f19c54a9 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Tue, 12 Sep 2023 16:26:21 +0200 Subject: [PATCH 2/2] ReflectionProperty: PHP < 8.1 needs explicit accessibility permission --- tests/tests/Report/PhpTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/tests/Report/PhpTest.php b/tests/tests/Report/PhpTest.php index 733e739f4..75c11b4ba 100644 --- a/tests/tests/Report/PhpTest.php +++ b/tests/tests/Report/PhpTest.php @@ -53,6 +53,7 @@ public function testCacheDataNeverGetSaved(): void $coverage->getReport(); $refProperty = new ReflectionProperty($coverage, 'cachedReport'); + $refProperty->setAccessible(true); $this->assertNotNull($refProperty->getValue($coverage));