Skip to content
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

[11.x] Fix prompting for missing array arguments on artisan command #50850

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/Illuminate/Console/Concerns/PromptsForMissingInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
{
$prompted = collect($this->getDefinition()->getArguments())
->filter(fn ($argument) => $argument->isRequired() && is_null($input->getArgument($argument->getName())))
->filter(fn ($argument) => $argument->isRequired() && (is_null($input->getArgument($argument->getName())) || (is_array($input->getArgument($argument->getName())) && count($input->getArgument($argument->getName())) === 0)))
->filter(fn ($argument) => $argument->getName() !== 'command')
->each(function ($argument) use ($input) {
$label = $this->promptForMissingArgumentsUsing()[$argument->getName()] ??
Expand Down
37 changes: 37 additions & 0 deletions tests/Console/ConsoleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Events\Dispatcher as EventsDispatcher;
use Illuminate\Foundation\Application as FoundationApplication;
use Illuminate\Tests\Console\Fixtures\FakeCommandWithArrayInputPrompting;
use Illuminate\Tests\Console\Fixtures\FakeCommandWithInputPrompting;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -181,6 +182,42 @@ public function testCommandInputDoesntPromptWhenRequiredArgumentIsPassed()
$this->assertSame(0, $statusCode);
}

public function testCommandInputPromptsWhenRequiredArgumentsAreMissing()
{
$app = new Application(
$laravel = new \Illuminate\Foundation\Application(__DIR__),
$events = m::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]),
'testing'
);

$app->addCommands([$command = new FakeCommandWithArrayInputPrompting()]);

$command->setLaravel($laravel);

$statusCode = $app->call('fake-command-for-testing-array');

$this->assertTrue($command->prompted);
$this->assertSame(0, $statusCode);
}

public function testCommandInputDoesntPromptWhenRequiredArgumentsArePassed()
{
$app = new Application(
$app = new \Illuminate\Foundation\Application(__DIR__),
$events = m::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]),
'testing'
);

$app->addCommands([$command = new FakeCommandWithArrayInputPrompting()]);

$statusCode = $app->call('fake-command-for-testing-array', [
'arguments' => ['foo', 'bar', 'baz'],
]);

$this->assertFalse($command->prompted);
$this->assertSame(0, $statusCode);
}

protected function getMockConsole(array $methods)
{
$app = m::mock(ApplicationContract::class, ['version' => '6.0']);
Expand Down
33 changes: 33 additions & 0 deletions tests/Console/Fixtures/FakeCommandWithArrayInputPrompting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Tests\Console\Fixtures;

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Laravel\Prompts\Prompt;
use Laravel\Prompts\TextPrompt;
use Symfony\Component\Console\Input\InputInterface;

class FakeCommandWithArrayInputPrompting extends Command implements PromptsForMissingInput
{
protected $signature = 'fake-command-for-testing-array {arguments* : arguments}';

public $prompted = false;

protected function configurePrompts(InputInterface $input)
{
Prompt::interactive(true);
Prompt::fallbackWhen(true);

TextPrompt::fallbackUsing(function () {
$this->prompted = true;

return 'foo';
});
}

public function handle(): int
{
return self::SUCCESS;
}
}