1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
<?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());
$app[Kernel::class]->registerCommand(new DummyPromptsWithLaravelRulesCommand());
$app[Kernel::class]->registerCommand(new DummyPromptsWithLaravelRulesMessagesAndAttributesCommand());
$app[Kernel::class]->registerCommand(new DummyPromptsWithLaravelRulesCommandWithInlineMessagesAndAttributesCommand());
}
public function testValidationForPrompts()
{
$this
->artisan(DummyPromptsValidationCommand::class)
->expectsQuestion('What is your name?', '')
->expectsOutputToContain('Required!');
}
public function testValidationWithLaravelRulesAndNoCustomization()
{
$this
->artisan(DummyPromptsWithLaravelRulesCommand::class)
->expectsQuestion('What is your name?', '')
->expectsOutputToContain('The answer field is required.');
}
public function testValidationWithLaravelRulesInlineMessagesAndAttributes()
{
$this
->artisan(DummyPromptsWithLaravelRulesCommandWithInlineMessagesAndAttributesCommand::class)
->expectsQuestion('What is your name?', '')
->expectsOutputToContain('Your full name is mandatory.');
}
public function testValidationWithLaravelRulesMessagesAndAttributes()
{
$this
->artisan(DummyPromptsWithLaravelRulesMessagesAndAttributesCommand::class)
->expectsQuestion('What is your name?', '')
->expectsOutputToContain('Your full name is mandatory.');
}
}
class DummyPromptsValidationCommand extends Command
{
protected $signature = 'prompts-validation-test';
public function handle()
{
text('What is your name?', validate: fn ($value) => $value == '' ? 'Required!' : null);
}
}
class DummyPromptsWithLaravelRulesCommand extends Command
{
protected $signature = 'prompts-laravel-rules-test';
public function handle()
{
text('What is your name?', validate: 'required');
}
}
class DummyPromptsWithLaravelRulesCommandWithInlineMessagesAndAttributesCommand extends Command
{
protected $signature = 'prompts-laravel-rules-inline-test';
public function handle()
{
text('What is your name?', validate: literal(
rules: ['name' => 'required'],
messages: ['name.required' => 'Your :attribute is mandatory.'],
attributes: ['name' => 'full name'],
));
}
}
class DummyPromptsWithLaravelRulesMessagesAndAttributesCommand extends Command
{
protected $signature = 'prompts-laravel-rules-messages-attributes-test';
public function handle()
{
text('What is your name?', validate: ['name' => 'required']);
}
protected function validationMessages()
{
return ['name.required' => 'Your :attribute is mandatory.'];
}
protected function validationAttributes()
{
return ['name' => 'full name'];
}
}
|