From 2e68402ef4799d3d598533b6fa551033cf1cece9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 19:55:18 +0100 Subject: [PATCH 01/45] Create ValidatingCommand.php --- .../src/Console/Concerns/ValidatingCommand.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 packages/framework/src/Console/Concerns/ValidatingCommand.php diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php new file mode 100644 index 00000000000..576a4eea23f --- /dev/null +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -0,0 +1,12 @@ + Date: Wed, 23 Nov 2022 19:55:49 +0100 Subject: [PATCH 02/45] Add class PHPDoc --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 576a4eea23f..3988df0baa2 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -6,6 +6,9 @@ use LaravelZero\Framework\Commands\Command; +/** + * An extended Command class that provides validation methods. + */ class ValidatingCommand extends Command { // From 8e74930b838cba16242fc184b00a03482d8727fd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 19:59:00 +0100 Subject: [PATCH 03/45] Move method PublicationService::askWithValidation() to ValidatingCommand --- .../Commands/MakePublicationCommand.php | 9 +-- .../Commands/MakePublicationTypeCommand.php | 23 ++++---- .../Console/Concerns/ValidatingCommand.php | 59 ++++++++++++++++++- .../Publications/PublicationService.php | 52 ---------------- 4 files changed, 75 insertions(+), 68 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 9593caf7d23..b7192da7d11 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -6,6 +6,7 @@ use Exception; use Hyde\Console\Commands\Interfaces\CommandHandleInterface; +use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationFile; use Hyde\Framework\Features\Publications\PublicationService; use Illuminate\Support\Str; @@ -46,7 +47,7 @@ public function handle(): int $offset++; $this->line(" $offset: $pubType->name"); } - $selected = (int) PublicationService::askWithValidation($this, 'selected', "Publication type (1-$offset)", ['required', 'integer', "between:1,$offset"]); + $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', "Publication type (1-$offset)", ['required', 'integer', "between:1,$offset"]); $pubType = $pubTypes->{$pubTypes->keys()[$selected - 1]}; } @@ -65,7 +66,7 @@ public function handle(): int // Useful for debugging //$this->output->writeln("xxx " . $exception->getTraceAsString()); $this->output->writeln("$msg"); - $overwrite = PublicationService::askWithValidation( + $overwrite = ValidatingCommand::askWithValidation( $this, 'overwrite', 'Do you wish to overwrite the existing file (y/n)', @@ -128,7 +129,7 @@ protected function captureFieldInput(object $field, Collection $mediaFiles): str $offset = $index + 1; $this->output->writeln(" $offset: $file"); } - $selected = PublicationService::askWithValidation($this, $field->name, $field->name, ['required', 'integer', "between:1,$offset"]); + $selected = ValidatingCommand::askWithValidation($this, $field->name, $field->name, ['required', 'integer', "between:1,$offset"]); $file = $mediaFiles->{$selected - 1}; return '_media/'.Str::of($file)->after('media/')->toString(); @@ -153,7 +154,7 @@ protected function captureFieldInput(object $field, Collection $mediaFiles): str } } - return PublicationService::askWithValidation($this, $field->name, $field->name, $fieldRules); + return ValidatingCommand::askWithValidation($this, $field->name, $field->name, $fieldRules); } protected function getValidationRulesPerType(): Collection diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index ab32ada0707..9ec5c21d513 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -6,6 +6,7 @@ use Exception; use Hyde\Console\Commands\Interfaces\CommandHandleInterface; +use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationType; use Hyde\Framework\Features\Publications\PublicationService; use InvalidArgumentException; @@ -32,7 +33,7 @@ public function handle(): int $title = $this->argument('title'); if (! $title) { - $title = trim(PublicationService::askWithValidation($this, 'name', 'Publication type name', ['required', 'string'])); + $title = trim(ValidatingCommand::askWithValidation($this, 'name', 'Publication type name', ['required', 'string'])); $dirname = PublicationService::formatNameForStorage($title); if (file_exists($dirname)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); @@ -48,26 +49,26 @@ public function handle(): int $offset = $k + 1; $this->line(" $offset: $v[name]"); } - $selected = (int) PublicationService::askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); + $selected = (int)ValidatingCommand::askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); $sortField = $selected ? $fields[$selected - 1]['name'] : '__createdAt'; $this->output->writeln('Choose the default sort direction:'); $this->line(' 1 - Ascending (oldest items first if sorting by dateCreated)'); $this->line(' 2 - Descending (newest items first if sorting by dateCreated)'); - $selected = (int) PublicationService::askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); + $selected = (int)ValidatingCommand::askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); $sortDirection = match ($selected) { 1 => 'ASC', 2 => 'DESC', }; - $pageSize = (int) PublicationService::askWithValidation( + $pageSize = (int)ValidatingCommand::askWithValidation( $this, 'pageSize', 'Enter the pageSize (0 for no limit)', ['required', 'integer', 'between:0,100'], 25 ); - $prevNextLinks = (bool) PublicationService::askWithValidation( + $prevNextLinks = (bool)ValidatingCommand::askWithValidation( $this, 'prevNextLinks', 'Generate previous/next links in detail view (y/n)', @@ -82,7 +83,7 @@ public function handle(): int $this->line(" $offset: $v->name"); } } - $selected = (int) PublicationService::askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); + $selected = (int)ValidatingCommand::askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); $canonicalField = $fields[$selected - 1]['name']; try { @@ -109,7 +110,7 @@ protected function captureFieldsDefinitions(): Collection $this->output->writeln("Field #$count:"); $field = Collection::create(); - $field->name = PublicationService::askWithValidation($this, 'name', 'Field name', ['required']); + $field->name = ValidatingCommand::askWithValidation($this, 'name', 'Field name', ['required']); $this->line('Field type:'); $this->line(' 1 - String'); $this->line(' 2 - Boolean '); @@ -120,18 +121,18 @@ protected function captureFieldsDefinitions(): Collection $this->line(' 7 - Array'); $this->line(' 8 - Text'); $this->line(' 9 - Local Image'); - $type = (int) PublicationService::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); + $type = (int)ValidatingCommand::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); do { // TODO This should only be done for types that can have length restrictions right? - $field->min = PublicationService::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); - $field->max = PublicationService::askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); + $field->min = ValidatingCommand::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); + $field->max = ValidatingCommand::askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); $lengthsValid = true; if ($field->max < $field->min) { $lengthsValid = false; $this->output->warning('Field length [max] must be [>=] than [min]'); } } while (! $lengthsValid); - $addAnother = PublicationService::askWithValidation($this, 'addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'y'); + $addAnother = ValidatingCommand::askWithValidation($this, 'addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'y'); // map field choice to actual field type $field->type = match ($type) { diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 3988df0baa2..8d60b5f1f07 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -4,12 +4,69 @@ namespace Hyde\Console\Concerns; +use Hyde\Framework\Features\Publications\PublicationService; use LaravelZero\Framework\Commands\Command; +use Rgasch\Collection\Collection; +use RuntimeException; +use Illuminate\Contracts\Validation\Factory as ValidationFactory; +use function ucfirst; /** * An extended Command class that provides validation methods. */ class ValidatingCommand extends Command { - // + /** @var int How many times can the validation loop run? Guards against infinite loops. */ + protected final const RETRY_COUNT = 10; + + /** + * Ask for a CLI input value until we pass validation rules. + * + * @param \LaravelZero\Framework\Commands\Command $command + * @param string $name + * @param string $message + * @param \Rgasch\Collection\Collection|array $rules + * @param mixed|null $default + * @param bool $isBeingRetried + * @return mixed + * + * @throws RuntimeException + */ + public static function askWithValidation( + Command $command, + string $name, + string $message, + Collection|array $rules = [], + mixed $default = null, + bool $isBeingRetried = false + ): mixed { + static $tries = 0; + if (!$isBeingRetried) { + $tries = 0; + } + + if ($rules instanceof Collection) { + $rules = $rules->toArray(); + } + + $answer = $command->ask(ucfirst($message), $default); + $factory = app(ValidationFactory::class); + $validator = $factory->make([$name => $answer], [$name => $rules]); + + if ($validator->passes()) { + return $answer; + } + + foreach ($validator->errors()->all() as $error) { + $command->error($error); + } + + $tries++; + + if ($tries >= PublicationService::RETRY_COUNT) { + throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); + } + + return self::askWithValidation($command, $name, $message, $rules, isBeingRetried: true); + } } diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index e9f7b5c6229..3bb14e451c7 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -8,68 +8,16 @@ use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; use Hyde\Pages\PublicationPage; -use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Support\Str; -use LaravelZero\Framework\Commands\Command; use Rgasch\Collection\Collection; -use RuntimeException; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; -use function ucfirst; /** * @see \Hyde\Framework\Testing\Feature\Services\PublicationServiceTest */ class PublicationService { - /** @var int How many times can the validation loop run? Guards against infinite loops. */ - protected final const RETRY_COUNT = 10; - - /** - * Ask for a CLI input value until we pass validation rules. - * - * @param \LaravelZero\Framework\Commands\Command $command - * @param string $name - * @param string $message - * @param \Rgasch\Collection\Collection|array $rules - * @param mixed|null $default - * @param bool $isBeingRetried - * @return mixed - * - * @throws RuntimeException - */ - public static function askWithValidation(Command $command, string $name, string $message, Collection|array $rules = [], mixed $default = null, bool $isBeingRetried = false): mixed - { - static $tries = 0; - if (! $isBeingRetried) { - $tries = 0; - } - - if ($rules instanceof Collection) { - $rules = $rules->toArray(); - } - - $answer = $command->ask(ucfirst($message), $default); - $factory = app(ValidationFactory::class); - $validator = $factory->make([$name => $answer], [$name => $rules]); - - if ($validator->passes()) { - return $answer; - } - - foreach ($validator->errors()->all() as $error) { - $command->error($error); - } - - $tries++; - - if ($tries >= self::RETRY_COUNT) { - throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); - } - - return self::askWithValidation($command, $name, $message, $rules, isBeingRetried: true); - } - /** * Format the publication type name to a suitable representation for file storage. */ From 4c921dfbc4599703d63e9bad251c1a127a72595e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 23 Nov 2022 18:59:35 +0000 Subject: [PATCH 04/45] Apply fixes from StyleCI --- .../Console/Commands/MakePublicationTypeCommand.php | 12 ++++++------ .../src/Console/Concerns/ValidatingCommand.php | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 9ec5c21d513..841d8b3bc91 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -49,26 +49,26 @@ public function handle(): int $offset = $k + 1; $this->line(" $offset: $v[name]"); } - $selected = (int)ValidatingCommand::askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); + $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); $sortField = $selected ? $fields[$selected - 1]['name'] : '__createdAt'; $this->output->writeln('Choose the default sort direction:'); $this->line(' 1 - Ascending (oldest items first if sorting by dateCreated)'); $this->line(' 2 - Descending (newest items first if sorting by dateCreated)'); - $selected = (int)ValidatingCommand::askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); + $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); $sortDirection = match ($selected) { 1 => 'ASC', 2 => 'DESC', }; - $pageSize = (int)ValidatingCommand::askWithValidation( + $pageSize = (int) ValidatingCommand::askWithValidation( $this, 'pageSize', 'Enter the pageSize (0 for no limit)', ['required', 'integer', 'between:0,100'], 25 ); - $prevNextLinks = (bool)ValidatingCommand::askWithValidation( + $prevNextLinks = (bool) ValidatingCommand::askWithValidation( $this, 'prevNextLinks', 'Generate previous/next links in detail view (y/n)', @@ -83,7 +83,7 @@ public function handle(): int $this->line(" $offset: $v->name"); } } - $selected = (int)ValidatingCommand::askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); + $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); $canonicalField = $fields[$selected - 1]['name']; try { @@ -121,7 +121,7 @@ protected function captureFieldsDefinitions(): Collection $this->line(' 7 - Array'); $this->line(' 8 - Text'); $this->line(' 9 - Local Image'); - $type = (int)ValidatingCommand::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); + $type = (int) ValidatingCommand::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); do { // TODO This should only be done for types that can have length restrictions right? $field->min = ValidatingCommand::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 8d60b5f1f07..dd51ae702af 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -5,10 +5,10 @@ namespace Hyde\Console\Concerns; use Hyde\Framework\Features\Publications\PublicationService; +use Illuminate\Contracts\Validation\Factory as ValidationFactory; use LaravelZero\Framework\Commands\Command; use Rgasch\Collection\Collection; use RuntimeException; -use Illuminate\Contracts\Validation\Factory as ValidationFactory; use function ucfirst; /** @@ -41,7 +41,7 @@ public static function askWithValidation( bool $isBeingRetried = false ): mixed { static $tries = 0; - if (!$isBeingRetried) { + if (! $isBeingRetried) { $tries = 0; } @@ -49,8 +49,8 @@ public static function askWithValidation( $rules = $rules->toArray(); } - $answer = $command->ask(ucfirst($message), $default); - $factory = app(ValidationFactory::class); + $answer = $command->ask(ucfirst($message), $default); + $factory = app(ValidationFactory::class); $validator = $factory->make([$name => $answer], [$name => $rules]); if ($validator->passes()) { From 2195fdcd262307088a9634113cd5c9fd70d5f98c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:07:52 +0100 Subject: [PATCH 05/45] Convert static method to instance method --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index dd51ae702af..dffceddaf52 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -32,7 +32,7 @@ class ValidatingCommand extends Command * * @throws RuntimeException */ - public static function askWithValidation( + public function askWithValidation( Command $command, string $name, string $message, From 564908ec9a45f0e6a29de0376ba2162c6677f50c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:08:54 +0100 Subject: [PATCH 06/45] Publication commands extend ValidatingCommand --- .../framework/src/Console/Commands/MakePublicationCommand.php | 2 +- .../src/Console/Commands/MakePublicationTypeCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index b7192da7d11..86c6a8c6709 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -19,7 +19,7 @@ * * @see \Hyde\Framework\Testing\Feature\Commands\MakePublicationCommandTest */ -class MakePublicationCommand extends Command implements CommandHandleInterface +class MakePublicationCommand extends ValidatingCommand implements CommandHandleInterface { /** @var string */ protected $signature = 'make:publication diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 841d8b3bc91..c1d520b597f 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -18,7 +18,7 @@ * * @see \Hyde\Framework\Testing\Feature\Commands\MakePublicationTypeCommandTest */ -class MakePublicationTypeCommand extends Command implements CommandHandleInterface +class MakePublicationTypeCommand extends ValidatingCommand implements CommandHandleInterface { /** @var string */ protected $signature = 'make:publicationType From da752d20f15f43d4e8fe212a3c5755acd222a9cc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:10:06 +0100 Subject: [PATCH 07/45] Use $this-> instead of calling base class method statically --- .../Commands/MakePublicationCommand.php | 8 +++---- .../Commands/MakePublicationTypeCommand.php | 22 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 86c6a8c6709..6aaef8e7f1a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -47,7 +47,7 @@ public function handle(): int $offset++; $this->line(" $offset: $pubType->name"); } - $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', "Publication type (1-$offset)", ['required', 'integer', "between:1,$offset"]); + $selected = (int) $this->askWithValidation($this, 'selected', "Publication type (1-$offset)", ['required', 'integer', "between:1,$offset"]); $pubType = $pubTypes->{$pubTypes->keys()[$selected - 1]}; } @@ -66,7 +66,7 @@ public function handle(): int // Useful for debugging //$this->output->writeln("xxx " . $exception->getTraceAsString()); $this->output->writeln("$msg"); - $overwrite = ValidatingCommand::askWithValidation( + $overwrite = $this->askWithValidation( $this, 'overwrite', 'Do you wish to overwrite the existing file (y/n)', @@ -129,7 +129,7 @@ protected function captureFieldInput(object $field, Collection $mediaFiles): str $offset = $index + 1; $this->output->writeln(" $offset: $file"); } - $selected = ValidatingCommand::askWithValidation($this, $field->name, $field->name, ['required', 'integer', "between:1,$offset"]); + $selected = $this->askWithValidation($this, $field->name, $field->name, ['required', 'integer', "between:1,$offset"]); $file = $mediaFiles->{$selected - 1}; return '_media/'.Str::of($file)->after('media/')->toString(); @@ -154,7 +154,7 @@ protected function captureFieldInput(object $field, Collection $mediaFiles): str } } - return ValidatingCommand::askWithValidation($this, $field->name, $field->name, $fieldRules); + return $this->askWithValidation($this, $field->name, $field->name, $fieldRules); } protected function getValidationRulesPerType(): Collection diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index c1d520b597f..f13f9c0e1b3 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -33,7 +33,7 @@ public function handle(): int $title = $this->argument('title'); if (! $title) { - $title = trim(ValidatingCommand::askWithValidation($this, 'name', 'Publication type name', ['required', 'string'])); + $title = trim($this->askWithValidation($this, 'name', 'Publication type name', ['required', 'string'])); $dirname = PublicationService::formatNameForStorage($title); if (file_exists($dirname)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); @@ -49,26 +49,26 @@ public function handle(): int $offset = $k + 1; $this->line(" $offset: $v[name]"); } - $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); + $selected = (int) $this->askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); $sortField = $selected ? $fields[$selected - 1]['name'] : '__createdAt'; $this->output->writeln('Choose the default sort direction:'); $this->line(' 1 - Ascending (oldest items first if sorting by dateCreated)'); $this->line(' 2 - Descending (newest items first if sorting by dateCreated)'); - $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); + $selected = (int) $this->askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); $sortDirection = match ($selected) { 1 => 'ASC', 2 => 'DESC', }; - $pageSize = (int) ValidatingCommand::askWithValidation( + $pageSize = (int) $this->askWithValidation( $this, 'pageSize', 'Enter the pageSize (0 for no limit)', ['required', 'integer', 'between:0,100'], 25 ); - $prevNextLinks = (bool) ValidatingCommand::askWithValidation( + $prevNextLinks = (bool) $this->askWithValidation( $this, 'prevNextLinks', 'Generate previous/next links in detail view (y/n)', @@ -83,7 +83,7 @@ public function handle(): int $this->line(" $offset: $v->name"); } } - $selected = (int) ValidatingCommand::askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); + $selected = (int) $this->askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); $canonicalField = $fields[$selected - 1]['name']; try { @@ -110,7 +110,7 @@ protected function captureFieldsDefinitions(): Collection $this->output->writeln("Field #$count:"); $field = Collection::create(); - $field->name = ValidatingCommand::askWithValidation($this, 'name', 'Field name', ['required']); + $field->name = $this->askWithValidation($this, 'name', 'Field name', ['required']); $this->line('Field type:'); $this->line(' 1 - String'); $this->line(' 2 - Boolean '); @@ -121,18 +121,18 @@ protected function captureFieldsDefinitions(): Collection $this->line(' 7 - Array'); $this->line(' 8 - Text'); $this->line(' 9 - Local Image'); - $type = (int) ValidatingCommand::askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); + $type = (int) $this->askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); do { // TODO This should only be done for types that can have length restrictions right? - $field->min = ValidatingCommand::askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); - $field->max = ValidatingCommand::askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); + $field->min = $this->askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); + $field->max = $this->askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); $lengthsValid = true; if ($field->max < $field->min) { $lengthsValid = false; $this->output->warning('Field length [max] must be [>=] than [min]'); } } while (! $lengthsValid); - $addAnother = ValidatingCommand::askWithValidation($this, 'addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'y'); + $addAnother = $this->askWithValidation($this, 'addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'y'); // map field choice to actual field type $field->type = match ($type) { From 64d9fa8f5dea1c608bdd035d4e2ae244eca94852 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:12:57 +0100 Subject: [PATCH 08/45] Use $this instead of $command --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index dffceddaf52..7bd253c3828 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -49,7 +49,7 @@ public function askWithValidation( $rules = $rules->toArray(); } - $answer = $command->ask(ucfirst($message), $default); + $answer = $this->ask(ucfirst($message), $default); $factory = app(ValidationFactory::class); $validator = $factory->make([$name => $answer], [$name => $rules]); @@ -58,7 +58,7 @@ public function askWithValidation( } foreach ($validator->errors()->all() as $error) { - $command->error($error); + $this->error($error); } $tries++; From 6a9988ecfab0d4590b62cc942fa7247ded2f5b67 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:13:06 +0100 Subject: [PATCH 09/45] Remove redundant self-referencing parameter $command --- .../Commands/MakePublicationCommand.php | 7 +++---- .../Commands/MakePublicationTypeCommand.php | 20 +++++++++---------- .../Console/Concerns/ValidatingCommand.php | 4 +--- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 6aaef8e7f1a..0108a98d13d 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -47,7 +47,7 @@ public function handle(): int $offset++; $this->line(" $offset: $pubType->name"); } - $selected = (int) $this->askWithValidation($this, 'selected', "Publication type (1-$offset)", ['required', 'integer', "between:1,$offset"]); + $selected = (int) $this->askWithValidation('selected', "Publication type (1-$offset)", ['required', 'integer', "between:1,$offset"]); $pubType = $pubTypes->{$pubTypes->keys()[$selected - 1]}; } @@ -67,7 +67,6 @@ public function handle(): int //$this->output->writeln("xxx " . $exception->getTraceAsString()); $this->output->writeln("$msg"); $overwrite = $this->askWithValidation( - $this, 'overwrite', 'Do you wish to overwrite the existing file (y/n)', ['required', 'string', 'in:y,n'], @@ -129,7 +128,7 @@ protected function captureFieldInput(object $field, Collection $mediaFiles): str $offset = $index + 1; $this->output->writeln(" $offset: $file"); } - $selected = $this->askWithValidation($this, $field->name, $field->name, ['required', 'integer', "between:1,$offset"]); + $selected = $this->askWithValidation($field->name, $field->name, ['required', 'integer', "between:1,$offset"]); $file = $mediaFiles->{$selected - 1}; return '_media/'.Str::of($file)->after('media/')->toString(); @@ -154,7 +153,7 @@ protected function captureFieldInput(object $field, Collection $mediaFiles): str } } - return $this->askWithValidation($this, $field->name, $field->name, $fieldRules); + return $this->askWithValidation($field->name, $field->name, $fieldRules); } protected function getValidationRulesPerType(): Collection diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index f13f9c0e1b3..5c3dbf16811 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -33,7 +33,7 @@ public function handle(): int $title = $this->argument('title'); if (! $title) { - $title = trim($this->askWithValidation($this, 'name', 'Publication type name', ['required', 'string'])); + $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); $dirname = PublicationService::formatNameForStorage($title); if (file_exists($dirname)) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); @@ -49,27 +49,25 @@ public function handle(): int $offset = $k + 1; $this->line(" $offset: $v[name]"); } - $selected = (int) $this->askWithValidation($this, 'selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); + $selected = (int) $this->askWithValidation('selected', "Sort field (0-$offset)", ['required', 'integer', "between:0,$offset"], 0); $sortField = $selected ? $fields[$selected - 1]['name'] : '__createdAt'; $this->output->writeln('Choose the default sort direction:'); $this->line(' 1 - Ascending (oldest items first if sorting by dateCreated)'); $this->line(' 2 - Descending (newest items first if sorting by dateCreated)'); - $selected = (int) $this->askWithValidation($this, 'selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); + $selected = (int) $this->askWithValidation('selected', 'Sort field (1-2)', ['required', 'integer', 'between:1,2'], 2); $sortDirection = match ($selected) { 1 => 'ASC', 2 => 'DESC', }; $pageSize = (int) $this->askWithValidation( - $this, 'pageSize', 'Enter the pageSize (0 for no limit)', ['required', 'integer', 'between:0,100'], 25 ); $prevNextLinks = (bool) $this->askWithValidation( - $this, 'prevNextLinks', 'Generate previous/next links in detail view (y/n)', ['required', 'string', 'in:y,n'], @@ -83,7 +81,7 @@ public function handle(): int $this->line(" $offset: $v->name"); } } - $selected = (int) $this->askWithValidation($this, 'selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); + $selected = (int) $this->askWithValidation('selected', "Canonical field (1-$offset)", ['required', 'integer', "between:1,$offset"], 1); $canonicalField = $fields[$selected - 1]['name']; try { @@ -110,7 +108,7 @@ protected function captureFieldsDefinitions(): Collection $this->output->writeln("Field #$count:"); $field = Collection::create(); - $field->name = $this->askWithValidation($this, 'name', 'Field name', ['required']); + $field->name = $this->askWithValidation('name', 'Field name', ['required']); $this->line('Field type:'); $this->line(' 1 - String'); $this->line(' 2 - Boolean '); @@ -121,18 +119,18 @@ protected function captureFieldsDefinitions(): Collection $this->line(' 7 - Array'); $this->line(' 8 - Text'); $this->line(' 9 - Local Image'); - $type = (int) $this->askWithValidation($this, 'type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); + $type = (int) $this->askWithValidation('type', 'Field type (1-9)', ['required', 'integer', 'between:1,9'], 1); do { // TODO This should only be done for types that can have length restrictions right? - $field->min = $this->askWithValidation($this, 'min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); - $field->max = $this->askWithValidation($this, 'max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); + $field->min = $this->askWithValidation('min', 'Min value (for strings, this refers to string length)', ['required', 'string'], 0); + $field->max = $this->askWithValidation('max', 'Max value (for strings, this refers to string length)', ['required', 'string'], 0); $lengthsValid = true; if ($field->max < $field->min) { $lengthsValid = false; $this->output->warning('Field length [max] must be [>=] than [min]'); } } while (! $lengthsValid); - $addAnother = $this->askWithValidation($this, 'addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'y'); + $addAnother = $this->askWithValidation('addAnother', 'Add another field (y/n)', ['required', 'string', 'in:y,n'], 'y'); // map field choice to actual field type $field->type = match ($type) { diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 7bd253c3828..28838ab144c 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -22,7 +22,6 @@ class ValidatingCommand extends Command /** * Ask for a CLI input value until we pass validation rules. * - * @param \LaravelZero\Framework\Commands\Command $command * @param string $name * @param string $message * @param \Rgasch\Collection\Collection|array $rules @@ -33,7 +32,6 @@ class ValidatingCommand extends Command * @throws RuntimeException */ public function askWithValidation( - Command $command, string $name, string $message, Collection|array $rules = [], @@ -67,6 +65,6 @@ public function askWithValidation( throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return self::askWithValidation($command, $name, $message, $rules, isBeingRetried: true); + return self::askWithValidation($name, $message, $rules, default: true, isBeingRetried: true); } } From 4c0a6e047745077b8749f840a96f68a010532d7d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:13:51 +0100 Subject: [PATCH 10/45] Use $this-> instead of self:: for non-static method --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 28838ab144c..e399e6127a2 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -65,6 +65,6 @@ public function askWithValidation( throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return self::askWithValidation($name, $message, $rules, default: true, isBeingRetried: true); + return $this->askWithValidation($name, $message, $rules, default: true, isBeingRetried: true); } } From aba7a7008aec769d9228a23238b0bfbe01048b5f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:14:22 +0100 Subject: [PATCH 11/45] Create ValidatingCommandTest.php Update ValidatingCommand.php --- .../src/Console/Concerns/ValidatingCommand.php | 2 ++ .../tests/Feature/ValidatingCommandTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 packages/framework/tests/Feature/ValidatingCommandTest.php diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index e399e6127a2..81b2bb24ab1 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -13,6 +13,8 @@ /** * An extended Command class that provides validation methods. + * + * @see \Hyde\Framework\Testing\Feature\ValidatingCommandTest */ class ValidatingCommand extends Command { diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php new file mode 100644 index 00000000000..10e76b5a552 --- /dev/null +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -0,0 +1,16 @@ + Date: Wed, 23 Nov 2022 19:16:59 +0000 Subject: [PATCH 12/45] Apply fixes from StyleCI --- packages/framework/tests/Feature/ValidatingCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 10e76b5a552..e7e2cb9c1c6 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Feature; -use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Testing\TestCase; /** From b0e9158562d703c383292f14ccbde608a9283275 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:17:42 +0100 Subject: [PATCH 13/45] Fix constant access call --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 81b2bb24ab1..476b7c1ffa2 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -63,7 +63,7 @@ public function askWithValidation( $tries++; - if ($tries >= PublicationService::RETRY_COUNT) { + if ($tries >= self::RETRY_COUNT) { throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } From c6c290b6909c74d03e82c2cf901fe8bac6623e81 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:21:02 +0100 Subject: [PATCH 14/45] Type hint Arrayable contract for greater compatibility --- .../framework/src/Console/Concerns/ValidatingCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 476b7c1ffa2..6d8520f3c66 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -5,9 +5,9 @@ namespace Hyde\Console\Concerns; use Hyde\Framework\Features\Publications\PublicationService; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Validation\Factory as ValidationFactory; use LaravelZero\Framework\Commands\Command; -use Rgasch\Collection\Collection; use RuntimeException; use function ucfirst; @@ -26,7 +26,7 @@ class ValidatingCommand extends Command * * @param string $name * @param string $message - * @param \Rgasch\Collection\Collection|array $rules + * @param \Illuminate\Contracts\Support\Arrayable|array $rules * @param mixed|null $default * @param bool $isBeingRetried * @return mixed @@ -36,7 +36,7 @@ class ValidatingCommand extends Command public function askWithValidation( string $name, string $message, - Collection|array $rules = [], + Arrayable|array $rules = [], mixed $default = null, bool $isBeingRetried = false ): mixed { @@ -45,7 +45,7 @@ public function askWithValidation( $tries = 0; } - if ($rules instanceof Collection) { + if ($rules instanceof Arrayable) { $rules = $rules->toArray(); } From 6c384fea3e4d788f89ded039bbc56137fde8828f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 23 Nov 2022 19:27:02 +0000 Subject: [PATCH 15/45] Apply fixes from StyleCI --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 6d8520f3c66..eb8cc972cd4 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -4,7 +4,6 @@ namespace Hyde\Console\Concerns; -use Hyde\Framework\Features\Publications\PublicationService; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Validation\Factory as ValidationFactory; use LaravelZero\Framework\Commands\Command; From 171fde69dee07f245c29a1a6f50d23cd56f924f8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:38:53 +0100 Subject: [PATCH 16/45] Start sketching mocks for test --- .../tests/Feature/ValidatingCommandTest.php | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index e7e2cb9c1c6..ab3fec5d24a 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -4,12 +4,48 @@ namespace Hyde\Framework\Testing\Feature; +use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Testing\TestCase; +use Rgasch\Collection\Collection; + +use Illuminate\Console\Application; +use Illuminate\Console\Command; +use Illuminate\Console\OutputStyle; +use Illuminate\Console\View\Components\Factory; +use Mockery; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\Console\Question\ChoiceQuestion; /** * @covers \Hyde\Console\Concerns\ValidatingCommand */ class ValidatingCommandTest extends TestCase { - // + public function testAskWithValidation() + { + $command = new class extends ValidatingCommand { + public function handle() + { + $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + } + }; + + $output = Mockery::mock(OutputStyle::class); + + $output->shouldReceive('ask')->once()->withArgs(function (string $question) { + return $question === 'What is your name?'; + }); + + $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { + return $message === 'validation.required'; + }); + + $command->setOutput($output); + + $command->handle(); + } } From 7caa60f296eba27df63a612acf2bfb95f3d6c332 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:49:23 +0100 Subject: [PATCH 17/45] Default should be null --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index eb8cc972cd4..3067e472f32 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -66,6 +66,6 @@ public function askWithValidation( throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return $this->askWithValidation($name, $message, $rules, default: true, isBeingRetried: true); + return $this->askWithValidation($name, $message, $rules, default: null, isBeingRetried: true); } } From 165e39eadcd136e030713b2ee0bb255faa5d0491 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 20:49:27 +0100 Subject: [PATCH 18/45] Remove redundant argument --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 3067e472f32..11a712ba3ce 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -66,6 +66,6 @@ public function askWithValidation( throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return $this->askWithValidation($name, $message, $rules, default: null, isBeingRetried: true); + return $this->askWithValidation($name, $message, $rules, isBeingRetried: true); } } From 43f32516a1fc53caef943a04417b36eaea398703 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:00:18 +0100 Subject: [PATCH 19/45] Finalize the test method --- .../framework/tests/Feature/ValidatingCommandTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index ab3fec5d24a..80db2020010 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -30,7 +30,8 @@ public function testAskWithValidation() $command = new class extends ValidatingCommand { public function handle() { - $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $this->output->writeln("Hello $name!"); } }; @@ -38,14 +39,13 @@ public function handle() $output->shouldReceive('ask')->once()->withArgs(function (string $question) { return $question === 'What is your name?'; - }); + })->andReturn('John Doe'); $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'validation.required'; + return $message === 'Hello John Doe!'; }); $command->setOutput($output); - $command->handle(); } } From 0207462ce794bdb6c9e3f5de41dcfaeb723db7db Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:00:39 +0100 Subject: [PATCH 20/45] Differentiate input from default --- packages/framework/tests/Feature/ValidatingCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 80db2020010..9bc9a5cb3e9 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -39,10 +39,10 @@ public function handle() $output->shouldReceive('ask')->once()->withArgs(function (string $question) { return $question === 'What is your name?'; - })->andReturn('John Doe'); + })->andReturn('Jane Doe'); $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'Hello John Doe!'; + return $message === 'Hello Jane Doe!'; }); $command->setOutput($output); From 632209d8de2b46fa2e255207a32d4530e01feaf8 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 23 Nov 2022 20:00:51 +0000 Subject: [PATCH 21/45] Apply fixes from StyleCI --- .../tests/Feature/ValidatingCommandTest.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 9bc9a5cb3e9..027baffb547 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -6,19 +6,8 @@ use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Testing\TestCase; -use Rgasch\Collection\Collection; - -use Illuminate\Console\Application; -use Illuminate\Console\Command; use Illuminate\Console\OutputStyle; -use Illuminate\Console\View\Components\Factory; use Mockery; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\NullOutput; -use Symfony\Component\Console\Question\ChoiceQuestion; /** * @covers \Hyde\Console\Concerns\ValidatingCommand @@ -27,7 +16,8 @@ class ValidatingCommandTest extends TestCase { public function testAskWithValidation() { - $command = new class extends ValidatingCommand { + $command = new class extends ValidatingCommand + { public function handle() { $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); From 7b042171850adafeb52b6268d26f3d45454fc979 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:02:58 +0100 Subject: [PATCH 22/45] Specify test method name --- packages/framework/tests/Feature/ValidatingCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 027baffb547..f0dd9681914 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -14,7 +14,7 @@ */ class ValidatingCommandTest extends TestCase { - public function testAskWithValidation() + public function testAskWithValidationCapturesInput() { $command = new class extends ValidatingCommand { From 2e8c93c01dcfb98b5b20c526b1c7a3b5c084ea24 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:06:16 +0100 Subject: [PATCH 23/45] Test retried validation --- .../tests/Feature/ValidatingCommandTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index f0dd9681914..39dbeda44ea 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -38,4 +38,33 @@ public function handle() $command->setOutput($output); $command->handle(); } + + public function testAskWithValidationRetries() + { + $command = new class extends ValidatingCommand + { + public function handle() + { + $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $this->output->writeln("Hello $name!"); + } + }; + + $output = Mockery::mock(OutputStyle::class); + + $output->shouldReceive('ask')->times(2)->withArgs(function (string $question) { + return $question === 'What is your name?'; + })->andReturn('', 'Jane Doe'); + + $output->shouldReceive('writeln')->times(1)->withArgs(function (string $message) { + return $message === 'validation.required'; + }); + + $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { + return $message === 'Hello Jane Doe!'; + }); + + $command->setOutput($output); + $command->handle(); + } } From 27b90c0ec22b2af5a0b305d3b6131ed5182e90ee Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:07:24 +0100 Subject: [PATCH 24/45] Test validation timeout --- .../tests/Feature/ValidatingCommandTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 39dbeda44ea..0da7f21d2d5 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -8,6 +8,7 @@ use Hyde\Testing\TestCase; use Illuminate\Console\OutputStyle; use Mockery; +use RuntimeException; /** * @covers \Hyde\Console\Concerns\ValidatingCommand @@ -67,4 +68,32 @@ public function handle() $command->setOutput($output); $command->handle(); } + + public function testAskWithValidationRetriesTooManyTimes() + { + $command = new class extends ValidatingCommand + { + public function handle() + { + $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $this->output->writeln("Hello $name!"); + } + }; + + $output = Mockery::mock(OutputStyle::class); + + $output->shouldReceive('ask')->times(10)->withArgs(function (string $question) { + return $question === 'What is your name?'; + })->andReturn(''); + + $output->shouldReceive('writeln')->times(10)->withArgs(function (string $message) { + return $message === 'validation.required'; + }); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage("Too many validation errors trying to validate 'name' with rules: [required]"); + + $command->setOutput($output); + $command->handle(); + } } From 48e0acbe68665f0879226f0ef27d71e017f817f6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:09:01 +0100 Subject: [PATCH 25/45] Document safeguard --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 11a712ba3ce..e2986a7133c 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -63,6 +63,7 @@ public function askWithValidation( $tries++; if ($tries >= self::RETRY_COUNT) { + // Prevent infinite loops that may happen, for example when testing. throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } From 2983bf682bbf09098c06ed1b8e9536d8a49cba38 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:10:20 +0100 Subject: [PATCH 26/45] Increase retry count to further not disturb normal usage --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 4 ++-- packages/framework/tests/Feature/ValidatingCommandTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index e2986a7133c..63b61016be4 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -18,7 +18,7 @@ class ValidatingCommand extends Command { /** @var int How many times can the validation loop run? Guards against infinite loops. */ - protected final const RETRY_COUNT = 10; + protected final const RETRY_COUNT = 30; /** * Ask for a CLI input value until we pass validation rules. @@ -63,7 +63,7 @@ public function askWithValidation( $tries++; if ($tries >= self::RETRY_COUNT) { - // Prevent infinite loops that may happen, for example when testing. + // Prevent infinite loops that may happen, for example when testing. The retry count is high enough to not affect normal usage. throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 0da7f21d2d5..ee13665e7e6 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -82,11 +82,11 @@ public function handle() $output = Mockery::mock(OutputStyle::class); - $output->shouldReceive('ask')->times(10)->withArgs(function (string $question) { + $output->shouldReceive('ask')->times(30)->withArgs(function (string $question) { return $question === 'What is your name?'; })->andReturn(''); - $output->shouldReceive('writeln')->times(10)->withArgs(function (string $message) { + $output->shouldReceive('writeln')->times(30)->withArgs(function (string $message) { return $message === 'validation.required'; }); From 2f9df0fb30a5997c8dc739abaa0609436849e5af Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:13:43 +0100 Subject: [PATCH 27/45] Refactor to handle recursion safeguard within the method call instead of via static variable --- .../src/Console/Concerns/ValidatingCommand.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 63b61016be4..b517f5b1235 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -27,7 +27,7 @@ class ValidatingCommand extends Command * @param string $message * @param \Illuminate\Contracts\Support\Arrayable|array $rules * @param mixed|null $default - * @param bool $isBeingRetried + * @param int $retryCount * @return mixed * * @throws RuntimeException @@ -37,13 +37,8 @@ public function askWithValidation( string $message, Arrayable|array $rules = [], mixed $default = null, - bool $isBeingRetried = false + int $retryCount = 0 ): mixed { - static $tries = 0; - if (! $isBeingRetried) { - $tries = 0; - } - if ($rules instanceof Arrayable) { $rules = $rules->toArray(); } @@ -60,13 +55,13 @@ public function askWithValidation( $this->error($error); } - $tries++; + $retryCount++; - if ($tries >= self::RETRY_COUNT) { + if ($retryCount >= self::RETRY_COUNT) { // Prevent infinite loops that may happen, for example when testing. The retry count is high enough to not affect normal usage. throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return $this->askWithValidation($name, $message, $rules, isBeingRetried: true); + return $this->askWithValidation($name, $message, $rules, $retryCount); } } From 54d022e3d50f6d14eb2da0624edf267425c00e06 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:14:32 +0100 Subject: [PATCH 28/45] Document parameter variable --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index b517f5b1235..9989053b471 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -27,7 +27,7 @@ class ValidatingCommand extends Command * @param string $message * @param \Illuminate\Contracts\Support\Arrayable|array $rules * @param mixed|null $default - * @param int $retryCount + * @param int $retryCount How many times has the validation loop run? * @return mixed * * @throws RuntimeException From 3dbdd4cdf5ffde3e7efb8a2b34d661a8c38a5897 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:14:58 +0100 Subject: [PATCH 29/45] Use simpler wording for code documentation --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 9989053b471..10ae51d799d 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -27,7 +27,7 @@ class ValidatingCommand extends Command * @param string $message * @param \Illuminate\Contracts\Support\Arrayable|array $rules * @param mixed|null $default - * @param int $retryCount How many times has the validation loop run? + * @param int $retryCount How many times has the question been asked? * @return mixed * * @throws RuntimeException From 5af712ec41bde15649545b8d661c574adf2169d4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:15:26 +0100 Subject: [PATCH 30/45] Rename protected internal constant to differentiate it from variable --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 10ae51d799d..e620a7dd3f6 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -18,7 +18,7 @@ class ValidatingCommand extends Command { /** @var int How many times can the validation loop run? Guards against infinite loops. */ - protected final const RETRY_COUNT = 30; + protected final const MAX_RETRIES = 30; /** * Ask for a CLI input value until we pass validation rules. @@ -57,7 +57,7 @@ public function askWithValidation( $retryCount++; - if ($retryCount >= self::RETRY_COUNT) { + if ($retryCount >= self::MAX_RETRIES) { // Prevent infinite loops that may happen, for example when testing. The retry count is high enough to not affect normal usage. throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } From 65596bfc77c26e3a2a86367e12ab19acb07683aa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:16:38 +0100 Subject: [PATCH 31/45] Fix missed default argument in refactor --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index e620a7dd3f6..fdf68f4b7c6 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -62,6 +62,6 @@ public function askWithValidation( throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return $this->askWithValidation($name, $message, $rules, $retryCount); + return $this->askWithValidation($name, $message, $rules, null, $retryCount); } } From eeacc609cbbf65b7c1f3062d5ae7846b010eb794 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 23 Nov 2022 20:19:40 +0000 Subject: [PATCH 32/45] Apply fixes from StyleCI --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index fdf68f4b7c6..19208460462 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -27,7 +27,7 @@ class ValidatingCommand extends Command * @param string $message * @param \Illuminate\Contracts\Support\Arrayable|array $rules * @param mixed|null $default - * @param int $retryCount How many times has the question been asked? + * @param int $retryCount How many times has the question been asked? * @return mixed * * @throws RuntimeException From 50fe4b0ebc4b99cd924fddb00bafcad44bceb85f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 21:20:44 +0100 Subject: [PATCH 33/45] Rename parameter $message to $question to match base implementation --- .../framework/src/Console/Concerns/ValidatingCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 19208460462..14a168c9f86 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -24,7 +24,7 @@ class ValidatingCommand extends Command * Ask for a CLI input value until we pass validation rules. * * @param string $name - * @param string $message + * @param string $question * @param \Illuminate\Contracts\Support\Arrayable|array $rules * @param mixed|null $default * @param int $retryCount How many times has the question been asked? @@ -34,7 +34,7 @@ class ValidatingCommand extends Command */ public function askWithValidation( string $name, - string $message, + string $question, Arrayable|array $rules = [], mixed $default = null, int $retryCount = 0 @@ -43,7 +43,7 @@ public function askWithValidation( $rules = $rules->toArray(); } - $answer = $this->ask(ucfirst($message), $default); + $answer = $this->ask(ucfirst($question), $default); $factory = app(ValidationFactory::class); $validator = $factory->make([$name => $answer], [$name => $rules]); @@ -62,6 +62,6 @@ public function askWithValidation( throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); } - return $this->askWithValidation($name, $message, $rules, null, $retryCount); + return $this->askWithValidation($name, $question, $rules, null, $retryCount); } } From 42a32a629771262e708cbcf09c569cece0cca215 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:42:12 +0100 Subject: [PATCH 34/45] Use the Validator facade as it is mockable --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 14a168c9f86..7c5e764ffe8 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Validation\Factory as ValidationFactory; +use Illuminate\Support\Facades\Validator; use LaravelZero\Framework\Commands\Command; use RuntimeException; use function ucfirst; @@ -44,8 +45,7 @@ public function askWithValidation( } $answer = $this->ask(ucfirst($question), $default); - $factory = app(ValidationFactory::class); - $validator = $factory->make([$name => $answer], [$name => $rules]); + $validator = Validator::make([$name => $answer], [$name => $rules]); if ($validator->passes()) { return $answer; From 99fae3514ec0994eda0d659440085d2040320f18 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:42:26 +0100 Subject: [PATCH 35/45] Run assertions on the validator --- .../tests/Feature/ValidatingCommandTest.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index ee13665e7e6..2b0cac2b675 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -7,6 +7,8 @@ use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Testing\TestCase; use Illuminate\Console\OutputStyle; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\MessageBag; use Mockery; use RuntimeException; @@ -96,4 +98,35 @@ public function handle() $command->setOutput($output); $command->handle(); } + + public function testValidationIsCalled() + { + $command = new class extends ValidatingCommand + { + public function handle() + { + $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $this->output->writeln("Hello $name!"); + } + }; + + $output = Mockery::mock(OutputStyle::class); + + $output->shouldReceive('ask')->once()->withArgs(function (string $question) { + return $question === 'What is your name?'; + })->andReturn('Jane Doe'); + + $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { + return $message === 'Hello Jane Doe!'; + }); + + $validator = Validator::spy(); + $validator->shouldReceive('make')->once()->withArgs(function (array $data) { + return $data === ['name' => 'Jane Doe']; + })->andReturn($validator); + $validator->shouldReceive('passes')->once()->andReturn(true); + + $command->setOutput($output); + $command->handle(); + } } From a44ea9aceb08a659718d99c1ac59f4d95a2ee3c2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 23 Nov 2022 21:42:52 +0000 Subject: [PATCH 36/45] Apply fixes from StyleCI --- packages/framework/src/Console/Concerns/ValidatingCommand.php | 1 - packages/framework/tests/Feature/ValidatingCommandTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/ValidatingCommand.php b/packages/framework/src/Console/Concerns/ValidatingCommand.php index 7c5e764ffe8..330a55d7c21 100644 --- a/packages/framework/src/Console/Concerns/ValidatingCommand.php +++ b/packages/framework/src/Console/Concerns/ValidatingCommand.php @@ -5,7 +5,6 @@ namespace Hyde\Console\Concerns; use Illuminate\Contracts\Support\Arrayable; -use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Support\Facades\Validator; use LaravelZero\Framework\Commands\Command; use RuntimeException; diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 2b0cac2b675..158fb6bf783 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -8,7 +8,6 @@ use Hyde\Testing\TestCase; use Illuminate\Console\OutputStyle; use Illuminate\Support\Facades\Validator; -use Illuminate\Support\MessageBag; use Mockery; use RuntimeException; From 56590f4f2f647961eb4c83656b7b513c00861e82 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:46:57 +0100 Subject: [PATCH 37/45] Extract method --- .../tests/Feature/ValidatingCommandTest.php | 47 ++++++------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 158fb6bf783..9503c7697fd 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -18,14 +18,7 @@ class ValidatingCommandTest extends TestCase { public function testAskWithValidationCapturesInput() { - $command = new class extends ValidatingCommand - { - public function handle() - { - $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); - $this->output->writeln("Hello $name!"); - } - }; + $command = $this->getCommand(); $output = Mockery::mock(OutputStyle::class); @@ -43,14 +36,7 @@ public function handle() public function testAskWithValidationRetries() { - $command = new class extends ValidatingCommand - { - public function handle() - { - $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); - $this->output->writeln("Hello $name!"); - } - }; + $command = $this->getCommand(); $output = Mockery::mock(OutputStyle::class); @@ -72,14 +58,7 @@ public function handle() public function testAskWithValidationRetriesTooManyTimes() { - $command = new class extends ValidatingCommand - { - public function handle() - { - $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); - $this->output->writeln("Hello $name!"); - } - }; + $command = $this->getCommand(); $output = Mockery::mock(OutputStyle::class); @@ -100,14 +79,7 @@ public function handle() public function testValidationIsCalled() { - $command = new class extends ValidatingCommand - { - public function handle() - { - $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); - $this->output->writeln("Hello $name!"); - } - }; + $command = $this->getCommand(); $output = Mockery::mock(OutputStyle::class); @@ -128,4 +100,15 @@ public function handle() $command->setOutput($output); $command->handle(); } + + protected function getCommand() + { + return new class extends ValidatingCommand { + public function handle() + { + $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $this->output->writeln("Hello $name!"); + } + }; + } } From ef8754e05214bdda7e36c0048a1e55c0236695a1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:47:43 +0100 Subject: [PATCH 38/45] Decouple test class --- .../tests/Feature/ValidatingCommandTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 9503c7697fd..9e10178ba1e 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -101,14 +101,16 @@ public function testValidationIsCalled() $command->handle(); } - protected function getCommand() + protected function getCommand(): ValidationTestClass { - return new class extends ValidatingCommand { - public function handle() - { - $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); - $this->output->writeln("Hello $name!"); - } - }; + return new ValidationTestClass(); + } +} + +class ValidationTestClass extends ValidatingCommand { + public function handle() + { + $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); + $this->output->writeln("Hello $name!"); } } From d11ee4d6b7afd6a5bb9c431872cffaddacebc577 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:48:12 +0100 Subject: [PATCH 39/45] Rename test class --- packages/framework/tests/Feature/ValidatingCommandTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 9e10178ba1e..181ab4bbb80 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -101,13 +101,13 @@ public function testValidationIsCalled() $command->handle(); } - protected function getCommand(): ValidationTestClass + protected function getCommand(): ValidationTestCommand { - return new ValidationTestClass(); + return new ValidationTestCommand(); } } -class ValidationTestClass extends ValidatingCommand { +class ValidationTestCommand extends ValidatingCommand { public function handle() { $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); From 443830ad44d6fb8aa36cac1b305b8b050be500e2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:48:24 +0100 Subject: [PATCH 40/45] Inline helper method --- .../tests/Feature/ValidatingCommandTest.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 181ab4bbb80..88c5dab097e 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -18,7 +18,7 @@ class ValidatingCommandTest extends TestCase { public function testAskWithValidationCapturesInput() { - $command = $this->getCommand(); + $command = new ValidationTestCommand(); $output = Mockery::mock(OutputStyle::class); @@ -36,7 +36,7 @@ public function testAskWithValidationCapturesInput() public function testAskWithValidationRetries() { - $command = $this->getCommand(); + $command = new ValidationTestCommand(); $output = Mockery::mock(OutputStyle::class); @@ -58,7 +58,7 @@ public function testAskWithValidationRetries() public function testAskWithValidationRetriesTooManyTimes() { - $command = $this->getCommand(); + $command = new ValidationTestCommand(); $output = Mockery::mock(OutputStyle::class); @@ -79,7 +79,7 @@ public function testAskWithValidationRetriesTooManyTimes() public function testValidationIsCalled() { - $command = $this->getCommand(); + $command = new ValidationTestCommand(); $output = Mockery::mock(OutputStyle::class); @@ -100,11 +100,6 @@ public function testValidationIsCalled() $command->setOutput($output); $command->handle(); } - - protected function getCommand(): ValidationTestCommand - { - return new ValidationTestCommand(); - } } class ValidationTestCommand extends ValidatingCommand { From 41415e66271831ac77f2d1f450646c88be04309f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:49:41 +0100 Subject: [PATCH 41/45] Remove extra newlines --- packages/framework/tests/Feature/ValidatingCommandTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 88c5dab097e..eb413dbeb99 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -37,7 +37,6 @@ public function testAskWithValidationCapturesInput() public function testAskWithValidationRetries() { $command = new ValidationTestCommand(); - $output = Mockery::mock(OutputStyle::class); $output->shouldReceive('ask')->times(2)->withArgs(function (string $question) { @@ -59,7 +58,6 @@ public function testAskWithValidationRetries() public function testAskWithValidationRetriesTooManyTimes() { $command = new ValidationTestCommand(); - $output = Mockery::mock(OutputStyle::class); $output->shouldReceive('ask')->times(30)->withArgs(function (string $question) { @@ -80,7 +78,6 @@ public function testAskWithValidationRetriesTooManyTimes() public function testValidationIsCalled() { $command = new ValidationTestCommand(); - $output = Mockery::mock(OutputStyle::class); $output->shouldReceive('ask')->once()->withArgs(function (string $question) { From ba2c362fc0548a2a5005e8547a4e4d9af6872f5e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:51:13 +0100 Subject: [PATCH 42/45] Remove unnecessary boilerplate --- .../framework/tests/Feature/ValidatingCommandTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index eb413dbeb99..a79f66f00c7 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -80,13 +80,8 @@ public function testValidationIsCalled() $command = new ValidationTestCommand(); $output = Mockery::mock(OutputStyle::class); - $output->shouldReceive('ask')->once()->withArgs(function (string $question) { - return $question === 'What is your name?'; - })->andReturn('Jane Doe'); - - $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'Hello Jane Doe!'; - }); + $output->shouldReceive('ask')->once()->andReturn('Jane Doe'); + $output->shouldReceive('writeln')->once(); $validator = Validator::spy(); $validator->shouldReceive('make')->once()->withArgs(function (array $data) { From 03a04ac8fb1deeac6015fa35f25245d6e17feb79 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:53:59 +0100 Subject: [PATCH 43/45] Test output was not sent --- packages/framework/tests/Feature/ValidatingCommandTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index a79f66f00c7..176ff8e6185 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -11,6 +11,8 @@ use Mockery; use RuntimeException; +use function str_starts_with; + /** * @covers \Hyde\Console\Concerns\ValidatingCommand */ @@ -73,6 +75,10 @@ public function testAskWithValidationRetriesTooManyTimes() $command->setOutput($output); $command->handle(); + + $output->shouldNotReceive('writeln')->once()->withArgs(function (string $message) { + return str_starts_with($message, 'Hello'); + }); } public function testValidationIsCalled() From a48f7a85ddb01f3a112762f59776ada71798d806 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 23 Nov 2022 22:54:47 +0100 Subject: [PATCH 44/45] Assert rules are passed along --- packages/framework/tests/Feature/ValidatingCommandTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index 176ff8e6185..a5d37219d92 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -90,8 +90,9 @@ public function testValidationIsCalled() $output->shouldReceive('writeln')->once(); $validator = Validator::spy(); - $validator->shouldReceive('make')->once()->withArgs(function (array $data) { - return $data === ['name' => 'Jane Doe']; + $validator->shouldReceive('make')->once()->withArgs(function (array $data, array $rules) { + return $data === ['name' => 'Jane Doe'] + && $rules === ['name' => ['required']]; })->andReturn($validator); $validator->shouldReceive('passes')->once()->andReturn(true); From fabdb6cec236f1305d27f064bbfcf884ae9856c9 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 23 Nov 2022 21:57:49 +0000 Subject: [PATCH 45/45] Apply fixes from StyleCI --- packages/framework/tests/Feature/ValidatingCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/ValidatingCommandTest.php b/packages/framework/tests/Feature/ValidatingCommandTest.php index a5d37219d92..4f16a444bf6 100644 --- a/packages/framework/tests/Feature/ValidatingCommandTest.php +++ b/packages/framework/tests/Feature/ValidatingCommandTest.php @@ -10,7 +10,6 @@ use Illuminate\Support\Facades\Validator; use Mockery; use RuntimeException; - use function str_starts_with; /** @@ -101,7 +100,8 @@ public function testValidationIsCalled() } } -class ValidationTestCommand extends ValidatingCommand { +class ValidationTestCommand extends ValidatingCommand +{ public function handle() { $name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe');