Skip to content

Commit

Permalink
Fix PHPT coverage from phpdbg
Browse files Browse the repository at this point in the history
Fixes #3111.
Stop using auto_append_file as it doesn't work with the phpdbg sapi.
Instead, the job's code is written to a temp file and the coverage instrumentation code takes over the code.
  • Loading branch information
kabel authored and sebastianbergmann committed Jul 9, 2018
1 parent 7a7c736 commit 09c97ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
19 changes: 9 additions & 10 deletions src/Runner/PhptTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function run(TestResult $result = null): TestResult
}

if ($result->getCollectCodeCoverageInformation()) {
$this->renderForCoverage($settings);
$this->renderForCoverage($code);
}

Timer::start();
Expand Down Expand Up @@ -488,7 +488,7 @@ private function getCoverageFiles(): array
];
}

private function renderForCoverage(array &$settings): void
private function renderForCoverage(string &$job): void
{
$files = $this->getCoverageFiles();

Expand Down Expand Up @@ -520,24 +520,23 @@ private function renderForCoverage(array &$settings): void
'phar' => $phar,
'globals' => $globals,
'job' => $files['job'],
'coverageFile' => $files['coverage'],
'autoPrependFile' => \var_export(
!empty($settings['auto_prepend_file']) ? $settings['auto_prepend_file'] : false,
true
)
'coverageFile' => $files['coverage']
]
);

\file_put_contents($files['job'], $template->render());

$settings['auto_prepend_file'] = $files['job'];
\file_put_contents($files['job'], $job);
$job = $template->render();
}

private function cleanupForCoverage(): array
{
$files = $this->getCoverageFiles();
$coverage = @\unserialize(\file_get_contents($files['coverage']));

if ($coverage === false) {
$coverage = [];
}

foreach ($files as $file) {
@\unlink($file);
}
Expand Down
10 changes: 2 additions & 8 deletions src/Util/PHP/Template/PhptTestCase.tpl.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use SebastianBergmann\CodeCoverage\CodeCoverage;

$composerAutoload = {composerAutoload};
$phar = {phar};
$autoPrependFile = {autoPrependFile};

ob_start();

Expand All @@ -28,7 +27,7 @@ if (class_exists('SebastianBergmann\CodeCoverage\CodeCoverage')) {
$coverage->start(__FILE__);
}

register_shutdown_function(function() use ($coverage, $autoPrependFile) {
register_shutdown_function(function() use ($coverage) {
$output = null;
if ($coverage) {
$output = $coverage->stop();
Expand All @@ -38,9 +37,4 @@ register_shutdown_function(function() use ($coverage, $autoPrependFile) {

ob_end_clean();

if ($autoPrependFile) {
require $autoPrependFile;
$includes = get_included_files();
$GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'][] = array_pop($includes);
unset($includes);
}
require '{job}';

0 comments on commit 09c97ca

Please sign in to comment.