Skip to content
Closed
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
11 changes: 10 additions & 1 deletion src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

Expand Down Expand Up @@ -172,7 +173,15 @@ public function run(InputInterface $input, OutputInterface $output): int
OutputStyle::class, ['input' => $input, 'output' => $output]
);

$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);
$componentsOutput = $this->output;

if ($output instanceof ConsoleOutputInterface) {
$componentsOutput = $this->laravel->make(
OutputStyle::class, ['input' => $input, 'output' => $output->getErrorOutput()]
);
}

$this->components = $this->laravel->make(Factory::class, ['output' => $componentsOutput]);

$this->configurePrompts($input);

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Console/Concerns/ConfiguresPrompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ protected function configurePrompts(InputInterface $input)
{
Prompt::setOutput($this->output);

Prompt::interactive(($input->isInteractive() && defined('STDIN') && stream_isatty(STDIN)) || $this->laravel->runningUnitTests());
Prompt::interactive(($input->isInteractive() && defined('STDIN') && stream_isatty(STDIN)) || (method_exists($this->laravel, 'runningUnitTests') && $this->laravel->runningUnitTests()));

Prompt::validateUsing(fn (Prompt $prompt) => $this->validatePrompt($prompt->value(), $prompt->validate));

Prompt::fallbackWhen(windows_os() || $this->laravel->runningUnitTests());
Prompt::fallbackWhen(windows_os() || (method_exists($this->laravel, 'runningUnitTests') && $this->laravel->runningUnitTests()));

TextPrompt::fallbackUsing(fn (TextPrompt $prompt) => $this->promptUntilValid(
fn () => $this->components->ask($prompt->label, $prompt->default ?: null) ?? '',
Expand Down
115 changes: 115 additions & 0 deletions tests/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;

Expand Down Expand Up @@ -215,4 +216,118 @@ public function testChoiceWithMultiselect()

$command->choice('Select all that apply.', ['option-1', 'option-2', 'option-3'], null, null, true);
}

public function testConsoleComponentsUseErrorOutputWhenConsoleOutputInterfaceIsAvailable()
{
$command = new class extends Command
{
public function handle()
{
$this->components->task('Test task', fn () => true);
}
};

$application = m::mock(Application::class);
$command->setLaravel($application);

$input = new ArrayInput([]);
$output = new ConsoleOutput;

$mainOutputStyle = m::mock(OutputStyle::class);
$application->shouldReceive('make')
->with(OutputStyle::class, ['input' => $input, 'output' => $output])
->andReturn($mainOutputStyle);

$errorOutputStyle = m::mock(OutputStyle::class);
$application->shouldReceive('make')
->with(OutputStyle::class, ['input' => $input, 'output' => $output->getErrorOutput()])
->andReturn($errorOutputStyle);

$factory = m::mock(Factory::class);
$application->shouldReceive('make')
->with(Factory::class, ['output' => $errorOutputStyle])
->andReturn($factory);

$factory->shouldReceive('task')
->with('Test task', m::type('callable'))
->once();

$application->shouldReceive('call')->with([$command, 'handle'])->andReturnUsing(function () use ($command) {
$command->handle();
});
$application->shouldReceive('runningUnitTests')->andReturn(true);

$command->run($input, $output);
}

public function testConsoleComponentsUseMainOutputWhenConsoleOutputInterfaceIsNotAvailable()
{
$command = new class extends Command
{
public function handle()
{
$this->components->task('Test task', fn () => true);
}
};

$application = m::mock(Application::class);
$command->setLaravel($application);

$input = new ArrayInput([]);
$output = new NullOutput;

$mainOutputStyle = m::mock(OutputStyle::class);
$application->shouldReceive('make')
->with(OutputStyle::class, ['input' => $input, 'output' => $output])
->andReturn($mainOutputStyle);

$factory = m::mock(Factory::class);
$application->shouldReceive('make')
->with(Factory::class, ['output' => $mainOutputStyle])
->andReturn($factory);

$factory->shouldReceive('task')
->with('Test task', m::type('callable'))
->once();

$application->shouldReceive('call')->with([$command, 'handle'])->andReturnUsing(function () use ($command) {
$command->handle();
});
$application->shouldReceive('runningUnitTests')->andReturn(true);

$command->run($input, $output);
}

public function testConsoleComponentsUseMainOutputWhenOutputIsAlreadyOutputStyle()
{
$command = new class extends Command
{
public function handle()
{
$this->components->task('Test task', fn () => true);
}
};

$application = m::mock(Application::class);
$command->setLaravel($application);

$input = new ArrayInput([]);
$existingOutputStyle = m::mock(OutputStyle::class);

$factory = m::mock(Factory::class);
$application->shouldReceive('make')
->with(Factory::class, ['output' => $existingOutputStyle])
->andReturn($factory);

$factory->shouldReceive('task')
->with('Test task', m::type('callable'))
->once();

$application->shouldReceive('call')->with([$command, 'handle'])->andReturnUsing(function () use ($command) {
$command->handle();
});
$application->shouldReceive('runningUnitTests')->andReturn(true);

$command->run($input, $existingOutputStyle);
}
}