|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Application; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Console\OutputStyle; |
| 8 | +use Illuminate\Console\View\Components\Factory; |
| 9 | +use Mockery as m; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Symfony\Component\Console\Input\ArrayInput; |
| 13 | +use Symfony\Component\Console\Output\NullOutput; |
| 14 | + |
| 15 | +use function Laravel\Prompts\multiselect; |
| 16 | +use function Laravel\Prompts\select; |
| 17 | + |
| 18 | +class ConfiguresPromptsTest extends TestCase |
| 19 | +{ |
| 20 | + protected function tearDown(): void |
| 21 | + { |
| 22 | + m::close(); |
| 23 | + } |
| 24 | + |
| 25 | + #[DataProvider('selectDataProvider')] |
| 26 | + public function testSelectFallback($prompt, $expectedDefault, $selection, $expectedReturn) |
| 27 | + { |
| 28 | + $command = new class($prompt) extends Command |
| 29 | + { |
| 30 | + public $answer; |
| 31 | + |
| 32 | + public function __construct(protected $prompt) |
| 33 | + { |
| 34 | + parent::__construct(); |
| 35 | + } |
| 36 | + |
| 37 | + public function handle() |
| 38 | + { |
| 39 | + $this->answer = ($this->prompt)(); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + $this->runCommand($command, fn ($components) => $components |
| 44 | + ->expects('choice') |
| 45 | + ->withArgs(fn ($question, $options, $default) => $default === $expectedDefault) |
| 46 | + ->andReturnUsing(fn ($question, $options, $default) => $options[$selection]) |
| 47 | + ); |
| 48 | + |
| 49 | + $this->assertSame($expectedReturn, $command->answer); |
| 50 | + } |
| 51 | + |
| 52 | + public static function selectDataProvider() |
| 53 | + { |
| 54 | + return [ |
| 55 | + 'list with no default' => [fn () => select('foo', ['a', 'b', 'c']), null, 1, 'b'], |
| 56 | + 'numeric keys with no default' => [fn () => select('foo', [1 => 'a', 2 => 'b', 3 => 'c']), null, 1, 2], |
| 57 | + 'assoc with no default' => [fn () => select('foo', ['a' => 'A', 'b' => 'B', 'c' => 'C']), null, 1, 'b'], |
| 58 | + 'list with default' => [fn () => select('foo', ['a', 'b', 'c'], 'b'), 1, 1, 'b'], |
| 59 | + 'numeric keys with default' => [fn () => select('foo', [1 => 'a', 2 => 'b', 3 => 'c'], 2), 1, 1, 2], |
| 60 | + 'assoc with default' => [fn () => select('foo', ['a' => 'A', 'b' => 'B', 'c' => 'C'], 'b'), 1, 1, 'b'], |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + #[DataProvider('multiselectDataProvider')] |
| 65 | + public function testMultiselectFallback($prompt, $expectedDefault, $selection, $expectedReturn) |
| 66 | + { |
| 67 | + $command = new class($prompt) extends Command |
| 68 | + { |
| 69 | + public $answer; |
| 70 | + |
| 71 | + public function __construct(protected $prompt) |
| 72 | + { |
| 73 | + parent::__construct(); |
| 74 | + } |
| 75 | + |
| 76 | + public function handle() |
| 77 | + { |
| 78 | + $this->answer = ($this->prompt)(); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + $this->runCommand($command, fn ($components) => $components |
| 83 | + ->expects('choice') |
| 84 | + ->withArgs(fn ($question, $options, $default, $multiple) => $default === $expectedDefault && $multiple === true) |
| 85 | + ->andReturnUsing(fn ($question, $options, $default, $multiple) => array_values(array_filter($options, fn ($index) => in_array($index, $selection), ARRAY_FILTER_USE_KEY))) |
| 86 | + ); |
| 87 | + |
| 88 | + $this->assertSame($expectedReturn, $command->answer); |
| 89 | + } |
| 90 | + |
| 91 | + public static function multiselectDataProvider() |
| 92 | + { |
| 93 | + return [ |
| 94 | + 'list with no default' => [fn () => multiselect('foo', ['a', 'b', 'c']), '0', [2, 3], ['b', 'c']], |
| 95 | + 'numeric keys with no default' => [fn () => multiselect('foo', [1 => 'a', 2 => 'b', 3 => 'c']), '0', [2, 3], [2, 3]], |
| 96 | + 'assoc with no default' => [fn () => multiselect('foo', ['a' => 'A', 'b' => 'B', 'c' => 'C']), '0', [2, 3], ['b', 'c']], |
| 97 | + 'list with default' => [fn () => multiselect('foo', ['a', 'b', 'c'], ['b', 'c']), '2,3', [2, 3], ['b', 'c']], |
| 98 | + 'numeric keys with default' => [fn () => multiselect('foo', [1 => 'a', 2 => 'b', 3 => 'c'], [2, 3]), '2,3', [2, 3], [2, 3]], |
| 99 | + 'assoc with default' => [fn () => multiselect('foo', ['a' => 'A', 'b' => 'B', 'c' => 'C'], ['b', 'c']), '2,3', [2, 3], ['b', 'c']], |
| 100 | + 'required list with no default' => [fn () => multiselect('foo', ['a', 'b', 'c'], required: true), null, [1, 2], ['b', 'c']], |
| 101 | + 'required numeric keys with no default' => [fn () => multiselect('foo', [1 => 'a', 2 => 'b', 3 => 'c'], required: true), null, [1, 2], [2, 3]], |
| 102 | + 'required assoc with no default' => [fn () => multiselect('foo', ['a' => 'A', 'b' => 'B', 'c' => 'C'], required: true), null, [1, 2], ['b', 'c']], |
| 103 | + 'required list with default' => [fn () => multiselect('foo', ['a', 'b', 'c'], ['b', 'c'], required: true), '1,2', [1, 2], ['b', 'c']], |
| 104 | + 'required numeric keys with default' => [fn () => multiselect('foo', [1 => 'a', 2 => 'b', 3 => 'c'], [2, 3], required: true), '1,2', [1, 2], [2, 3]], |
| 105 | + 'required assoc with default' => [fn () => multiselect('foo', ['a' => 'A', 'b' => 'B', 'c' => 'C'], ['b', 'c'], required: true), '1,2', [1, 2], ['b', 'c']], |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + protected function runCommand($command, $expectations) |
| 110 | + { |
| 111 | + $command->setLaravel($application = m::mock(Application::class)); |
| 112 | + |
| 113 | + $application->shouldReceive('make')->withArgs(fn ($abstract) => $abstract === OutputStyle::class)->andReturn($outputStyle = m::mock(OutputStyle::class)); |
| 114 | + $application->shouldReceive('make')->withArgs(fn ($abstract) => $abstract === Factory::class)->andReturn($factory = m::mock(Factory::class)); |
| 115 | + $application->shouldReceive('runningUnitTests')->andReturn(true); |
| 116 | + $application->shouldReceive('call')->with([$command, 'handle'])->andReturnUsing(fn ($callback) => call_user_func($callback)); |
| 117 | + $outputStyle->shouldReceive('newLinesWritten')->andReturn(1); |
| 118 | + |
| 119 | + $expectations($factory); |
| 120 | + |
| 121 | + $command->run(new ArrayInput([]), new NullOutput); |
| 122 | + } |
| 123 | +} |
0 commit comments