Skip to content

Commit 61716c9

Browse files
[11.x] Add Prompts textarea fallback for tests and add assertion tests (#51055)
* add textarea fallback * add text and textarea assertion tests * add input tests * add tests * add multiselect test * add select test * update composer.json * styles * Use multiline fallback * Formatting --------- Co-authored-by: Jess Archer <[email protected]>
1 parent 21d7ad9 commit 61716c9

File tree

4 files changed

+182
-3
lines changed

4 files changed

+182
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"fruitcake/php-cors": "^1.3",
3232
"guzzlehttp/guzzle": "^7.8",
3333
"guzzlehttp/uri-template": "^1.0",
34-
"laravel/prompts": "^0.1.15",
34+
"laravel/prompts": "^0.1.18",
3535
"laravel/serializable-closure": "^1.3",
3636
"league/commonmark": "^2.2.1",
3737
"league/flysystem": "^3.8.0",

src/Illuminate/Console/Concerns/ConfiguresPrompts.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Laravel\Prompts\SearchPrompt;
1313
use Laravel\Prompts\SelectPrompt;
1414
use Laravel\Prompts\SuggestPrompt;
15+
use Laravel\Prompts\TextareaPrompt;
1516
use Laravel\Prompts\TextPrompt;
1617
use stdClass;
1718
use Symfony\Component\Console\Input\InputInterface;
@@ -40,6 +41,12 @@ protected function configurePrompts(InputInterface $input)
4041
$prompt->validate
4142
));
4243

44+
TextareaPrompt::fallbackUsing(fn (TextareaPrompt $prompt) => $this->promptUntilValid(
45+
fn () => $this->components->ask($prompt->label, $prompt->default ?: null, multiline: true) ?? '',
46+
$prompt->required,
47+
$prompt->validate
48+
));
49+
4350
PasswordPrompt::fallbackUsing(fn (PasswordPrompt $prompt) => $this->promptUntilValid(
4451
fn () => $this->components->secret($prompt->label) ?? '',
4552
$prompt->required,

src/Illuminate/Console/View/Components/Ask.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Console\View\Components;
44

5+
use Symfony\Component\Console\Question\Question;
6+
57
class Ask extends Component
68
{
79
/**
@@ -11,8 +13,13 @@ class Ask extends Component
1113
* @param string $default
1214
* @return mixed
1315
*/
14-
public function render($question, $default = null)
16+
public function render($question, $default = null, $multiline = false)
1517
{
16-
return $this->usingQuestionHelper(fn () => $this->output->ask($question, $default));
18+
return $this->usingQuestionHelper(
19+
fn () => $this->output->askQuestion(
20+
(new Question($question, $default))
21+
->setMultiline($multiline)
22+
)
23+
);
1724
}
1825
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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

Comments
 (0)