Skip to content

Commit be7e048

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

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/Report/PHP.php

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

4332
if ($target !== null) {

tests/tests/PHPTest.php

+33
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)