Skip to content

Commit

Permalink
Merge branch 'main' of github.com:thettler/laravel-console-toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
thettler committed May 7, 2023
2 parents d9850fc + 5235f89 commit b70b9ca
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.3.5
uses: dependabot/fetch-metadata@v1.4.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [9.*]
php: [8.1, 8.2]
laravel: [9.*, 10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: ^7.0
testbench: 7.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-console-toolkit` will be documented in this file.

## v0.1.1 - 2022-11-22

- Fixes Issue described in #8

## 0.1.0 - 2022-03-1

- initial release
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
"php": "^8.1",
"symfony/console": "^6.0",
"spatie/laravel-package-tools": "^1.9.2",
"illuminate/contracts": "^9.0"
"illuminate/contracts": "^9.0|^10.0"
},
"require-dev": {
"laravel/sail": "^1.13",
"nunomaduro/collision": "^v6.1.0",
"nunomaduro/larastan": "^1.0",
"orchestra/testbench": "^7.0",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"pestphp/pest-plugin-laravel": "^1.4",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
Expand Down
12 changes: 6 additions & 6 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
3 changes: 3 additions & 0 deletions src/Reflections/ArgumentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Thettler\LaravelConsoleToolkit\Attributes\Argument;
use Thettler\LaravelConsoleToolkit\Enums\ConsoleInputType;

/**
* @extends InputReflection<Argument>
*/
class ArgumentReflection extends InputReflection
{
public static function isArgument(\ReflectionProperty $property): bool
Expand Down
6 changes: 6 additions & 0 deletions src/Reflections/CommandReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function usesInputAttributes(): bool
return $this->getArguments()->isNotEmpty() || $this->getOptions()->isNotEmpty();
}

/**
* @return Collection<int, ArgumentReflection>
*/
public function getArguments(): Collection
{
return collect($this->reflection->getProperties())
Expand All @@ -49,6 +52,9 @@ public function getArguments(): Collection
);
}

/**
* @return Collection<int, OptionReflection>
*/
public function getOptions(): Collection
{
return collect($this->reflection->getProperties())
Expand Down
7 changes: 7 additions & 0 deletions src/Transfers/InputErrorData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace Thettler\LaravelConsoleToolkit\Transfers;

use Thettler\LaravelConsoleToolkit\Contracts\ConsoleInput;
use Thettler\LaravelConsoleToolkit\Reflections\InputReflection;

class InputErrorData
{
/**
* @param string $key
* @param array $choices
* @param InputReflection<ConsoleInput> $reflection
* @param bool $hasAutoAsk
*/
public function __construct(
public readonly string $key,
public readonly array $choices,
Expand Down
7 changes: 5 additions & 2 deletions tests/ConsoleInputAutoAskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Translation\Translator;
use Thettler\LaravelConsoleToolkit\Attributes\Argument;
use Thettler\LaravelConsoleToolkit\Attributes\Option;
use Thettler\LaravelConsoleToolkit\Concerns\UsesConsoleToolkit;
Expand Down Expand Up @@ -143,11 +145,12 @@ public function handle()
};

Artisan::starting(fn (Artisan $artisan) => $artisan->add($command));
$translator = App::make(Translator::class);

\Pest\Laravel\artisan('validate LongerThan5 --shortOption=alsoLonger')
->expectsOutput('The short argument must not be greater than 5 characters.')
->expectsOutput($translator->get('validation.max.string', ['attribute' => 'short argument', 'max' => 5]))
->expectsQuestion('Please enter "shortArgument"', 'short')
->expectsOutput('The short option must not be greater than 5 characters.')
->expectsOutput($translator->get('validation.max.string', ['attribute' => 'short option', 'max' => 5]))
->expectsQuestion('Please enter "shortOption"', 'small')
->expectsOutput('short small')
->assertSuccessful();
Expand Down
26 changes: 15 additions & 11 deletions tests/ConsoleInputValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use Illuminate\Console\Application as Artisan;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Translation\Translator;
use Thettler\LaravelConsoleToolkit\Attributes\Argument;
use Thettler\LaravelConsoleToolkit\Attributes\Option;
use Thettler\LaravelConsoleToolkit\Concerns\UsesConsoleToolkit;
Expand Down Expand Up @@ -30,15 +32,17 @@

public function handle()
{
$this->line($this->shortArgument . ' ' . $this->shortOption);
$this->line($this->shortArgument.' '.$this->shortOption);
}
};

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

$translator = App::make(Translator::class);

\Pest\Laravel\artisan('validate LongerThan5 --shortOption=alsoLonger')
->expectsOutput('The short argument must not be greater than 5 characters.')
->expectsOutput('The short option must not be greater than 5 characters.')
->expectsOutput($translator->get('validation.max.string', ['attribute' => 'short argument', 'max' => 5]))
->expectsOutput($translator->get('validation.max.string', ['attribute' => 'short option', 'max' => 5]))
->doesntExpectOutput('LongerThan5 alsoLonger')
->assertFailed();
});
Expand All @@ -59,7 +63,7 @@ public function handle()

public function handle()
{
$this->line($this->A->name . ' ' . $this->O->value);
$this->line($this->A->name.' '.$this->O->value);
}
};

Expand All @@ -68,14 +72,14 @@ public function handle()
\Pest\Laravel\artisan('validate notValid --O=notValid')
->expectsOutput('The selected a is invalid.')
->expectsOutput('Possible values for: A.')
->expectsOutput(' - ' . Enum::A->name)
->expectsOutput(' - ' . Enum::B->name)
->expectsOutput(' - ' . Enum::C->name)
->expectsOutput(' - '.Enum::A->name)
->expectsOutput(' - '.Enum::B->name)
->expectsOutput(' - '.Enum::C->name)
->expectsOutput('The selected o is invalid.')
->expectsOutput('Possible values for: O.')
->expectsOutput(' - ' . StringEnum::A->value)
->expectsOutput(' - ' . StringEnum::B->value)
->expectsOutput(' - ' . StringEnum::C->value)
->expectsOutput(' - '.StringEnum::A->value)
->expectsOutput(' - '.StringEnum::B->value)
->expectsOutput(' - '.StringEnum::C->value)
->assertFailed();
});

Expand Down Expand Up @@ -105,7 +109,7 @@ public function handle()

public function handle()
{
$this->line($this->shortArgument . ' ' . $this->shortOption);
$this->line($this->shortArgument.' '.$this->shortOption);
}
};

Expand Down

0 comments on commit b70b9ca

Please sign in to comment.