-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add runningInConsole() method to Application
#341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
binaryfire
wants to merge
2
commits into
hypervel:main
Choose a base branch
from
binaryfire:feature/running-in-console
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Hypervel\Tests\Foundation; | ||
|
|
||
| use Hyperf\Context\Context; | ||
| use Hypervel\Console\Contracts\EventMutex; | ||
| use Hypervel\Console\Scheduling\Event; | ||
| use Hypervel\Foundation\Console\Contracts\Kernel as KernelContract; | ||
| use Hypervel\Support\Facades\Artisan; | ||
| use Hypervel\Testbench\TestCase; | ||
| use Mockery as m; | ||
| use Symfony\Component\Console\Input\ArrayInput; | ||
| use Symfony\Component\Console\Output\BufferedOutput; | ||
|
|
||
| /** | ||
| * @internal | ||
| * @coversNothing | ||
| */ | ||
| class FoundationRunningInConsoleTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Reset captured values between tests | ||
| CaptureRunningInConsoleCommand::$capturedValue = null; | ||
| NestedCallerCommand::$capturedValueBeforeCall = null; | ||
| NestedCallerCommand::$capturedValueAfterCall = null; | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| Context::destroy('__foundation.running_in_console'); | ||
| m::close(); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testDefaultStateReturnsFalse(): void | ||
| { | ||
| $this->assertFalse($this->app->runningInConsole()); | ||
| } | ||
|
|
||
| public function testMarkAsRunningInConsoleSetsStateToTrue(): void | ||
| { | ||
| $this->assertFalse($this->app->runningInConsole()); | ||
|
|
||
| $this->app->markAsRunningInConsole(); | ||
|
|
||
| $this->assertTrue($this->app->runningInConsole()); | ||
| } | ||
|
|
||
| public function testMarkAsRunningInConsoleIsIdempotent(): void | ||
| { | ||
| $this->app->markAsRunningInConsole(); | ||
| $this->app->markAsRunningInConsole(); | ||
| $this->app->markAsRunningInConsole(); | ||
|
|
||
| $this->assertTrue($this->app->runningInConsole()); | ||
| } | ||
|
|
||
| public function testKernelHandleSetsRunningInConsole(): void | ||
| { | ||
| $this->assertFalse($this->app->runningInConsole()); | ||
|
|
||
| Artisan::command('test:noop', fn () => 0); | ||
|
|
||
| $kernel = $this->app->get(KernelContract::class); | ||
| $kernel->handle( | ||
| new ArrayInput(['command' => 'test:noop']), | ||
| new BufferedOutput() | ||
| ); | ||
|
|
||
| $this->assertTrue($this->app->runningInConsole()); | ||
| } | ||
|
|
||
| public function testArtisanCallDoesNotSetRunningInConsole(): void | ||
| { | ||
| $this->assertFalse($this->app->runningInConsole()); | ||
|
|
||
| Artisan::command('test:noop', fn () => 0); | ||
| Artisan::call('test:noop'); | ||
|
|
||
| // Artisan::call() from non-console context should NOT set the flag | ||
| $this->assertFalse($this->app->runningInConsole()); | ||
| } | ||
|
|
||
| public function testCodeInsideCliCommandSeesRunningInConsoleTrue(): void | ||
| { | ||
| // This is the primary use case: code running inside a CLI command | ||
| // (like a model's global scope) should see runningInConsole() = true | ||
|
|
||
| $this->registerCaptureCommand(); | ||
|
|
||
| $kernel = $this->app->get(KernelContract::class); | ||
| $kernel->handle( | ||
| new ArrayInput(['command' => 'test:capture']), | ||
| new BufferedOutput() | ||
| ); | ||
|
|
||
| $this->assertTrue(CaptureRunningInConsoleCommand::$capturedValue); | ||
| } | ||
|
|
||
| public function testCodeInsideKernelCallWithoutPriorHandleSeesRunningInConsoleFalse(): void | ||
| { | ||
| // When Kernel::call() is used without a prior Kernel::handle() (e.g., from | ||
| // HTTP context), code inside the command should see runningInConsole() = false | ||
|
|
||
| $this->registerCaptureCommand(); | ||
|
|
||
| $kernel = $this->app->get(KernelContract::class); | ||
| $kernel->call('test:capture'); | ||
|
|
||
| $this->assertFalse(CaptureRunningInConsoleCommand::$capturedValue); | ||
| } | ||
|
|
||
| public function testNestedCommandInheritsConsoleContext(): void | ||
| { | ||
| // When a CLI command calls another command via Artisan::call(), | ||
| // the nested command should inherit the console context | ||
|
|
||
| $this->registerCaptureCommand(); | ||
| $this->registerNestedCallerCommand(); | ||
|
|
||
| $kernel = $this->app->get(KernelContract::class); | ||
| $kernel->handle( | ||
| new ArrayInput(['command' => 'test:nested-caller']), | ||
| new BufferedOutput() | ||
| ); | ||
|
|
||
| // Parent command should see true | ||
| $this->assertTrue(NestedCallerCommand::$capturedValueBeforeCall); | ||
|
|
||
| // Nested command (called via Artisan::call) should also see true | ||
| $this->assertTrue(CaptureRunningInConsoleCommand::$capturedValue); | ||
|
|
||
| // Parent should still see true after the call | ||
| $this->assertTrue(NestedCallerCommand::$capturedValueAfterCall); | ||
| } | ||
|
|
||
| public function testAppHelperReflectsRunningInConsoleState(): void | ||
| { | ||
| $this->assertFalse(app()->runningInConsole()); | ||
|
|
||
| $this->app->markAsRunningInConsole(); | ||
|
|
||
| $this->assertTrue(app()->runningInConsole()); | ||
| } | ||
|
|
||
| public function testScheduledCommandInheritsConsoleContext(): void | ||
| { | ||
| // Scheduled commands are run via Kernel::call() from schedule:run, | ||
| // which is itself invoked via Kernel::handle(). The scheduled command | ||
| // should inherit the console context from the parent schedule:run command. | ||
|
|
||
| $this->registerCaptureCommand(); | ||
|
|
||
| // Simulate the schedule:run command having set the console context | ||
| // (this happens when `php artisan schedule:run` calls Kernel::handle()) | ||
| $this->app->markAsRunningInConsole(); | ||
|
|
||
| // Create a scheduled event that runs our capture command | ||
| $event = new Event(m::mock(EventMutex::class), 'test:capture'); | ||
|
|
||
| // Run the scheduled event - this calls Kernel::call() internally | ||
| $event->run($this->app); | ||
|
|
||
| // The command should have inherited the console context | ||
| $this->assertTrue( | ||
| CaptureRunningInConsoleCommand::$capturedValue, | ||
| 'Scheduled command should inherit runningInConsole() = true from schedule:run' | ||
| ); | ||
| } | ||
|
|
||
| private function registerCaptureCommand(): void | ||
| { | ||
| $this->app->bind(CaptureRunningInConsoleCommand::class, CaptureRunningInConsoleCommand::class); | ||
| $this->app->get(KernelContract::class)->registerCommand(CaptureRunningInConsoleCommand::class); | ||
| } | ||
|
|
||
| private function registerNestedCallerCommand(): void | ||
| { | ||
| $this->app->bind(NestedCallerCommand::class, NestedCallerCommand::class); | ||
| $this->app->get(KernelContract::class)->registerCommand(NestedCallerCommand::class); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test command that captures runningInConsole() value during execution. | ||
| */ | ||
| class CaptureRunningInConsoleCommand extends \Hypervel\Console\Command | ||
| { | ||
| protected ?string $signature = 'test:capture'; | ||
|
|
||
| protected string $description = 'Captures runningInConsole value'; | ||
|
|
||
| public static ?bool $capturedValue = null; | ||
|
|
||
| public function handle(): int | ||
| { | ||
| self::$capturedValue = app()->runningInConsole(); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test command that calls another command via Artisan::call(). | ||
| */ | ||
| class NestedCallerCommand extends \Hypervel\Console\Command | ||
| { | ||
| protected ?string $signature = 'test:nested-caller'; | ||
|
|
||
| protected string $description = 'Calls another command via Artisan::call'; | ||
|
|
||
| public static ?bool $capturedValueBeforeCall = null; | ||
|
|
||
| public static ?bool $capturedValueAfterCall = null; | ||
|
|
||
| public function handle(): int | ||
| { | ||
| self::$capturedValueBeforeCall = app()->runningInConsole(); | ||
|
|
||
| \Hypervel\Support\Facades\Artisan::call('test:capture'); | ||
|
|
||
| self::$capturedValueAfterCall = app()->runningInConsole(); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these two lines duplicated code?