|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Process; |
| 4 | + |
| 5 | +use PHPStan\Command\AnalyseCommand; |
| 6 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Symfony\Component\Console\Input\ArrayInput; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputDefinition; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Input\InputOption; |
| 13 | +use function getenv; |
| 14 | +use function putenv; |
| 15 | +use function sprintf; |
| 16 | + |
| 17 | +#[CoversClass(ProcessHelper::class)] |
| 18 | +class ProcessHelperTest extends TestCase |
| 19 | +{ |
| 20 | + |
| 21 | + public function testWorkerCommandDisablesPcov(): void |
| 22 | + { |
| 23 | + if (!PcovHelper::shouldDisableInSubProcesses()) { |
| 24 | + $this->markTestSkipped('pcov is not loaded in this process.'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->assertStringContainsString( |
| 28 | + sprintf('-d %s', PcovHelper::DISABLED_INI_SETTING), |
| 29 | + self::getWorkerCommand(), |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + public function testWorkerCommandKeepsPcovWhenAllowed(): void |
| 34 | + { |
| 35 | + if (!PcovHelper::isLoaded()) { |
| 36 | + $this->markTestSkipped('pcov is not loaded in this process.'); |
| 37 | + } |
| 38 | + |
| 39 | + $originalValue = getenv(PcovHelper::ALLOW_ENV_VARIABLE); |
| 40 | + putenv(sprintf('%s=1', PcovHelper::ALLOW_ENV_VARIABLE)); |
| 41 | + |
| 42 | + try { |
| 43 | + $this->assertStringNotContainsString('pcov', self::getWorkerCommand()); |
| 44 | + } finally { |
| 45 | + putenv($originalValue === false |
| 46 | + ? PcovHelper::ALLOW_ENV_VARIABLE |
| 47 | + : sprintf('%s=%s', PcovHelper::ALLOW_ENV_VARIABLE, $originalValue)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public function testWorkerCommandDoesNotMentionPcovWhenItIsNotLoaded(): void |
| 52 | + { |
| 53 | + if (PcovHelper::isLoaded()) { |
| 54 | + $this->markTestSkipped('pcov is loaded in this process.'); |
| 55 | + } |
| 56 | + |
| 57 | + $this->assertStringNotContainsString('pcov', self::getWorkerCommand()); |
| 58 | + } |
| 59 | + |
| 60 | + private static function getWorkerCommand(): string |
| 61 | + { |
| 62 | + return ProcessHelper::getWorkerCommand( |
| 63 | + 'bin/phpstan', |
| 64 | + 'worker', |
| 65 | + null, |
| 66 | + [], |
| 67 | + self::createInput(), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private static function createInput(): InputInterface |
| 72 | + { |
| 73 | + return new ArrayInput(['paths' => ['src']], new InputDefinition([ |
| 74 | + new InputArgument('paths', InputArgument::IS_ARRAY), |
| 75 | + new InputOption(AnalyseCommand::OPTION_LEVEL, mode: InputOption::VALUE_REQUIRED), |
| 76 | + new InputOption('autoload-file', mode: InputOption::VALUE_REQUIRED), |
| 77 | + new InputOption('memory-limit', mode: InputOption::VALUE_REQUIRED), |
| 78 | + new InputOption('xdebug', mode: InputOption::VALUE_NONE), |
| 79 | + new InputOption('verbose', mode: InputOption::VALUE_NONE), |
| 80 | + ])); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments