Skip to content

Commit 1457180

Browse files
committed
Add ability to use GetValue with Laravel command
1 parent 2c00735 commit 1457180

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

docs/content/en/laravel.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ subtitle: ''
44
position: 4
55
---
66

7+
## Command input
8+
9+
For safe access of command arguments / options you can extend `GetValueFactoryCommand` that provides `$this->inputData`
10+
with wrapped arguments and options in `GetValue` instance.
11+
12+
```php
13+
class MyCommand extends GetValueFactoryCommand {
14+
protected $signature = 'test {argument} {--option} {--value=}';
15+
16+
public function handle(): void
17+
{
18+
// Ensures that you will get a string
19+
$this->inputData->getRequiredString('argument');
20+
// Get bool (option always return bool)
21+
$this->inputData->getRequiredBool('option');
22+
// Returns nullable string
23+
$this->inputData->getString('value');
24+
}
25+
}
26+
```
27+
728
## GetValueFormRequest
829

930
Allows you access `GetValue` instance within your `FormRequest` by extending `GetValueFormRequest`.
@@ -36,6 +57,7 @@ We have implemented helper functions that pulls array data from Laravel requests
3657
- `$this->getValueFactory->request($request);` - Initializes `ArrayData` with FormRequest and uses only **validated**
3758
array data.
3859
- `$this->getValueFactory->requestAll($request);` - Initializes `ArrayData` with **all values** from the requests
60+
- `$this->getValueFactory->command($request);` - Initializes `ArrayData` with command arguments and options.
3961

