File tree Expand file tree Collapse file tree 4 files changed +59
-4
lines changed Expand file tree Collapse file tree 4 files changed +59
-4
lines changed Original file line number Diff line number Diff line change 99 */
1010namespace SebastianBergmann \CodeCoverage \Report ;
1111
12- use function addcslashes ;
1312use function dirname ;
1413use function file_put_contents ;
1514use function serialize ;
@@ -23,9 +22,12 @@ final class PHP
2322 public function process (CodeCoverage $ coverage , ?string $ target = null ): string
2423 {
2524 $ buffer = sprintf (
26- '<?php
27- return \unserialize( \'%s \'); ' ,
28- addcslashes (serialize ($ coverage ), "' " )
25+ "<?php
26+ return \u nserialize(<<<'END_OF_COVERAGE_SERIALIZATION'%s%s%sEND_OF_COVERAGE_SERIALIZATION%s); " ,
27+ PHP_EOL ,
28+ serialize ($ coverage ),
29+ PHP_EOL ,
30+ PHP_EOL
2931 );
3032
3133 if ($ target !== null ) {
Original file line number Diff line number Diff line change @@ -1688,6 +1688,42 @@ protected function setUpXdebugStubForFileWithIgnoredLines(): Driver
16881688 return $ stub ;
16891689 }
16901690
1691+ protected function getLineCoverageForFileWithEval (): CodeCoverage
1692+ {
1693+ $ filter = new Filter ;
1694+ $ filter ->includeFile (TEST_FILES_PATH . 'source_with_eval.php ' );
1695+
1696+ $ coverage = new CodeCoverage (
1697+ $ this ->setUpXdebugStubForFileWithEval (),
1698+ $ filter
1699+ );
1700+
1701+ $ coverage ->start ('FileWithEval ' , true );
1702+ $ coverage ->stop ();
1703+
1704+ return $ coverage ;
1705+ }
1706+
1707+ protected function setUpXdebugStubForFileWithEval (): Driver
1708+ {
1709+ $ stub = $ this ->createStub (Driver::class);
1710+
1711+ $ stub ->method ('stop ' )
1712+ ->willReturn (RawCodeCoverageData::fromXdebugWithoutPathCoverage (
1713+ [
1714+ TEST_FILES_PATH . 'source_with_eval.php ' => [
1715+ 3 => 1 ,
1716+ 5 => 1 ,
1717+ ],
1718+ TEST_FILES_PATH . 'source_with_eval.php(5) : eval() \'d code ' => [
1719+ 1 => 1 ,
1720+ ],
1721+ ]
1722+ ));
1723+
1724+ return $ stub ;
1725+ }
1726+
16911727 protected function getCoverageForClassWithAnonymousFunction (): CodeCoverage
16921728 {
16931729 $ filter = new Filter ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ $ normalResult = 1 + 1 ;
4+
5+ $ evalResult = eval ('return 1+1; ' );
Original file line number Diff line number Diff line change @@ -31,4 +31,16 @@ public function testPHPSerialisationProducesValidCode(): void
3131
3232 $ this ->assertEquals ($ coverage , $ unserialized );
3333 }
34+
35+ public function testPHPSerialisationProducesValidCodeWhenOutputIncludesSingleQuote (): void
36+ {
37+ $ coverage = $ this ->getLineCoverageForFileWithEval ();
38+
39+ /* @noinspection UnusedFunctionResultInspection */
40+ (new PHP )->process ($ coverage , self ::$ TEST_TMP_PATH . '/serialized.php ' );
41+
42+ $ unserialized = require self ::$ TEST_TMP_PATH . '/serialized.php ' ;
43+
44+ $ this ->assertEquals ($ coverage , $ unserialized );
45+ }
3446}
You can’t perform that action at this time.
0 commit comments