Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Allow testing prompts validation #49447

Merged
merged 4 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!');
}
}