Skip to content

Commit

Permalink
[11.x] Add expectsSearch() assertion for testing prompts that use `…
Browse files Browse the repository at this point in the history
…search()` and `multisearch()` functions (#51669)

* Update PromptsAssertionTest.php

* Update ConfiguresPrompts.php

* Update PromptsAssertionTest.php

* Add tests for search and multisearch assertions

* Formatting

* Formatting

* Add `expectsSearch` method

---------

Co-authored-by: Jess Archer <jess@jessarcher.com>
  • Loading branch information
JayBizzle and jessarcher committed Aug 19, 2024
1 parent d35f4ff commit 81a0b5b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
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\suggest;
use function Laravel\Prompts\text;
Expand Down Expand Up @@ -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.');
}
}

0 comments on commit 81a0b5b

Please sign in to comment.