diff --git a/.changelog/require-ergebnis-agent-detector-3.md b/.changelog/require-ergebnis-agent-detector-3.md new file mode 100644 index 000000000..83e58f853 --- /dev/null +++ b/.changelog/require-ergebnis-agent-detector-3.md @@ -0,0 +1,6 @@ +--- +category: changed +type: patch +--- + +Allow `ergebnis/agent-detector:^3.0` next to `^2.0`, and use its `EnvironmentDetector` when it is installed. diff --git a/composer.json b/composer.json index 1139be855..93c5b5ef2 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "composer-plugin-api": "^2.0", "container-interop/service-provider": "^0.4.1", "dg/bypass-finals": "^1.9", - "ergebnis/agent-detector": "^2.0", + "ergebnis/agent-detector": "^2.0 || ^3.0", "ergebnis/composer-normalize": "^2.51", "ergebnis/phpunit-agent-reporter": "^1.0", "ergebnis/rector-rules": "^1.18", diff --git a/src/Container/ServiceProvider/DevToolsServiceProvider.php b/src/Container/ServiceProvider/DevToolsServiceProvider.php index 2a6191be8..e412b9e92 100644 --- a/src/Container/ServiceProvider/DevToolsServiceProvider.php +++ b/src/Container/ServiceProvider/DevToolsServiceProvider.php @@ -20,6 +20,8 @@ namespace FastForward\DevTools\Container\ServiceProvider; use Composer\Plugin\Capability\CommandProvider; +use Ergebnis\AgentDetector\Detector; +use Ergebnis\AgentDetector\EnvironmentDetector; use FastForward\DevTools\Composer\Capability\DevToolsCommandProvider; use FastForward\DevTools\Composer\Json\ComposerJson; use FastForward\DevTools\Composer\Json\ComposerJsonInterface; @@ -128,6 +130,15 @@ public function getFactories(): array // Process EnvironmentInterface::class => get(Environment::class), RuntimeEnvironmentInterface::class => get(RuntimeEnvironment::class), + Detector::class => factory( + static function (): Detector { + if (class_exists(EnvironmentDetector::class)) { + return new EnvironmentDetector(); + } + + return new Detector(); + } + ), ExtensionInterface::class => get(Extension::class), OutputCapabilityDetectorInterface::class => get(OutputCapabilityDetector::class), ProcessBuilderInterface::class => get(ProcessBuilder::class), diff --git a/src/Environment/RuntimeEnvironment.php b/src/Environment/RuntimeEnvironment.php index 8462751e3..7d7d36c17 100644 --- a/src/Environment/RuntimeEnvironment.php +++ b/src/Environment/RuntimeEnvironment.php @@ -82,12 +82,6 @@ public function isComposerTestRun(): bool */ public function isAgentPresent(): bool { - $environment = $this->environment->get(); - - if (! \is_array($environment)) { - return false; - } - - return $this->agentDetector->isAgentPresent($environment); + return $this->agentDetector->isAgentPresent(); } } diff --git a/tests/Environment/RuntimeEnvironmentTest.php b/tests/Environment/RuntimeEnvironmentTest.php index 5fde74339..58c18eac3 100644 --- a/tests/Environment/RuntimeEnvironmentTest.php +++ b/tests/Environment/RuntimeEnvironmentTest.php @@ -20,6 +20,7 @@ namespace FastForward\DevTools\Tests\Environment; use Ergebnis\AgentDetector\Detector; +use Ergebnis\AgentDetector\EnvironmentDetector; use FastForward\DevTools\Environment\EnvironmentInterface; use FastForward\DevTools\Environment\RuntimeEnvironment; use PHPUnit\Framework\Attributes\CoversClass; @@ -86,7 +87,28 @@ public static function truthyEnvironmentFlagsProvider(): iterable protected function setUp(): void { $this->environment = $this->prophesize(EnvironmentInterface::class); - $this->runtimeEnvironment = new RuntimeEnvironment($this->environment->reveal(), new Detector()); + $this->runtimeEnvironment = new RuntimeEnvironment($this->environment->reveal(), self::createAgentDetector()); + } + + /** + * @return void + */ + protected function tearDown(): void + { + putenv('AGENT_DETECTOR_DISABLE'); + putenv('CODEX_SANDBOX'); + } + + /** + * @return Detector + */ + private static function createAgentDetector(): Detector + { + if (class_exists(EnvironmentDetector::class)) { + return new EnvironmentDetector(); + } + + return new Detector(); } /** @@ -170,11 +192,20 @@ public function isComposerTestRunWillReturnTrueDuringPhpUnitRuntime(): void #[Test] public function isAgentPresentWillReturnWhetherKnownAgentVariablesExist(): void { - $this->environment->get() - ->willReturn([ - 'CODEX_SANDBOX' => '1', - ]); + putenv('CODEX_SANDBOX=1'); self::assertTrue($this->runtimeEnvironment->isAgentPresent()); } + + /** + * @return void + */ + #[Test] + public function isAgentPresentWillReturnFalseWhenAgentDetectionIsDisabled(): void + { + putenv('CODEX_SANDBOX=1'); + putenv('AGENT_DETECTOR_DISABLE=1'); + + self::assertFalse($this->runtimeEnvironment->isAgentPresent()); + } }