Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changelog/require-ergebnis-agent-detector-3.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/Container/ServiceProvider/DevToolsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 1 addition & 7 deletions src/Environment/RuntimeEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
41 changes: 36 additions & 5 deletions tests/Environment/RuntimeEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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());
}
}