Skip to content

Commit

Permalink
Avoid serialization of cache data in PHP report
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Sep 12, 2023
1 parent 78ba572 commit 6da91b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public function clear(): void
$this->cachedReport = null;
}

/**
* @internal
*/
public function clearCache(): void
{
$this->cachedReport = null;
}

/**
* Returns the filter object used.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Report/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class PHP
{
public function process(CodeCoverage $coverage, ?string $target = null): string
{
$coverage->clearCache();

$buffer = "<?php
return \unserialize(<<<'END_OF_COVERAGE_SERIALIZATION'" . PHP_EOL . serialize($coverage) . PHP_EOL . 'END_OF_COVERAGE_SERIALIZATION' . PHP_EOL . ');';

Expand Down
18 changes: 18 additions & 0 deletions tests/tests/Report/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\CodeCoverage\Report;

use ReflectionProperty;
use SebastianBergmann\CodeCoverage\TestCase;

final class PhpTest extends TestCase
Expand Down Expand Up @@ -43,4 +44,21 @@ public function testPHPSerialisationProducesValidCodeWhenOutputIncludesSingleQuo

$this->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));
}
}

0 comments on commit 6da91b6

Please sign in to comment.