|
7 | 7 | use Orchestra\Testbench\TestCase;
|
8 | 8 |
|
9 | 9 | use function Laravel\Prompts\confirm;
|
| 10 | +use function Laravel\Prompts\multisearch; |
10 | 11 | use function Laravel\Prompts\multiselect;
|
11 | 12 | use function Laravel\Prompts\password;
|
| 13 | +use function Laravel\Prompts\search; |
12 | 14 | use function Laravel\Prompts\select;
|
13 | 15 | use function Laravel\Prompts\suggest;
|
14 | 16 | use function Laravel\Prompts\text;
|
@@ -269,4 +271,105 @@ public function handle()
|
269 | 271 | ->expectsChoice('Which names do you like?', ['None'], ['John', 'Jane', 'Sally', 'Jack'])
|
270 | 272 | ->expectsOutput('You like nobody.');
|
271 | 273 | }
|
| 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 | + } |
272 | 375 | }
|
0 commit comments