Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeCoverage: better support for Xdebug 3 #424

Merged
merged 1 commit into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/CodeCoverage/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class Collector
public static function detectEngines(): array
{
return array_filter([
extension_loaded('pcov') ? self::ENGINE_PCOV : null,
defined('PHPDBG_VERSION') ? self::ENGINE_PHPDBG : null,
extension_loaded('xdebug') ? self::ENGINE_XDEBUG : null,
extension_loaded('pcov') ? [self::ENGINE_PCOV, phpversion('pcov')] : null,
defined('PHPDBG_VERSION') ? [self::ENGINE_PHPDBG, PHPDBG_VERSION] : null,
extension_loaded('xdebug') ? [self::ENGINE_XDEBUG, phpversion('xdebug')] : null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe [self::ENGINE_XDEBUG => phpversion('xdebug')]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be harder to work with.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought it would be easier xd

]);
}

Expand All @@ -52,7 +52,7 @@ public static function start(string $file, string $engine): void
if (self::isStarted()) {
throw new \LogicException('Code coverage collector has been already started.');

} elseif (!in_array($engine, self::detectEngines(), true)) {
} elseif (!in_array($engine, array_map(function (array $engineInfo) { return $engineInfo[0]; }, self::detectEngines()), true)) {
throw new \LogicException("Code coverage engine '$engine' is not supported.");
}

Expand Down
8 changes: 7 additions & 1 deletion src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,14 @@ private function prepareCodeCoverage(Runner $runner): string
file_put_contents($this->options['--coverage'], '');
$file = realpath($this->options['--coverage']);

[$engine, $version] = reset($engines);

$runner->setEnvironmentVariable(Environment::COVERAGE, $file);
$runner->setEnvironmentVariable(Environment::COVERAGE_ENGINE, $engine = reset($engines));
$runner->setEnvironmentVariable(Environment::COVERAGE_ENGINE, $engine);

if ($engine === CodeCoverage\Collector::ENGINE_XDEBUG && version_compare($version, '3.0.0', '>=')) {
$runner->addPhpIniOption('xdebug.mode', ltrim(ini_get('xdebug.mode') . ',coverage', ','));
}

if ($engine === CodeCoverage\Collector::ENGINE_PCOV && count($this->options['--coverage-src'])) {
$runner->addPhpIniOption('pcov.directory', Helpers::findCommonDirectory($this->options['--coverage-src']));
Expand Down
4 changes: 3 additions & 1 deletion src/Runner/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
'PHP version' . ($isPhpDbg ? '; PHPDBG version' : '')
=> "$info->version ($info->sapi)" . ($isPhpDbg ? "; $info->phpDbgVersion" : ''),
'Loaded php.ini files' => count($info->iniFiles) ? implode(', ', $info->iniFiles) : '(none)',
'Code coverage engines' => count($info->codeCoverageEngines) ? implode(', ', $info->codeCoverageEngines) : '(not available)',
'Code coverage engines' => count($info->codeCoverageEngines)
? implode(', ', array_map(function (array $engineInfo) { return sprintf('%s (%s)', ...$engineInfo); }, $info->codeCoverageEngines))
: '(not available)',
'PHP temporary directory' => $info->tempDir == '' ? '(empty)' : $info->tempDir,
'Loaded extensions' => count($info->extensions) ? implode(', ', $info->extensions) : '(none)',
] as $title => $value) {
Expand Down
12 changes: 10 additions & 2 deletions tests/CodeCoverage/Collector.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ use Tester\FileMock;
require __DIR__ . '/../bootstrap.php';


$engines = array_filter(CodeCoverage\Collector::detectEngines(), function (string $engine) {
$engines = array_filter(CodeCoverage\Collector::detectEngines(), function (array $engineInfo) {
[$engine] = $engineInfo;
return $engine !== CodeCoverage\Collector::ENGINE_PCOV; // PCOV needs system pcov.directory INI to be set
});
if (count($engines) < 1) {
Tester\Environment::skip('Requires Xdebug or PHPDB SAPI.');
}
$engine = reset($engines);
[$engine, $version] = reset($engines);

if ($engine === CodeCoverage\Collector::ENGINE_XDEBUG
&& version_compare($version, '3.0.0', '>=')
&& strpos(ini_get('xdebug.mode'), 'coverage') === false
) {
Tester\Environment::skip('Requires xdebug.mode=coverage with Xdebug 3.');
}

if (CodeCoverage\Collector::isStarted()) {
Tester\Environment::skip('Requires running without --coverage.');
Expand Down
6 changes: 3 additions & 3 deletions tests/Runner/PhpInterpreter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Assert::same(strpos(PHP_SAPI, 'cgi') !== false, $interpreter->isCgi());
$count = 0;
$engines = $interpreter->getCodeCoverageEngines();
if (defined('PHPDBG_VERSION')) {
Assert::contains(Tester\CodeCoverage\Collector::ENGINE_PHPDBG, $engines);
Assert::contains([Tester\CodeCoverage\Collector::ENGINE_PHPDBG, PHPDBG_VERSION], $engines);
$count++;
}
if (extension_loaded('xdebug')) {
Assert::contains(Tester\CodeCoverage\Collector::ENGINE_XDEBUG, $engines);
Assert::contains([Tester\CodeCoverage\Collector::ENGINE_XDEBUG, phpversion('xdebug')], $engines);
$count++;
}
if (extension_loaded('pcov')) {
Assert::contains(Tester\CodeCoverage\Collector::ENGINE_PCOV, $engines);
Assert::contains([Tester\CodeCoverage\Collector::ENGINE_PCOV, phpversion('pcov')], $engines);
$count++;
}
Assert::count($count, $engines);