Skip to content

Commit 81a0b5b

Browse files
[11.x] Add expectsSearch() assertion for testing prompts that use 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>
1 parent d35f4ff commit 81a0b5b

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/Illuminate/Testing/PendingCommand.php

+16
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ public function expectsChoice($question, $answer, $answers, $strict = false)
135135
return $this->expectsQuestion($question, $answer);
136136
}
137137

138+
/**
139+
* Specify an expected search question with an expected search string, followed by an expected choice question with expected answers.
140+
*
141+
* @param string $question
142+
* @param string|array $answer
143+
* @param string $search
144+
* @param array $answers
145+
* @return $this
146+
*/
147+
public function expectsSearch($question, $answer, $search, $answers)
148+
{
149+
return $this
150+
->expectsQuestion($question, $search)
151+
->expectsChoice($question, $answer, $answers);
152+
}
153+
138154
/**
139155
* Specify output that should be printed when the command runs.
140156
*

tests/Integration/Console/PromptsAssertionTest.php

+103
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Orchestra\Testbench\TestCase;
88

99
use function Laravel\Prompts\confirm;
10+
use function Laravel\Prompts\multisearch;
1011
use function Laravel\Prompts\multiselect;
1112
use function Laravel\Prompts\password;
13+
use function Laravel\Prompts\search;
1214
use function Laravel\Prompts\select;
1315
use function Laravel\Prompts\suggest;
1416
use function Laravel\Prompts\text;
@@ -269,4 +271,105 @@ public function handle()
269271
->expectsChoice('Which names do you like?', ['None'], ['John', 'Jane', 'Sally', 'Jack'])
270272
->expectsOutput('You like nobody.');
271273
}
274+
275+
public function testAssertionForSearchPrompt()
276+
{
277+
$this->app[Kernel::class]->registerCommand(
278+
new class extends Command
279+
{
280+
protected $signature = 'test:search';
281+
282+
public function handle()
283+
{
284+
$options = collect(['John', 'Jane', 'Sally', 'Jack']);
285+
286+
$name = search(
287+
label: 'What is your name?',
288+
options: fn (string $value) => strlen($value) > 0
289+
? $options->filter(fn ($title) => str_contains($title, $value))->values()->toArray()
290+
: []
291+
);
292+
293+
$this->line("Your name is $name.");
294+
}
295+
}
296+
);
297+
298+
$this
299+
->artisan('test:search')
300+
->expectsSearch('What is your name?', 'Jane', 'J', ['John', 'Jane', 'Jack'])
301+
->expectsOutput('Your name is Jane.');
302+
}
303+
304+
public function testAssertionForMultisearchPrompt()
305+
{
306+
$this->app[Kernel::class]->registerCommand(
307+
new class extends Command
308+
{
309+
protected $signature = 'test:multisearch';
310+
311+
public function handle()
312+
{
313+
$options = collect(['John', 'Jane', 'Sally', 'Jack']);
314+
315+
$names = multisearch(
316+
label: 'Which names do you like?',
317+
options: fn (string $value) => strlen($value) > 0
318+
? $options->filter(fn ($title) => str_contains($title, $value))->values()->toArray()
319+
: []
320+
);
321+
322+
if (empty($names)) {
323+
$this->line('You like nobody.');
324+
} else {
325+
$this->line(sprintf('You like %s.', implode(', ', $names)));
326+
}
327+
}
328+
}
329+
);
330+
331+
$this
332+
->artisan('test:multisearch')
333+
->expectsSearch('Which names do you like?', ['John', 'Jane'], 'J', ['John', 'Jane', 'Jack'])
334+
->expectsOutput('You like John, Jane.');
335+
336+
$this
337+
->artisan('test:multisearch')
338+
->expectsSearch('Which names do you like?', [], 'J', ['John', 'Jane', 'Jack'])
339+
->expectsOutput('You like nobody.');
340+
}
341+
342+
public function testAssertionForSelectPromptFollowedByMultisearchPrompt()
343+
{
344+
$this->app[Kernel::class]->registerCommand(
345+
new class extends Command
346+
{
347+
protected $signature = 'test:select';
348+
349+
public function handle()
350+
{
351+
$name = select(
352+
label: 'What is your name?',
353+
options: ['John', 'Jane']
354+
);
355+
356+
$titles = collect(['Mr', 'Mrs', 'Ms', 'Dr']);
357+
$title = multisearch(
358+
label: 'What is your title?',
359+
options: fn (string $value) => strlen($value) > 0
360+
? $titles->filter(fn ($title) => str_contains($title, $value))->values()->toArray()
361+
: []
362+
);
363+
364+
$this->line('I will refer to you '.$title[0].' '.$name.'.');
365+
}
366+
}
367+
);
368+
369+
$this
370+
->artisan('test:select')
371+
->expectsChoice('What is your name?', 'Jane', ['John', 'Jane'])
372+
->expectsSearch('What is your title?', ['Dr'], 'D', ['Dr'])
373+
->expectsOutput('I will refer to you Dr Jane.');
374+
}
272375
}

0 commit comments

Comments
 (0)