Skip to content

Commit

Permalink
[10.x] Allow testing prompts validation (#49447)
Browse files Browse the repository at this point in the history
* [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 <taylor@laravel.com>
  • Loading branch information
cerbero90 and taylorotwell committed Dec 23, 2023
1 parent 90ed27d commit a77d6b6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Illuminate/Console/Concerns/ConfiguresPrompts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Console\Concerns;

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

continue;
if ($this->laravel->runningUnitTests()) {
throw new PromptValidationException;
} else {
continue;
}
}

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

continue;
if ($this->laravel->runningUnitTests()) {
throw new PromptValidationException;
} else {
continue;
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/Console/PromptValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Illuminate\Console;

use RuntimeException;

class PromptValidationException extends RuntimeException
{
}
3 changes: 3 additions & 0 deletions src/Illuminate/Testing/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Testing;

use Illuminate\Console\OutputStyle;
use Illuminate\Console\PromptValidationException;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -300,6 +301,8 @@ public function run()
}

throw $e;
} catch (PromptValidationException) {
$exitCode = Command::FAILURE;
}

if ($this->expectedExitCode !== null) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Integration/Console/PromptsValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Tests\Integration\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel;
use Orchestra\Testbench\TestCase;

use function Laravel\Prompts\text;

class PromptsValidationTest extends TestCase
{
protected function defineEnvironment($app)
{
$app[Kernel::class]->registerCommand(new DummyPromptsValidationCommand());
}

public function testValidationForPrompts()
{
$this
->artisan(DummyPromptsValidationCommand::class)
->expectsQuestion('Test', 'bar')
->expectsOutputToContain('error!');
}
}

class DummyPromptsValidationCommand extends Command
{
protected $signature = 'prompts-validation-test';

public function handle()
{
text('Test', validate: fn ($value) => $value == 'foo' ? '' : 'error!');
}
}

0 comments on commit a77d6b6

Please sign in to comment.