diff --git a/README.md b/README.md index 8dd30e5..599637e 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` 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/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..331011f 100644 --- a/src/Capability.php +++ b/src/Capability.php @@ -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'); } } diff --git a/src/Phpunit/Tracer.php b/src/Phpunit/Tracer.php index 753aac4..b3fd445 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 @@ -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; 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..fad9a61 --- /dev/null +++ b/tests/Unit/CapabilityTest.php @@ -0,0 +1,87 @@ +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]); + } +}