From de6386a189cc0af34e79a5a98a5ac0ffef56e7ff Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Wed, 18 Nov 2026 15:00:17 +0000 Subject: [PATCH 1/2] isolate tests from terminal environment --- tests/Feature/OffersSkillsInstallTest.php | 5 +-- tests/TestCase.php | 38 ++++++++++++++++++++++- tests/Unit/ContextDetectorTest.php | 32 ++++++++++++++----- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/tests/Feature/OffersSkillsInstallTest.php b/tests/Feature/OffersSkillsInstallTest.php index a96568b2..c39f7fad 100644 --- a/tests/Feature/OffersSkillsInstallTest.php +++ b/tests/Feature/OffersSkillsInstallTest.php @@ -47,10 +47,11 @@ function partialMiddleware(array $overrides = []): OffersSkillsInstall }); it('skips when the session is non-interactive', function () { - // Under phpunit, STDIN is not a TTY so isInteractiveSession() returns false. $this->mockConfig->shouldNotReceive('get'); - expect(runMiddleware(new OffersSkillsInstall))->toBeTrue(); + $middleware = partialMiddleware(['isInteractiveSession' => false]); + + expect(runMiddleware($middleware))->toBeTrue(); }); it('skips when the CLI is a project dependency', function () { diff --git a/tests/TestCase.php b/tests/TestCase.php index a3eb9fc0..9df6bb3d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,4 +4,40 @@ use LaravelZero\Framework\Testing\TestCase as BaseTestCase; -abstract class TestCase extends BaseTestCase {} +abstract class TestCase extends BaseTestCase +{ + /** + * The CLI decides between interactive and headless behaviour from the environment + * (TTY, CI and agent env vars). The suite expects headless, which CI and agents + * provide but a developer's own terminal doesn't, so these pin it to match CI. + * + * NO_COLOR matters because a decorated (TTY) output makes Laravel Prompts' spinner + * fork a child process, which can outlive the test and signal the test runner. + */ + protected const ENVIRONMENT = [ + 'CI' => 'true', + 'NO_COLOR' => '1', + ]; + + /** @var array */ + protected array $originalEnvironment = []; + + protected function setUp(): void + { + foreach (static::ENVIRONMENT as $key => $value) { + $this->originalEnvironment[$key] = getenv($key); + putenv("{$key}={$value}"); + } + + parent::setUp(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + foreach ($this->originalEnvironment as $key => $value) { + putenv($value === false ? $key : "{$key}={$value}"); + } + } +} diff --git a/tests/Unit/ContextDetectorTest.php b/tests/Unit/ContextDetectorTest.php index 7a95cc3e..7f68284f 100644 --- a/tests/Unit/ContextDetectorTest.php +++ b/tests/Unit/ContextDetectorTest.php @@ -1,20 +1,36 @@ originalEnv = []; + + foreach (contextDetectorEnvVars() as $var) { + $this->originalEnv[$var] = getenv($var); + putenv($var); + } + ContextDetector::flush(); }); afterEach(function () { - putenv('CLAUDECODE'); - putenv('CLAUDE_CODE'); - putenv('CODEX_SANDBOX'); - putenv('CURSOR_AGENT'); - putenv('CURSOR_TRACE_ID'); - putenv('OPENCODE'); - putenv('OPENCODE_CLIENT'); - putenv('TERM_PROGRAM'); + foreach ($this->originalEnv as $var => $value) { + putenv($value === false ? $var : "{$var}={$value}"); + } + ContextDetector::flush(); }); From e731775871f5c6afba81d3040ab4493fe0ff325b Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Mon, 23 Nov 2026 20:29:16 -0500 Subject: [PATCH 2/2] use php unit config instead of test case --- phpunit.xml.dist | 12 ++++++++++++ tests/TestCase.php | 38 +------------------------------------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 80836995..41cd5346 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,6 +12,18 @@ ./tests/Unit + + + + + ./app diff --git a/tests/TestCase.php b/tests/TestCase.php index 9df6bb3d..a3eb9fc0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,40 +4,4 @@ use LaravelZero\Framework\Testing\TestCase as BaseTestCase; -abstract class TestCase extends BaseTestCase -{ - /** - * The CLI decides between interactive and headless behaviour from the environment - * (TTY, CI and agent env vars). The suite expects headless, which CI and agents - * provide but a developer's own terminal doesn't, so these pin it to match CI. - * - * NO_COLOR matters because a decorated (TTY) output makes Laravel Prompts' spinner - * fork a child process, which can outlive the test and signal the test runner. - */ - protected const ENVIRONMENT = [ - 'CI' => 'true', - 'NO_COLOR' => '1', - ]; - - /** @var array */ - protected array $originalEnvironment = []; - - protected function setUp(): void - { - foreach (static::ENVIRONMENT as $key => $value) { - $this->originalEnvironment[$key] = getenv($key); - putenv("{$key}={$value}"); - } - - parent::setUp(); - } - - protected function tearDown(): void - { - parent::tearDown(); - - foreach ($this->originalEnvironment as $key => $value) { - putenv($value === false ? $key : "{$key}={$value}"); - } - } -} +abstract class TestCase extends BaseTestCase {}