Skip to content

Commit 6dbfc20

Browse files
Slamdunksebastianbergmann
authored andcommitted
Avoid serialization of cache data in PHP report
1 parent 78ba572 commit 6dbfc20

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/CodeCoverage.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ public function clear(): void
150150
$this->cachedReport = null;
151151
}
152152

153+
/**
154+
* @internal
155+
*/
156+
public function clearCache(): void
157+
{
158+
$this->cachedReport = null;
159+
}
160+
153161
/**
154162
* Returns the filter object used.
155163
*/

src/Report/PHP.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class PHP
2121
{
2222
public function process(CodeCoverage $coverage, ?string $target = null): string
2323
{
24+
$coverage->clearCache();
25+
2426
$buffer = "<?php
2527
return \unserialize(<<<'END_OF_COVERAGE_SERIALIZATION'" . PHP_EOL . serialize($coverage) . PHP_EOL . 'END_OF_COVERAGE_SERIALIZATION' . PHP_EOL . ');';
2628

tests/tests/Report/PhpTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12+
use ReflectionProperty;
1213
use SebastianBergmann\CodeCoverage\TestCase;
1314

1415
final class PhpTest extends TestCase
@@ -43,4 +44,21 @@ public function testPHPSerialisationProducesValidCodeWhenOutputIncludesSingleQuo
4344

4445
$this->assertEquals($coverage, $unserialized);
4546
}
47+
48+
public function testCacheDataNeverGetSaved(): void
49+
{
50+
$coverage = $this->getLineCoverageForBankAccount();
51+
52+
// Warm up cache
53+
$coverage->getReport();
54+
55+
$refProperty = new ReflectionProperty($coverage, 'cachedReport');
56+
57+
$this->assertNotNull($refProperty->getValue($coverage));
58+
59+
/* @noinspection UnusedFunctionResultInspection */
60+
(new PHP)->process($coverage, self::$TEST_TMP_PATH . '/serialized.php');
61+
62+
$this->assertNull($refProperty->getValue($coverage));
63+
}
4664
}

0 commit comments

Comments
 (0)