From 87d7164cf163a3ceb092002f32848907af0f81e5 Mon Sep 17 00:00:00 2001 From: Nate Vick Date: Mon, 9 Nov 2026 08:38:54 -0800 Subject: [PATCH 1/2] fix: advertise record when the Xdebug opt-in can actually record Capability::testMap() only looked for pcov, so a runner using Xdebug with SELECTIVE_TEST_MAP_XDEBUG=1 advertised select and the server never asked it to record, even though Tracer could. Tracer::available() and the "no tracer" warning in Session each carried their own copy of the check, and neither looked at xdebug.mode: with Xdebug loaded and the opt-in set but no coverage mode, the client would take a recording and write nothing. One predicate now, Capability::canRecord(): pcov is loaded, or Xdebug 3.1+ reports the coverage mode (xdebug_info('mode'), so XDEBUG_MODE is honored) and the opt-in is set. testMap(), Tracer::available() and Session use it. SELECTIVE_TEST_MAP_DISABLE still wins. Also suggest ext-pcov in composer.json and say in the README that pcov is required to record and is not installed with the client, plus the Xdebug opt-in. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01RhRfiMBEZfMLrhpYsH7DSF --- README.md | 2 +- composer.json | 3 +- src/Capability.php | 26 +++++++++++++- src/Phpunit/Tracer.php | 7 ++-- src/Session.php | 3 +- tests/Unit/CapabilityTest.php | 64 +++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 tests/Unit/CapabilityTest.php diff --git a/README.md b/README.md index 7c967db..ab0f1f6 100644 --- a/README.md +++ b/README.md @@ -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` only when a coverage tracer is usable, otherwise `select`, and `SELECTIVE_TEST_MAP_DISABLE` of `1`, `true`, or `yes` makes it `0`. Recording needs the `pcov` extension, which is not installed with the client (`pecl install pcov`). Xdebug is the opt-in alternative: with `SELECTIVE_TEST_MAP_XDEBUG=1`, Xdebug 3.1 or later counts as a tracer when `xdebug.mode` (or `XDEBUG_MODE`) includes `coverage`. Without that mode the client 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. ## PHPUnit versions diff --git a/composer.json b/composer.json index b8865a1..2d3d722 100644 --- a/composer.json +++ b/composer.json @@ -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 diff --git a/src/Capability.php b/src/Capability.php index 7458ec9..0ea6240 100644 --- a/src/Capability.php +++ b/src/Capability.php @@ -13,6 +13,30 @@ public static function testMap(): string return '0'; } - return extension_loaded('pcov') ? 'record' : 'select'; + return self::canRecord() ? 'record' : 'select'; + } + + public static function canRecord(): bool + { + return self::tracerUsable( + extension_loaded('pcov'), + self::xdebugModes(), + getenv('SELECTIVE_TEST_MAP_XDEBUG') === '1' + ); + } + + public static function tracerUsable(bool $pcov, array $xdebugModes, bool $xdebugOptIn): bool + { + return $pcov || ($xdebugOptIn && in_array('coverage', $xdebugModes, true)); + } + + private static function xdebugModes(): array + { + if (!extension_loaded('xdebug') || version_compare((string) phpversion('xdebug'), '3.1', '<')) { + return []; + } + $modes = xdebug_info('mode'); + + return is_array($modes) ? $modes : []; } } diff --git a/src/Phpunit/Tracer.php b/src/Phpunit/Tracer.php index 753aac4..2fda599 100644 --- a/src/Phpunit/Tracer.php +++ b/src/Phpunit/Tracer.php @@ -4,6 +4,7 @@ namespace Selective\PHPUnit\Phpunit; +use Selective\PHPUnit\Capability; use Selective\PHPUnit\Paths; final class Tracer @@ -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 diff --git a/src/Session.php b/src/Session.php index 9daa91e..129bb8d 100644 --- a/src/Session.php +++ b/src/Session.php @@ -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.'); } diff --git a/tests/Unit/CapabilityTest.php b/tests/Unit/CapabilityTest.php new file mode 100644 index 0000000..1aefc04 --- /dev/null +++ b/tests/Unit/CapabilityTest.php @@ -0,0 +1,64 @@ +disable = getenv('SELECTIVE_TEST_MAP_DISABLE'); + } + + protected function tearDown(): void + { + putenv($this->disable === false ? 'SELECTIVE_TEST_MAP_DISABLE' : 'SELECTIVE_TEST_MAP_DISABLE=' . $this->disable); + } + + public static function tracers(): array + { + return [ + 'nothing loaded' => [false, [], false, false], + 'pcov' => [true, [], false, true], + 'pcov wins over the opt-in' => [true, ['develop'], true, true], + 'xdebug coverage without the opt-in' => [false, ['coverage'], false, false], + 'xdebug coverage with the opt-in' => [false, ['coverage'], true, true], + 'xdebug coverage among other modes' => [false, ['develop', 'coverage'], true, true], + 'xdebug without coverage mode' => [false, ['develop'], true, false], + 'xdebug mode off' => [false, [], true, false], + ]; + } + + #[DataProvider('tracers')] + public function testATracerIsUsableOnlyWhenItCanActuallyRecord(bool $pcov, array $modes, bool $optIn, bool $expected): void + { + $this->assertSame($expected, Capability::tracerUsable($pcov, $modes, $optIn)); + } + + #[DataProvider('disableValues')] + public function testDisableWinsOverAnyTracer(string $value): void + { + putenv('SELECTIVE_TEST_MAP_DISABLE=' . $value); + + $this->assertSame('0', Capability::testMap()); + } + + public static function disableValues(): array + { + return ['1' => ['1'], 'true' => ['true'], 'yes' => ['YES']]; + } + + public function testTestMapFollowsTheLoadedTracerWhenNotDisabled(): void + { + putenv('SELECTIVE_TEST_MAP_DISABLE'); + + $this->assertSame(Capability::canRecord() ? 'record' : 'select', Capability::testMap()); + } +} From 735896a044e7b3ea72e46ee8f0196238762a0e29 Mon Sep 17 00:00:00 2001 From: Nate Vick Date: Mon, 9 Nov 2026 08:59:43 -0800 Subject: [PATCH 2/2] fix: drop the Xdebug opt-in, record with pcov only Advertising record for Xdebug (the previous commit) makes the three recording conformance scenarios time out under Xdebug, 3 of 3 runs. The first recorded test takes ~9s: CodeCoverage::stop() runs with an empty filter and processes ~290 vendor files (7.6s measured). Excluding vendor with xdebug_set_filter does not help, since PHPUnit's files are compiled before the filter is set. So the Xdebug path is removed. Capability::canRecord() is now just extension_loaded('pcov'), still the one predicate behind testMap(), Tracer::available() and the "no tracer" warning. The XdebugDriver fallback and the SELECTIVE_TEST_MAP_XDEBUG branch are gone, the README no longer mentions Xdebug, and the tests cover pcov, no pcov, Xdebug in each mode with the old env var, and DISABLE. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01RhRfiMBEZfMLrhpYsH7DSF --- README.md | 2 +- src/Capability.php | 21 +--------- src/Phpunit/Tracer.php | 12 ++---- tests/Unit/CapabilityTest.php | 75 +++++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index ab0f1f6..2105463 100644 --- a/README.md +++ b/README.md @@ -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` only when a coverage tracer is usable, otherwise `select`, and `SELECTIVE_TEST_MAP_DISABLE` of `1`, `true`, or `yes` makes it `0`. Recording needs the `pcov` extension, which is not installed with the client (`pecl install pcov`). Xdebug is the opt-in alternative: with `SELECTIVE_TEST_MAP_XDEBUG=1`, Xdebug 3.1 or later counts as a tracer when `xdebug.mode` (or `XDEBUG_MODE`) includes `coverage`. Without that mode the client 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 diff --git a/src/Capability.php b/src/Capability.php index 0ea6240..331011f 100644 --- a/src/Capability.php +++ b/src/Capability.php @@ -18,25 +18,6 @@ public static function testMap(): string public static function canRecord(): bool { - return self::tracerUsable( - extension_loaded('pcov'), - self::xdebugModes(), - getenv('SELECTIVE_TEST_MAP_XDEBUG') === '1' - ); - } - - public static function tracerUsable(bool $pcov, array $xdebugModes, bool $xdebugOptIn): bool - { - return $pcov || ($xdebugOptIn && in_array('coverage', $xdebugModes, true)); - } - - private static function xdebugModes(): array - { - if (!extension_loaded('xdebug') || version_compare((string) phpversion('xdebug'), '3.1', '<')) { - return []; - } - $modes = xdebug_info('mode'); - - return is_array($modes) ? $modes : []; + return extension_loaded('pcov'); } } diff --git a/src/Phpunit/Tracer.php b/src/Phpunit/Tracer.php index 2fda599..b3fd445 100644 --- a/src/Phpunit/Tracer.php +++ b/src/Phpunit/Tracer.php @@ -27,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; diff --git a/tests/Unit/CapabilityTest.php b/tests/Unit/CapabilityTest.php index 1aefc04..fad9a61 100644 --- a/tests/Unit/CapabilityTest.php +++ b/tests/Unit/CapabilityTest.php @@ -6,48 +6,46 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Selective\PHPUnit\Capability; +use Selective\PHPUnit\Command; final class CapabilityTest extends TestCase { - private string|false $disable; + public function testWithoutPcovTheClientAdvertisesSelect(): void + { + $this->assertSame('select', $this->testMap([])); + } - protected function setUp(): void + public function testWithPcovTheClientAdvertisesRecord(): void { - $this->disable = getenv('SELECTIVE_TEST_MAP_DISABLE'); + $this->requireExtension('extension=pcov', 'pcov'); + + $this->assertSame('record', $this->testMap(['extension=pcov'])); } - protected function tearDown(): void + public function testTheOldXdebugOptInDoesNotAffectSelect(): void { - putenv($this->disable === false ? 'SELECTIVE_TEST_MAP_DISABLE' : 'SELECTIVE_TEST_MAP_DISABLE=' . $this->disable); + $this->assertSame('select', $this->testMap([], ['SELECTIVE_TEST_MAP_XDEBUG' => '1'])); } - public static function tracers(): array + #[DataProvider('xdebugModes')] + public function testXdebugAloneNeverMakesTheClientAdvertiseRecord(string $mode): void { - return [ - 'nothing loaded' => [false, [], false, false], - 'pcov' => [true, [], false, true], - 'pcov wins over the opt-in' => [true, ['develop'], true, true], - 'xdebug coverage without the opt-in' => [false, ['coverage'], false, false], - 'xdebug coverage with the opt-in' => [false, ['coverage'], true, true], - 'xdebug coverage among other modes' => [false, ['develop', 'coverage'], true, true], - 'xdebug without coverage mode' => [false, ['develop'], true, false], - 'xdebug mode off' => [false, [], true, false], - ]; + $this->requireExtension('zend_extension=xdebug', 'xdebug'); + + $this->assertSame('select', $this->testMap(['zend_extension=xdebug', 'xdebug.mode=' . $mode], ['SELECTIVE_TEST_MAP_XDEBUG' => '1'])); } - #[DataProvider('tracers')] - public function testATracerIsUsableOnlyWhenItCanActuallyRecord(bool $pcov, array $modes, bool $optIn, bool $expected): void + public static function xdebugModes(): array { - $this->assertSame($expected, Capability::tracerUsable($pcov, $modes, $optIn)); + return ['coverage' => ['coverage'], 'develop' => ['develop'], 'off' => ['off']]; } #[DataProvider('disableValues')] - public function testDisableWinsOverAnyTracer(string $value): void + public function testDisableWinsOverPcov(string $value): void { - putenv('SELECTIVE_TEST_MAP_DISABLE=' . $value); + $this->requireExtension('extension=pcov', 'pcov'); - $this->assertSame('0', Capability::testMap()); + $this->assertSame('0', $this->testMap(['extension=pcov'], ['SELECTIVE_TEST_MAP_DISABLE' => $value])); } public static function disableValues(): array @@ -55,10 +53,35 @@ public static function disableValues(): array return ['1' => ['1'], 'true' => ['true'], 'yes' => ['YES']]; } - public function testTestMapFollowsTheLoadedTracerWhenNotDisabled(): void + 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 { - putenv('SELECTIVE_TEST_MAP_DISABLE'); + $command = [PHP_BINARY, '-n']; + foreach ($ini as $directive) { + array_push($command, '-d', $directive); + } - $this->assertSame(Capability::canRecord() ? 'record' : 'select', Capability::testMap()); + return array_merge($command, ['-r', $script]); } }