Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Each assigned id is built again with `TestSuite::fromClassReflector` and narrowe

The manifest id comes from PHPUnit's own `--list-tests-xml` writer. PHPUnit 10 names are `testCaseClass` / `testCaseMethod`. PHPUnit 11 and later use a `testSuite` document with `testClass` / `testMethod`.

The client advertises `test_map` and `remove_failed=true` on every connect, and adds them to a preset `SELECTIVE_TRANSPORT_URL` that lacks them. `test_map` is `record` when the `pcov` extension is loaded, otherwise `select`, and `SELECTIVE_TEST_MAP_DISABLE` of `1`, `true`, or `yes` makes it `0`; with `SELECTIVE_TEST_MAP_XDEBUG=1` and no pcov, Xdebug traces but the client still advertises `select`. When recording, each test's covered project files become its dependencies, and the fragment is uploaded after the run by a separate PHP process (with `display_errors=stderr`, so a deprecation cannot break it). A failed upload warns and does not fail the build. When the server asks for a recording and no tracer is loaded, the client says so once.
The client advertises `test_map` and `remove_failed=true` on every connect, and adds them to a preset `SELECTIVE_TRANSPORT_URL` that lacks them. `test_map` is `record` when the `pcov` extension is loaded, otherwise `select`, and `SELECTIVE_TEST_MAP_DISABLE` of `1`, `true`, or `yes` makes it `0`. Recording requires `pcov`. It is not installed with the client; Composer suggests it (`pecl install pcov`). When recording, each test's covered project files become its dependencies, and the fragment is uploaded after the run by a separate PHP process (with `display_errors=stderr`, so a deprecation cannot break it). A failed upload warns and does not fail the build. When the server asks for a recording and no tracer is loaded, the client says so once.

## PHPUnit versions

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
}
},
"suggest": {
"ext-pcntl": "Stop a run when the process receives SIGINT or SIGTERM."
"ext-pcntl": "Stop a run when the process receives SIGINT or SIGTERM.",
"ext-pcov": "Record each test's covered files for the test map. Not installed with the client."
},
"config": {
"sort-packages": true
Expand Down
7 changes: 6 additions & 1 deletion src/Capability.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public static function testMap(): string
return '0';
}

return extension_loaded('pcov') ? 'record' : 'select';
return self::canRecord() ? 'record' : 'select';
}

public static function canRecord(): bool
{
return extension_loaded('pcov');
}
}
19 changes: 6 additions & 13 deletions src/Phpunit/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Selective\PHPUnit\Phpunit;

use Selective\PHPUnit\Capability;
use Selective\PHPUnit\Paths;

final class Tracer
Expand All @@ -16,11 +17,7 @@ final class Tracer

public function available(): bool
{
if (extension_loaded('pcov')) {
return true;
}

return extension_loaded('xdebug') && getenv('SELECTIVE_TEST_MAP_XDEBUG') === '1';
return Capability::canRecord();
}

public function start(string $id): void
Expand All @@ -30,16 +27,12 @@ public function start(string $id): void
return;
}
try {
if (extension_loaded('pcov')) {
$current = ini_get('pcov.directory');
if ($current === false || $current === '') {
ini_set('pcov.directory', getcwd() ?: '.');
}
$current = ini_get('pcov.directory');
if ($current === false || $current === '') {
ini_set('pcov.directory', getcwd() ?: '.');
}
$filter = new \SebastianBergmann\CodeCoverage\Filter();
$driver = extension_loaded('pcov')
? new \SebastianBergmann\CodeCoverage\Driver\PcovDriver($filter)
: new \SebastianBergmann\CodeCoverage\Driver\XdebugDriver($filter);
$driver = new \SebastianBergmann\CodeCoverage\Driver\PcovDriver($filter);
$this->coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage($driver, $filter);
$this->coverage->start($id);
$this->ready = true;
Expand Down
3 changes: 1 addition & 2 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ private function configure(array $command): void
{
if (($command['record'] ?? false) === true) {
$this->record = true;
$xdebug = extension_loaded('xdebug') && getenv('SELECTIVE_TEST_MAP_XDEBUG') === '1';
if (!extension_loaded('pcov') && !$xdebug && !$this->recordWarned) {
if (!Capability::canRecord() && !$this->recordWarned) {
$this->recordWarned = true;
$this->console->message('Selective asked this runner to record a test map, but the pcov tracer is not available. Tests will run normally.');
}
Expand Down
87 changes: 87 additions & 0 deletions tests/Unit/CapabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Selective\PHPUnit\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Selective\PHPUnit\Command;

final class CapabilityTest extends TestCase
{
public function testWithoutPcovTheClientAdvertisesSelect(): void
{
$this->assertSame('select', $this->testMap([]));
}

public function testWithPcovTheClientAdvertisesRecord(): void
{
$this->requireExtension('extension=pcov', 'pcov');

$this->assertSame('record', $this->testMap(['extension=pcov']));
}

public function testTheOldXdebugOptInDoesNotAffectSelect(): void
{
$this->assertSame('select', $this->testMap([], ['SELECTIVE_TEST_MAP_XDEBUG' => '1']));
}

#[DataProvider('xdebugModes')]
public function testXdebugAloneNeverMakesTheClientAdvertiseRecord(string $mode): void
{
$this->requireExtension('zend_extension=xdebug', 'xdebug');

$this->assertSame('select', $this->testMap(['zend_extension=xdebug', 'xdebug.mode=' . $mode], ['SELECTIVE_TEST_MAP_XDEBUG' => '1']));
}

public static function xdebugModes(): array
{
return ['coverage' => ['coverage'], 'develop' => ['develop'], 'off' => ['off']];
}

#[DataProvider('disableValues')]
public function testDisableWinsOverPcov(string $value): void
{
$this->requireExtension('extension=pcov', 'pcov');

$this->assertSame('0', $this->testMap(['extension=pcov'], ['SELECTIVE_TEST_MAP_DISABLE' => $value]));
}

public static function disableValues(): array
{
return ['1' => ['1'], 'true' => ['true'], 'yes' => ['YES']];
}

private function testMap(array $ini, array $env = []): string
{
$assignments = 'putenv("SELECTIVE_TEST_MAP_DISABLE");putenv("SELECTIVE_TEST_MAP_XDEBUG");';
foreach ($env as $name => $value) {
$assignments .= 'putenv(' . var_export($name . '=' . $value, true) . ');';
}
$script = $assignments . 'require ' . var_export(dirname(__DIR__, 2) . '/src/Capability.php', true) . ';'
. 'echo \Selective\PHPUnit\Capability::testMap();';
[$code, $stdout] = Command::run($this->php($ini, $script));
$this->assertSame(0, $code);

return trim($stdout);
}

private function requireExtension(string $directive, string $name): void
{
[, $stdout] = Command::run($this->php([$directive], 'echo extension_loaded(' . var_export($name, true) . ') ? "yes" : "no";'));
if (trim($stdout) !== 'yes') {
$this->markTestSkipped($name . ' cannot be loaded into a bare PHP here');
}
}

private function php(array $ini, string $script): array
{
$command = [PHP_BINARY, '-n'];
foreach ($ini as $directive) {
array_push($command, '-d', $directive);
}

return array_merge($command, ['-r', $script]);
}
}
Loading