Skip to content

Commit

Permalink
fix for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Apr 9, 2023
1 parent 0e3f0aa commit 77389f8
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 76 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions src/Concerns/InteractsWithCommand.php

This file was deleted.

33 changes: 17 additions & 16 deletions src/Concerns/ValidatesInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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()]);
}

/**
Expand All @@ -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'],
Expand Down
2 changes: 0 additions & 2 deletions src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +30,6 @@ class ConsoleOutput
InteractsWithSleep,
InteractsWithSignals,
InteractsWithOptions,
InteractsWithCommand,
InteractsWithArguments,
InteractsWithInfiniteLoop;

Expand Down
43 changes: 16 additions & 27 deletions src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();

/**
Expand All @@ -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,
Expand Down
21 changes: 19 additions & 2 deletions tests/Unit/Console/Concerns/Stub/StubCommandServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Console/Concerns/Stub/TestValidationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -14,7 +14,7 @@ class TestValidationCommand extends Command
protected function configure()
{
Console::validateWith([
'--test'=>'size:2'
'--test' => 'size:3'
]);
}

Expand Down
31 changes: 24 additions & 7 deletions tests/Unit/Console/Concerns/ValidatesInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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']);
}
}

0 comments on commit 77389f8

Please sign in to comment.