Skip to content

Commit

Permalink
[11.x] Make expectsChoice assertion more intuitive with associative…
Browse files Browse the repository at this point in the history
… arrays. (#52408)

* Add failing test for `expectsChoice` with associative array

* Fix `expectsChoice` with associative array

* Add failing test for `expectsChoice` with `suggest` prompt

* Fix `expectsChoice` with `suggest` prompt

* Add failing test for existing `expectsChoice` behaviour

* Fix existing behaviour of `expectsChoice` command
  • Loading branch information
jessarcher authored Aug 7, 2024
1 parent ef9cf47 commit 5a9d85b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;

class PendingCommand
{
Expand Down Expand Up @@ -397,7 +398,9 @@ protected function mockConsoleOutput()
->ordered()
->with(Mockery::on(function ($argument) use ($question) {
if (isset($this->test->expectedChoices[$question[0]])) {
$this->test->expectedChoices[$question[0]]['actual'] = $argument->getAutocompleterValues();
$this->test->expectedChoices[$question[0]]['actual'] = $argument instanceof ChoiceQuestion && ! array_is_list($this->test->expectedChoices[$question[0]]['expected'])
? $argument->getChoices()
: $argument->getAutocompleterValues();
}

return $argument->getQuestion() == $question[0];
Expand Down
75 changes: 74 additions & 1 deletion tests/Integration/Console/PromptsAssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\suggest;
use function Laravel\Prompts\text;
use function Laravel\Prompts\textarea;

Expand Down Expand Up @@ -59,6 +60,28 @@ public function handle()
->expectsOutput('Jane');
}

public function testAssertionForSuggestPrompt()
{
$this->app[Kernel::class]->registerCommand(
new class extends Command
{
protected $signature = 'test:suggest';

public function handle()
{
$name = suggest('What is your name?', ['John', 'Jane']);

$this->line($name);
}
}
);

$this
->artisan('test:suggest')
->expectsChoice('What is your name?', 'Joe', ['John', 'Jane'])
->expectsOutput('Joe');
}

public function testAssertionForPasswordPrompt()
{
$this->app[Kernel::class]->registerCommand(
Expand Down Expand Up @@ -112,7 +135,7 @@ public function handle()
->expectsOutput('Your name is John.');
}

public function testAssertionForSelectPrompt()
public function testAssertionForSelectPromptWithAList()
{
$this->app[Kernel::class]->registerCommand(
new class extends Command
Expand All @@ -137,6 +160,56 @@ public function handle()
->expectsOutput('Your name is Jane.');
}

public function testAssertionForSelectPromptWithAnAssociativeArray()
{
$this->app[Kernel::class]->registerCommand(
new class extends Command
{
protected $signature = 'test:select';

public function handle()
{
$name = select(
label: 'What is your name?',
options: ['john' => 'John', 'jane' => 'Jane']
);

$this->line("Your name is $name.");
}
}
);

$this
->artisan('test:select')
->expectsChoice('What is your name?', 'jane', ['john' => 'John', 'jane' => 'Jane'])
->expectsOutput('Your name is jane.');
}

public function testAlternativeAssertionForSelectPromptWithAnAssociativeArray()
{
$this->app[Kernel::class]->registerCommand(
new class extends Command
{
protected $signature = 'test:select';

public function handle()
{
$name = select(
label: 'What is your name?',
options: ['john' => 'John', 'jane' => 'Jane']
);

$this->line("Your name is $name.");
}
}
);

$this
->artisan('test:select')
->expectsChoice('What is your name?', 'jane', ['john', 'jane', 'John', 'Jane'])
->expectsOutput('Your name is jane.');
}

public function testAssertionForRequiredMultiselectPrompt()
{
$this->app[Kernel::class]->registerCommand(
Expand Down

0 comments on commit 5a9d85b

Please sign in to comment.