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] Add expectsSearch() assertion for testing prompts that use search() and multisearch() functions #51669

Merged
merged 7 commits into from
Aug 19, 2024
Merged
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
16 changes: 16 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,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.
*
Expand Down
103 changes: 103 additions & 0 deletions tests/Integration/Console/PromptsAssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\text;
use function Laravel\Prompts\textarea;
Expand Down Expand Up @@ -196,4 +198,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.');
}
}