Skip to content

Commit 0ae21a3

Browse files
committed
Add tests for search and multisearch assertions
1 parent 9226bd0 commit 0ae21a3

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/Illuminate/Console/Concerns/ConfiguresPrompts.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ private function multiselectFallback($label, $options, $default = [], $required
270270
}
271271

272272
if ($required === false) {
273-
$answers = Arr::wrap($answers);
274273
return array_is_list($options)
275274
? array_values(array_filter($answers, fn ($value) => $value !== 'None'))
276275
: array_filter($answers, fn ($value) => $value !== '');

tests/Integration/Console/PromptsAssertionTest.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function Laravel\Prompts\multisearch;
1111
use function Laravel\Prompts\multiselect;
1212
use function Laravel\Prompts\password;
13+
use function Laravel\Prompts\search;
1314
use function Laravel\Prompts\select;
1415
use function Laravel\Prompts\text;
1516
use function Laravel\Prompts\textarea;
@@ -168,7 +169,8 @@ public function handle()
168169
$this
169170
->artisan('test:select')
170171
->expectsChoice('What is your name?', 'Jane', ['John', 'Jane'])
171-
->expectsChoice('What is your title?', 'Dr', ['Dr'])
172+
->expectsQuestion('What is your title?', 'D')
173+
->expectsChoice('What is your title?', ['Dr'], ['Dr'])
172174
->expectsOutput('I will refer to you Dr Jane.');
173175
}
174176

@@ -231,4 +233,74 @@ public function handle()
231233
->expectsChoice('Which names do you like?', ['None'], ['John', 'Jane', 'Sally', 'Jack'])
232234
->expectsOutput('You like nobody.');
233235
}
236+
237+
public function testAssertionForSearchPrompt()
238+
{
239+
$this->app[Kernel::class]->registerCommand(
240+
new class extends Command
241+
{
242+
protected $signature = 'test:search';
243+
244+
public function handle()
245+
{
246+
$options = collect(['John', 'Jane', 'Sally', 'Jack']);
247+
248+
$name = search(
249+
label: 'What is your name?',
250+
options: fn (string $value) => strlen($value) > 0
251+
? $options->filter(fn ($title) => str_contains($title, $value))->values()->toArray()
252+
: []
253+
);
254+
255+
$this->line("Your name is $name.");
256+
}
257+
}
258+
);
259+
260+
$this
261+
->artisan('test:search')
262+
->expectsQuestion('What is your name?', 'J')
263+
->expectsChoice('What is your name?', 'Jane', ['John', 'Jane', 'Jack'])
264+
->expectsOutput('Your name is Jane.');
265+
}
266+
267+
public function testAssertionForMultisearchPrompt()
268+
{
269+
$this->app[Kernel::class]->registerCommand(
270+
new class extends Command
271+
{
272+
protected $signature = 'test:multisearch';
273+
274+
public function handle()
275+
{
276+
$options = collect(['John', 'Jane', 'Sally', 'Jack']);
277+
278+
$names = multisearch(
279+
label: 'Which names do you like?',
280+
options: fn (string $value) => strlen($value) > 0
281+
? $options->filter(fn ($title) => str_contains($title, $value))->values()->toArray()
282+
: []
283+
);
284+
285+
if (empty($names)) {
286+
$this->line('You like nobody.');
287+
} else {
288+
$this->line(sprintf('You like %s.', implode(', ', $names)));
289+
}
290+
}
291+
}
292+
);
293+
294+
$this
295+
->artisan('test:multisearch')
296+
->expectsQuestion('Which names do you like?', 'J')
297+
->expectsChoice('Which names do you like?', ['John', 'Jane'], ['John', 'Jane', 'Jack'])
298+
->expectsOutput('You like John, Jane.');
299+
300+
$this
301+
->artisan('test:multisearch')
302+
->expectsQuestion('Which names do you like?', 'J')
303+
->expectsChoice('Which names do you like?', ['None'], ['John', 'Jane', 'Jack'])
304+
->expectsOutput('You like nobody.');
305+
}
234306
}

0 commit comments

Comments
 (0)