|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Contracts\Console\Kernel; |
| 7 | +use Orchestra\Testbench\TestCase; |
| 8 | + |
| 9 | +use function Laravel\Prompts\confirm; |
| 10 | +use function Laravel\Prompts\multiselect; |
| 11 | +use function Laravel\Prompts\password; |
| 12 | +use function Laravel\Prompts\select; |
| 13 | +use function Laravel\Prompts\text; |
| 14 | +use function Laravel\Prompts\textarea; |
| 15 | + |
| 16 | +class PromptsAssertionTest extends TestCase |
| 17 | +{ |
| 18 | + public function testAssertionForTextPrompt() |
| 19 | + { |
| 20 | + $this->app[Kernel::class]->registerCommand( |
| 21 | + new class extends Command |
| 22 | + { |
| 23 | + protected $signature = 'test:text'; |
| 24 | + |
| 25 | + public function handle() |
| 26 | + { |
| 27 | + $name = text('What is your name?', 'John'); |
| 28 | + |
| 29 | + $this->line($name); |
| 30 | + } |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + $this |
| 35 | + ->artisan('test:text') |
| 36 | + ->expectsQuestion('What is your name?', 'Jane') |
| 37 | + ->expectsOutput('Jane'); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAssertionForTextareaPrompt() |
| 41 | + { |
| 42 | + $this->app[Kernel::class]->registerCommand( |
| 43 | + new class extends Command |
| 44 | + { |
| 45 | + protected $signature = 'test:textarea'; |
| 46 | + |
| 47 | + public function handle() |
| 48 | + { |
| 49 | + $name = textarea('What is your name?', 'John'); |
| 50 | + |
| 51 | + $this->line($name); |
| 52 | + } |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + $this |
| 57 | + ->artisan('test:textarea') |
| 58 | + ->expectsQuestion('What is your name?', 'Jane') |
| 59 | + ->expectsOutput('Jane'); |
| 60 | + } |
| 61 | + |
| 62 | + public function testAssertionForPasswordPrompt() |
| 63 | + { |
| 64 | + $this->app[Kernel::class]->registerCommand( |
| 65 | + new class extends Command |
| 66 | + { |
| 67 | + protected $signature = 'test:password'; |
| 68 | + |
| 69 | + public function handle() |
| 70 | + { |
| 71 | + $name = password('What is your password?'); |
| 72 | + |
| 73 | + $this->line($name); |
| 74 | + } |
| 75 | + } |
| 76 | + ); |
| 77 | + |
| 78 | + $this |
| 79 | + ->artisan('test:password') |
| 80 | + ->expectsQuestion('What is your password?', 'secret') |
| 81 | + ->expectsOutput('secret'); |
| 82 | + } |
| 83 | + |
| 84 | + public function testAssertionForConfirmPrompt() |
| 85 | + { |
| 86 | + $this->app[Kernel::class]->registerCommand( |
| 87 | + new class extends Command |
| 88 | + { |
| 89 | + protected $signature = 'test:confirm'; |
| 90 | + |
| 91 | + public function handle() |
| 92 | + { |
| 93 | + $confirmed = confirm('Is your name John?'); |
| 94 | + |
| 95 | + if ($confirmed) { |
| 96 | + $this->line('Your name is John.'); |
| 97 | + } else { |
| 98 | + $this->line('Your name is not John.'); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + $this |
| 105 | + ->artisan('test:confirm') |
| 106 | + ->expectsConfirmation('Is your name John?', 'no') |
| 107 | + ->expectsOutput('Your name is not John.'); |
| 108 | + |
| 109 | + $this |
| 110 | + ->artisan('test:confirm') |
| 111 | + ->expectsConfirmation('Is your name John?', 'yes') |
| 112 | + ->expectsOutput('Your name is John.'); |
| 113 | + } |
| 114 | + |
| 115 | + public function testAssertionForSelectPrompt() |
| 116 | + { |
| 117 | + $this->app[Kernel::class]->registerCommand( |
| 118 | + new class extends Command |
| 119 | + { |
| 120 | + protected $signature = 'test:select'; |
| 121 | + |
| 122 | + public function handle() |
| 123 | + { |
| 124 | + $name = select( |
| 125 | + label: 'What is your name?', |
| 126 | + options: ['John', 'Jane'] |
| 127 | + ); |
| 128 | + |
| 129 | + $this->line("Your name is $name."); |
| 130 | + } |
| 131 | + } |
| 132 | + ); |
| 133 | + |
| 134 | + $this |
| 135 | + ->artisan('test:select') |
| 136 | + ->expectsChoice('What is your name?', 'Jane', ['John', 'Jane']) |
| 137 | + ->expectsOutput('Your name is Jane.'); |
| 138 | + } |
| 139 | + |
| 140 | + public function testAssertionForRequiredMultiselectPrompt() |
| 141 | + { |
| 142 | + $this->app[Kernel::class]->registerCommand( |
| 143 | + new class extends Command |
| 144 | + { |
| 145 | + protected $signature = 'test:multiselect'; |
| 146 | + |
| 147 | + public function handle() |
| 148 | + { |
| 149 | + $names = multiselect( |
| 150 | + label: 'Which names do you like?', |
| 151 | + options: ['John', 'Jane', 'Sally', 'Jack'], |
| 152 | + required: true |
| 153 | + ); |
| 154 | + |
| 155 | + $this->line(sprintf('You like %s.', implode(', ', $names))); |
| 156 | + } |
| 157 | + } |
| 158 | + ); |
| 159 | + |
| 160 | + $this |
| 161 | + ->artisan('test:multiselect') |
| 162 | + ->expectsChoice('Which names do you like?', ['John', 'Jane'], ['John', 'Jane', 'Sally', 'Jack']) |
| 163 | + ->expectsOutput('You like John, Jane.'); |
| 164 | + } |
| 165 | +} |
0 commit comments