diff --git a/src/Illuminate/Testing/PendingCommand.php b/src/Illuminate/Testing/PendingCommand.php index 1d71cf578674..0fc154612cb8 100644 --- a/src/Illuminate/Testing/PendingCommand.php +++ b/src/Illuminate/Testing/PendingCommand.php @@ -135,6 +135,22 @@ public function expectsChoice($question, $answer, $answers, $strict = false) return $this->expectsQuestion($question, $answer); } + /** + * Specify an expected search question with an expected search string, followed by an expected choice question with expected answers. + * + * @param string $question + * @param string|array $answer + * @param string $search + * @param array $answers + * @return $this + */ + public function expectsSearch($question, $answer, $search, $answers) + { + return $this + ->expectsQuestion($question, $search) + ->expectsChoice($question, $answer, $answers); + } + /** * Specify output that should be printed when the command runs. * diff --git a/tests/Integration/Console/PromptsAssertionTest.php b/tests/Integration/Console/PromptsAssertionTest.php index 6822be83fc0d..64076fdddb2e 100644 --- a/tests/Integration/Console/PromptsAssertionTest.php +++ b/tests/Integration/Console/PromptsAssertionTest.php @@ -7,8 +7,10 @@ use Orchestra\Testbench\TestCase; use function Laravel\Prompts\confirm; +use function Laravel\Prompts\multisearch; use function Laravel\Prompts\multiselect; use function Laravel\Prompts\password; +use function Laravel\Prompts\search; use function Laravel\Prompts\select; use function Laravel\Prompts\suggest; use function Laravel\Prompts\text; @@ -269,4 +271,105 @@ public function handle() ->expectsChoice('Which names do you like?', ['None'], ['John', 'Jane', 'Sally', 'Jack']) ->expectsOutput('You like nobody.'); } + + public function testAssertionForSearchPrompt() + { + $this->app[Kernel::class]->registerCommand( + new class extends Command + { + protected $signature = 'test:search'; + + public function handle() + { + $options = collect(['John', 'Jane', 'Sally', 'Jack']); + + $name = search( + label: 'What is your name?', + options: fn (string $value) => strlen($value) > 0 + ? $options->filter(fn ($title) => str_contains($title, $value))->values()->toArray() + : [] + ); + + $this->line("Your name is $name."); + } + } + ); + + $this + ->artisan('test:search') + ->expectsSearch('What is your name?', 'Jane', 'J', ['John', 'Jane', 'Jack']) + ->expectsOutput('Your name is Jane.'); + } + + public function testAssertionForMultisearchPrompt() + { + $this->app[Kernel::class]->registerCommand( + new class extends Command + { + protected $signature = 'test:multisearch'; + + public function handle() + { + $options = collect(['John', 'Jane', 'Sally', 'Jack']); + + $names = multisearch( + label: 'Which names do you like?', + options: fn (string $value) => strlen($value) > 0 + ? $options->filter(fn ($title) => str_contains($title, $value))->values()->toArray() + : [] + ); + + if (empty($names)) { + $this->line('You like nobody.'); + } else { + $this->line(sprintf('You like %s.', implode(', ', $names))); + } + } + } + ); + + $this + ->artisan('test:multisearch') + ->expectsSearch('Which names do you like?', ['John', 'Jane'], 'J', ['John', 'Jane', 'Jack']) + ->expectsOutput('You like John, Jane.'); + + $this + ->artisan('test:multisearch') + ->expectsSearch('Which names do you like?', [], 'J', ['John', 'Jane', 'Jack']) + ->expectsOutput('You like nobody.'); + } + + public function testAssertionForSelectPromptFollowedByMultisearchPrompt() + { + $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', 'Jane'] + ); + + $titles = collect(['Mr', 'Mrs', 'Ms', 'Dr']); + $title = multisearch( + label: 'What is your title?', + options: fn (string $value) => strlen($value) > 0 + ? $titles->filter(fn ($title) => str_contains($title, $value))->values()->toArray() + : [] + ); + + $this->line('I will refer to you '.$title[0].' '.$name.'.'); + } + } + ); + + $this + ->artisan('test:select') + ->expectsChoice('What is your name?', 'Jane', ['John', 'Jane']) + ->expectsSearch('What is your title?', ['Dr'], 'D', ['Dr']) + ->expectsOutput('I will refer to you Dr Jane.'); + } }