Skip to content

Commit a77d6b6

Browse files
Andrea Marco Sartoritaylorotwell
andauthored
[10.x] Allow testing prompts validation (#49447)
* [10.x] Allow testing prompts validation * [10.x] Move test * [10.x] Throw exception when testing prompt validation * Update ConfiguresPrompts.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 90ed27d commit a77d6b6

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/Illuminate/Console/Concerns/ConfiguresPrompts.php

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

33
namespace Illuminate\Console\Concerns;
44

5+
use Illuminate\Console\PromptValidationException;
56
use Laravel\Prompts\ConfirmPrompt;
67
use Laravel\Prompts\MultiSearchPrompt;
78
use Laravel\Prompts\MultiSelectPrompt;
@@ -132,7 +133,11 @@ protected function promptUntilValid($prompt, $required, $validate)
132133
if ($required && ($result === '' || $result === [] || $result === false)) {
133134
$this->components->error(is_string($required) ? $required : 'Required.');
134135

135-
continue;
136+
if ($this->laravel->runningUnitTests()) {
137+
throw new PromptValidationException;
138+
} else {
139+
continue;
140+
}
136141
}
137142

138143
if ($validate) {
@@ -141,7 +146,11 @@ protected function promptUntilValid($prompt, $required, $validate)
141146
if (is_string($error) && strlen($error) > 0) {
142147
$this->components->error($error);
143148

144-
continue;
149+
if ($this->laravel->runningUnitTests()) {
150+
throw new PromptValidationException;
151+
} else {
152+
continue;
153+
}
145154
}
146155
}
147156

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Illuminate\Console;
4+
5+
use RuntimeException;
6+
7+
class PromptValidationException extends RuntimeException
8+
{
9+
}

src/Illuminate/Testing/PendingCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Testing;
44

55
use Illuminate\Console\OutputStyle;
6+
use Illuminate\Console\PromptValidationException;
67
use Illuminate\Contracts\Console\Kernel;
78
use Illuminate\Contracts\Container\Container;
89
use Illuminate\Contracts\Support\Arrayable;
@@ -300,6 +301,8 @@ public function run()
300301
}
301302

302303
throw $e;
304+
} catch (PromptValidationException) {
305+
$exitCode = Command::FAILURE;
303306
}
304307

305308
if ($this->expectedExitCode !== null) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\text;
10+
11+
class PromptsValidationTest extends TestCase
12+
{
13+
protected function defineEnvironment($app)
14+
{
15+
$app[Kernel::class]->registerCommand(new DummyPromptsValidationCommand());
16+
}
17+
18+
public function testValidationForPrompts()
19+
{
20+
$this
21+
->artisan(DummyPromptsValidationCommand::class)
22+
->expectsQuestion('Test', 'bar')
23+
->expectsOutputToContain('error!');
24+
}
25+
}
26+
27+
class DummyPromptsValidationCommand extends Command
28+
{
29+
protected $signature = 'prompts-validation-test';
30+
31+
public function handle()
32+
{
33+
text('Test', validate: fn ($value) => $value == 'foo' ? '' : 'error!');
34+
}
35+
}

0 commit comments

Comments
 (0)