Skip to content

Commit

Permalink
Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
thettler committed Nov 22, 2022
1 parent 7c33dd8 commit a3c521c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/Concerns/UsesConsoleToolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ protected function propertyToOption(OptionReflection $option): InputOption
$option->hasRequiredValue() => $this->makeInputOption(
$option,
$option->isAutoAskEnabled() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_REQUIRED,
$option->isAutoAskEnabled() ? '$__not_provided__$' : null
),

$option->hasValue() => $this->makeInputOption(
Expand Down
19 changes: 12 additions & 7 deletions src/Concerns/UsesInputValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ protected function validate(Collection $collection): Collection
$messages
);

if (! $validator->fails()) {
if (!$validator->fails()) {
return $collection;
}

$inputErrors = $collection->mapWithKeys(fn (InputReflection $reflection) => [
$inputErrors = $collection->mapWithKeys(fn(InputReflection $reflection) => [
$reflection->getName() => new InputErrorData(
key: $reflection->getName(),
choices: $choices[$reflection->getName()] ?? [],
Expand All @@ -56,7 +56,7 @@ protected function validate(Collection $collection): Collection
*/
protected function extractValidationData(Collection $collection): array
{
return $collection->reduce(fn (array $carry, InputReflection $reflection) => [
return $collection->reduce(fn(array $carry, InputReflection $reflection) => [
'values' => [...$carry['values'], ...$this->extractInputValues($reflection)],
'rules' => [...$carry['rules'], ...$this->extractInputRules($reflection)],
'messages' => [...$carry['messages'], ...$this->extractValidationMessages($reflection)],
Expand All @@ -71,12 +71,12 @@ protected function extractValidationData(Collection $collection): array

protected function extractValidationMessages(InputReflection $reflection): array
{
if (! $reflection->getValidationMessage()) {
if (!$reflection->getValidationMessage()) {
return [];
}

return collect($reflection->getValidationMessage())
->mapWithKeys(fn (string $value, string $key) => ["{$reflection->getName()}.{$key}" => $value])
->mapWithKeys(fn(string $value, string $key) => ["{$reflection->getName()}.{$key}" => $value])
->all();
}

Expand All @@ -98,7 +98,7 @@ protected function extractInputRules(
): array {
$rules = [];

if ($this->hasAutoAskEnabled($reflection) && ! $reflection->isArray()) {
if ($this->hasAutoAskEnabled($reflection) && !$reflection->isArray()) {
$rules[] = 'required';
}

Expand Down Expand Up @@ -129,7 +129,12 @@ protected function hasAutoAskEnabled(InputReflection $reflection): bool
ConsoleInputType::Argument => $reflection->isAutoAskEnabled(),
ConsoleInputType::Option => $reflection->isAutoAskEnabled()
&& $reflection->hasRequiredValue()
&& $this->option($reflection->getName()) !== '$__not_provided__$',
&& array_key_exists($reflection->getName(), $this->getSpecifiedOptions()),
};
}

protected function getSpecifiedOptions(): array
{
return (new \ReflectionClass($this->input))->getProperty('options')->getValue($this->input);
}
}
23 changes: 23 additions & 0 deletions tests/ConsoleInputAutoAskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ public function handle()
->assertSuccessful();
});

it('only asks for options if there is an required value and the option is used', function () {
$command = new class () extends Command {
use UsesConsoleToolkit;

protected $name = 'ask:me';

#[Option]
public string $required;

public function handle()
{
$this->line('Some Text');
}
};

Artisan::starting(fn (Artisan $artisan) => $artisan->add($command));

\Pest\Laravel\artisan('ask:me --required')
->expectsQuestion('Please enter "required"', 'Swiss')
->expectsOutput('Some Text')
->assertSuccessful();
});

it('can give choices if missing input', function () {
$command = new class () extends Command {
use UsesConsoleToolkit;
Expand Down

0 comments on commit a3c521c

Please sign in to comment.