From 77389f8f4e44c1df0a24172c68c05f9d012f29b9 Mon Sep 17 00:00:00 2001 From: henzeb Date: Sun, 9 Apr 2023 13:36:48 +0200 Subject: [PATCH] fix for validation --- CHANGELOG.md | 6 +++ src/Concerns/InteractsWithCommand.php | 20 --------- src/Concerns/ValidatesInput.php | 33 +++++++------- src/Output/ConsoleOutput.php | 2 - src/Providers/ConsoleServiceProvider.php | 43 +++++++------------ .../Stub/StubCommandServiceProvider.php | 21 ++++++++- .../Concerns/Stub/TestValidationCommand.php | 4 +- .../Console/Concerns/ValidatesInputTest.php | 31 ++++++++++--- 8 files changed, 84 insertions(+), 76 deletions(-) delete mode 100644 src/Concerns/InteractsWithCommand.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b06ffa..f05f451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `Laravel Console Facade` will be documented in this file +## 1.16.0 - 2023-04-09 + +- added [tail](README.md#tail) for easy scrolling functionality +- added [console](README.md#console-helper) helper function +- fixed some issues with validation + ## 1.15.0 - 2023-03-29 - added easy-to-use interface for [verbose](README.md#verbosity) output diff --git a/src/Concerns/InteractsWithCommand.php b/src/Concerns/InteractsWithCommand.php deleted file mode 100644 index 5de2d31..0000000 --- a/src/Concerns/InteractsWithCommand.php +++ /dev/null @@ -1,20 +0,0 @@ -command = $command; - } - - private function getCommand(): Command - { - return $this->command ?? (new Command())->setName('default'); - } -} diff --git a/src/Concerns/ValidatesInput.php b/src/Concerns/ValidatesInput.php index c9b8347..a0283b5 100644 --- a/src/Concerns/ValidatesInput.php +++ b/src/Concerns/ValidatesInput.php @@ -3,17 +3,27 @@ namespace Henzeb\Console\Concerns; use Closure; -use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\Validator; +use Illuminate\Support\MessageBag; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Input\Input; use Symfony\Component\Console\Input\InputDefinition; -use Symfony\Component\Console\Exception\InvalidArgumentException; trait ValidatesInput { private array $rules = []; private array $messages = []; - private string $commandToValidateWith = 'default'; + private string $command = 'default'; + + public function setCommandForValidation(string $command): void + { + $this->command = $command; + } + + private function getCommandForValidation(): string + { + return $this->command; + } private function getDefinition(): InputDefinition { @@ -26,18 +36,10 @@ function () { )(); } - private function commandToValidateWithDefault() { - $this->setCommandToValidateWith('default'); - } - - private function setCommandToValidateWith(string $command) { - $this->commandToValidateWith = $command; - } - public function validateWith(array $rules, array $messages = []): void { - $this->rules[$this->commandToValidateWith] = $rules; - $this->messages[$this->commandToValidateWith] = $messages; + $this->rules[$this->getCommandForValidation()] = $rules; + $this->messages[$this->getCommandForValidation()] = $messages; } private function getData(): array @@ -59,8 +61,7 @@ private function getData(): array public function shouldValidate(): bool { - return !empty($this->rules[get_class($this->getCommand())]) - || !empty($this->rules['default']); + return !empty($this->rules[$this->getCommandForValidation()]); } /** @@ -71,7 +72,7 @@ public function shouldValidate(): bool public function validate(): void { if ($this->shouldValidate()) { - $command = get_class($this->getCommand()); + $command = $this->getCommandForValidation(); $validator = Validator::make( $this->getData(), $this->rules[$command] ?? $this->rules['default'], diff --git a/src/Output/ConsoleOutput.php b/src/Output/ConsoleOutput.php index 4d7c2b4..e2ef4b5 100644 --- a/src/Output/ConsoleOutput.php +++ b/src/Output/ConsoleOutput.php @@ -5,7 +5,6 @@ use Closure; use Henzeb\Console\Concerns\InteractsWithArguments; -use Henzeb\Console\Concerns\InteractsWithCommand; use Henzeb\Console\Concerns\InteractsWithExit; use Henzeb\Console\Concerns\InteractsWithInfiniteLoop; use Henzeb\Console\Concerns\InteractsWithIO; @@ -31,7 +30,6 @@ class ConsoleOutput InteractsWithSleep, InteractsWithSignals, InteractsWithOptions, - InteractsWithCommand, InteractsWithArguments, InteractsWithInfiniteLoop; diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index eb529dc..cba5587 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -4,13 +4,13 @@ use Closure; use Henzeb\Console\Facades\Console; -use Henzeb\Console\Output\ConsoleOutput; use Henzeb\Console\Stores\OutputStore; use Illuminate\Console\Command; use Illuminate\Console\Events\CommandFinished; use Illuminate\Console\Events\CommandStarting; use Illuminate\Console\OutputStyle; use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; use Symfony\Component\Console\Formatter\OutputFormatterStyle; @@ -30,25 +30,13 @@ public function boot(): void App::runningUnitTests() ? new ArrayInput([]) : new ArgvInput(), new SymfonyConsoleOutput() ); - /*return resolve( - OutputStyle::class, - [ - 'input' => App::runningUnitTests() ? new ArrayInput([]) : new ArgvInput(), - 'output' => new SymfonyConsoleOutput() - ] - );*/ }); $this->afterResolvingOutputStyle(); $this->afterResolvingCommand(); - $this->listenToCommandFinished(); - } - - public function register() - { - + $this->listenToCommandEvents(); } /** @@ -76,16 +64,22 @@ function (OutputStyle $outputStyle) { private function afterResolvingCommand(): void { + $this->app->beforeResolving( + Command::class, + function (string $command) { + Console::setCommandForValidation($command); + } + ); + $this->app->afterResolving( Command::class, function (Command $command) { - Console::setCommand($command); $command->ignoreValidationErrors(); $command->setCode( Closure::bind(function (InputInterface $input, OutputInterface $output) { - Console::setCommand($this); + Console::setCommandForValidation($this::class); Console::validate(); /** @@ -103,21 +97,16 @@ function (Command $command) { ); } - private function listenToCommandFinished(): void + private function listenToCommandEvents(): void { Event::listen( CommandStarting::class, function (CommandStarting $command) { - - Closure::bind( - function (string $command = null) { - /** @var $this ConsoleOutput */ - $this->setCommandToValidateWith((string)$command); - }, - Console::getFacadeRoot(), - ConsoleOutput::class - )($command->command); - }); + Console::setCommandForValidation( + Artisan::all()[$command->command]->getName() + ); + } + ); Event::listen( CommandFinished::class, diff --git a/tests/Unit/Console/Concerns/Stub/StubCommandServiceProvider.php b/tests/Unit/Console/Concerns/Stub/StubCommandServiceProvider.php index 9e0d84d..e592dda 100644 --- a/tests/Unit/Console/Concerns/Stub/StubCommandServiceProvider.php +++ b/tests/Unit/Console/Concerns/Stub/StubCommandServiceProvider.php @@ -3,8 +3,8 @@ namespace Henzeb\Console\Tests\Unit\Console\Concerns\Stub; use Henzeb\Console\Facades\Console; -use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\ServiceProvider; class StubCommandServiceProvider extends ServiceProvider { @@ -21,11 +21,28 @@ public function register() 'test:closure {--test=}', function () { Console::validateWith([ - '--test'=>'size:2' + '--test' => 'size:2' + ]); + Console::validate(); + } + ); + + Artisan::command( + 'test:second-closure {--test=}', + function () { + Console::validateWith([ + '--test' => 'size:4' ]); Console::validate(); } ); + + Artisan::command( + 'test:no-validation-rules {--test=}', + function () { + Console::validate(); + } + ); } /** diff --git a/tests/Unit/Console/Concerns/Stub/TestValidationCommand.php b/tests/Unit/Console/Concerns/Stub/TestValidationCommand.php index 4160a37..c3e4623 100644 --- a/tests/Unit/Console/Concerns/Stub/TestValidationCommand.php +++ b/tests/Unit/Console/Concerns/Stub/TestValidationCommand.php @@ -2,8 +2,8 @@ namespace Henzeb\Console\Tests\Unit\Console\Concerns\Stub; -use Illuminate\Console\Command; use Henzeb\Console\Facades\Console; +use Illuminate\Console\Command; class TestValidationCommand extends Command { @@ -14,7 +14,7 @@ class TestValidationCommand extends Command protected function configure() { Console::validateWith([ - '--test'=>'size:2' + '--test' => 'size:3' ]); } diff --git a/tests/Unit/Console/Concerns/ValidatesInputTest.php b/tests/Unit/Console/Concerns/ValidatesInputTest.php index 3c1ef86..b12b126 100644 --- a/tests/Unit/Console/Concerns/ValidatesInputTest.php +++ b/tests/Unit/Console/Concerns/ValidatesInputTest.php @@ -2,17 +2,17 @@ namespace Henzeb\Console\Tests\Unit\Console\Concerns; -use Illuminate\Console\Parser; -use Orchestra\Testbench\TestCase; use Henzeb\Console\Facades\Console; +use Henzeb\Console\Providers\ConsoleServiceProvider; +use Henzeb\Console\Tests\Unit\Console\Concerns\Stub\StubCommandServiceProvider; use Illuminate\Console\OutputStyle; +use Illuminate\Console\Parser; use Illuminate\Support\Facades\Artisan; +use Orchestra\Testbench\TestCase; +use Symfony\Component\Console\Exception\InvalidArgumentException; +use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\ConsoleOutput; -use Symfony\Component\Console\Input\InputDefinition; -use Henzeb\Console\Providers\ConsoleServiceProvider; -use Symfony\Component\Console\Exception\InvalidArgumentException; -use Henzeb\Console\Tests\Unit\Console\Concerns\Stub\StubCommandServiceProvider; class ValidatesInputTest extends TestCase { @@ -157,19 +157,36 @@ public function testArgumentShouldNotValidateIfNotGiven() public function testAutomatedValidationFails() { $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches("/The --test .*must be 3 characters./"); Artisan::call('test:test', ['--test' => 'a']); } public function testAutomatedValidationSucceeds() { - $this->assertEquals(0, Artisan::call('test:test', ['--test' => 'ab'])); + $this->assertEquals(0, Artisan::call('test:test', ['--test' => 'abc'])); } public function testClosureCommand() { $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches("/The --test .*must be 2 characters./"); Artisan::call('test:closure', ['--test' => 'a']); } + + public function testSecondClosureCommand() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches("/The --test .*must be 4 characters./"); + + Artisan::call('test:second-closure', ['--test' => 'a']); + } + + public function testClosureWithoutValidationCommand() + { + $this->expectNotToPerformAssertions(); + + Artisan::call('test:no-validation-rules', ['--test' => 'a']); + } }