4062
```php
4163
class TestAction {

src/GetValueFactory.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Wrkflow\GetValue;
66

7+
use Illuminate\Console\Command;
78
use Illuminate\Foundation\Http\FormRequest;
89
use Illuminate\Http\Request;
910
use SimpleXMLElement;
@@ -23,11 +24,17 @@ public function __construct(
2324
) {
2425
}
2526

27+
/**
28+
* Wraps all request data.
29+
*/
2630
public function requestAll(Request $request, string $parentKey = ''): GetValue
2731
{
2832
return $this->array($request->all(), $parentKey);
2933
}
3034

35+
/**
36+
* Wraps validated request data.
37+
*/
3138
public function request(FormRequest $request, string $parentKey = ''): GetValue
3239
{
3340
return $this->array($request->validated(), $parentKey);
@@ -43,6 +50,14 @@ public function array(array $array, string $parentKey = ''): GetValue
4350
return $this->make(new ArrayData($array, $parentKey));
4451
}
4552

53+
/**
54+
* Wraps and merges command arguments and options.
55+
*/
56+
public function command(Command $command): GetValue
57+
{
58+
return $this->array(array_merge($command->arguments(), $command->options()));
59+
}
60+
4661
protected function make(AbstractData $data): GetValue
4762
{
4863
return new GetValue(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValue\Laravel;
6+
7+
use Illuminate\Console\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Wrkflow\GetValue\GetValue;
11+
use Wrkflow\GetValue\GetValueFactory;
12+
13+
class GetValueFactoryCommand extends Command
14+
{
15+
protected GetValue $inputData;
16+
17+
public function __construct(
18+
private readonly GetValueFactory $getValueFactory,
19+
) {
20+
parent::__construct();
21+
}
22+
23+
protected function initialize(InputInterface $input, OutputInterface $output): void
24+
{
25+
parent::initialize($input, $output);
26+
27+
$this->inputData = $this->getValueFactory->command($this);
28+
}
29+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Laravel;
6+
7+
use Closure;
8+
use PHPUnit\Framework\Assert;
9+
use Symfony\Component\Console\Input\StringInput;
10+
use Symfony\Component\Console\Output\NullOutput;
11+
use Wrkflow\GetValue\GetValueFactory;
12+
use Wrkflow\GetValue\Laravel\GetValueFactoryCommand;
13+
14+
class GetValueFactoryCommandTest extends AbstractLaravelTestCase
15+
{
16+
/**
17+
* @return array<string|int, array{0: Closure(static):void}>
18+
*/
19+
public function dataCommand(): array
20+
{
21+
return [
22+
'argument only' => [
23+
fn (self $self) => $self->assertCommand(
24+
input: 'test',
25+
expectedArgument: 'test',
26+
expectedBoolOption: false,
27+
expectedValueOption: null,
28+
),
29+
],
30+
'argument, option' => [
31+
fn (self $self) => $self->assertCommand(
32+
input: 'test --option',
33+
expectedArgument: 'test',
34+
expectedBoolOption: true,
35+
expectedValueOption: null,
36+
),
37+
],
38+
'argument, option, value' => [
39+
fn (self $self) => $self->assertCommand(
40+
input: 'test --option --value=test22',
41+
expectedArgument: 'test',
42+
expectedBoolOption: true,
43+
expectedValueOption: 'test22',
44+
),
45+
],
46+
];
47+
}
48+
49+
/**
50+
* @param Closure(static):void $assert
51+
*
52+
* @dataProvider dataCommand
53+
*/
54+
public function testCommand(Closure $assert): void
55+
{
56+
$assert($this);
57+
}
58+
59+
public function assertCommand(
60+
string $input,
61+
?string $expectedArgument,
62+
bool $expectedBoolOption,
63+
?string $expectedValueOption,
64+
): void {
65+
$command = new class(
66+
new GetValueFactory(),
67+
$expectedArgument,
68+
$expectedBoolOption,
69+
$expectedValueOption,
70+
) extends GetValueFactoryCommand {
71+
protected $signature = 'test {argument} {--option} {--value=}';
72+
73+
public function __construct(
74+
private readonly GetValueFactory $getValueFactory,
75+
private readonly ?string $expectedArgument,
76+
private readonly ?bool $expectedBoolOption,
77+
private readonly ?string $expectedValueOption,
78+
) {
79+
parent::__construct($this->getValueFactory);
80+
}
81+
82+
public function handle(): void
83+
{
84+
Assert::assertSame($this->expectedArgument, $this->inputData->getString('argument'));
85+
Assert::assertSame($this->expectedBoolOption, $this->inputData->getBool('option'));
86+
Assert::assertSame($this->expectedValueOption, $this->inputData->getString('value'));
87+
}
88+
};
89+
90+
$command->setLaravel($this->app());
91+
92+
$command->run(new StringInput($input), new NullOutput());
93+
}
94+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Laravel;
6+
7+
use Closure;
8+
use Illuminate\Console\Command;
9+
use PHPUnit\Framework\Assert;
10+
use Symfony\Component\Console\Input\StringInput;
11+
use Symfony\Component\Console\Output\NullOutput;
12+
use Wrkflow\GetValue\GetValueFactory;
13+
14+
class GetValueFactoryLaravelCommandTest extends AbstractLaravelTestCase
15+
{
16+
private GetValueFactory $factory;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->factory = new GetValueFactory();
23+
}
24+
25+
/**
26+
* @return array<string|int, array{0: Closure(static):void}>
27+
*/
28+
public function dataCommand(): array
29+
{
30+
return [
31+
'argument only' => [
32+
fn (self $self) => $self->assertCommand(
33+
input: 'test',
34+
expectedArgument: 'test',
35+
expectedBoolOption: false,
36+
expectedValueOption: null,
37+
),
38+
],
39+
'argument, option' => [
40+
fn (self $self) => $self->assertCommand(
41+
input: 'test --option',
42+
expectedArgument: 'test',
43+
expectedBoolOption: true,
44+
expectedValueOption: null,
45+
),
46+
],
47+
'argument, option, value' => [
48+
fn (self $self) => $self->assertCommand(
49+
input: 'test --option --value=test22',
50+
expectedArgument: 'test',
51+
expectedBoolOption: true,
52+
expectedValueOption: 'test22',
53+
),
54+
],
55+
];
56+
}
57+
58+
/**
59+
* @param Closure(static):void $assert
60+
*
61+
* @dataProvider dataCommand
62+
*/
63+
public function testCommand(Closure $assert): void
64+
{
65+
$assert($this);
66+
}
67+
68+
public function assertCommand(
69+
string $input,
70+
?string $expectedArgument,
71+
bool $expectedBoolOption,
72+
?string $expectedValueOption,
73+
): void {
74+
$command = new class(
75+
$this->factory,
76+
$expectedArgument,
77+
$expectedBoolOption,
78+
$expectedValueOption,
79+
) extends Command {
80+
protected $signature = 'test {argument} {--option} {--value=}';
81+
82+
public function __construct(
83+
private readonly GetValueFactory $getValueFactory,
84+
private readonly ?string $expectedArgument,
85+
private readonly ?bool $expectedBoolOption,
86+
private readonly ?string $expectedValueOption,
87+
) {
88+
parent::__construct();
89+
}
90+
91+
public function handle(): void
92+
{
93+
$data = $this->getValueFactory->command($this);
94+
95+
Assert::assertSame($this->expectedArgument, $data->getString('argument'));
96+
Assert::assertSame($this->expectedBoolOption, $data->getBool('option'));
97+
Assert::assertSame($this->expectedValueOption, $data->getString('value'));
98+
}
99+
};
100+
101+
$command->setLaravel($this->app());
102+
103+
$command->run(new StringInput($input), new NullOutput());
104+
}
105+
}

0 commit comments

Comments
 (0)