Skip to content

Commit 3699d42

Browse files
dvdougsebastianbergmann
authored andcommitted
Change PHP report to use serialize, rather than var_export for better compatibility with data structure changes
1 parent 06bfa0c commit 3699d42

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/Report/PHP.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,10 @@ final class PHP
2222
*/
2323
public function process(CodeCoverage $coverage, ?string $target = null): string
2424
{
25-
$filter = $coverage->filter();
26-
2725
$buffer = \sprintf(
2826
'<?php
29-
$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
30-
$coverage->setData(%s);
31-
$coverage->setTests(%s);
32-
33-
$filter = $coverage->filter();
34-
$filter->setWhitelistedFiles(%s);
35-
36-
return $coverage;',
37-
\var_export($coverage->getData(true), true),
38-
\var_export($coverage->getTests(), true),
39-
\var_export($filter->getWhitelistedFiles(), true)
27+
return \unserialize(\'%s\');',
28+
\serialize($coverage)
4029
);
4130

4231
if ($target !== null) {

tests/tests/PHPTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Report;
11+
12+
use SebastianBergmann\CodeCoverage\TestCase;
13+
14+
class PHPTest extends TestCase
15+
{
16+
protected function tearDown(): void
17+
{
18+
parent::tearDown();
19+
20+
$this->removeTemporaryFiles();
21+
}
22+
23+
public function testPHPSerialisationProducesValidCode(): void
24+
{
25+
$coverage = $this->getCoverageForBankAccount();
26+
27+
(new PHP())->process($coverage, self::$TEST_TMP_PATH . '/serialised.php');
28+
29+
$unserialised = require self::$TEST_TMP_PATH . '/serialised.php';
30+
31+
$this->assertEquals($coverage, $unserialised);
32+
}
33+
}

0 commit comments

Comments
 (0)