-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to use GetValue with Laravel command
- Loading branch information
Showing
5 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wrkflow\GetValue\Laravel; | ||
|
||
use Illuminate\Console\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Wrkflow\GetValue\GetValue; | ||
use Wrkflow\GetValue\GetValueFactory; | ||
|
||
class GetValueFactoryCommand extends Command | ||
{ | ||
protected GetValue $inputData; | ||
|
||
public function __construct( | ||
private readonly GetValueFactory $getValueFactory, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output): void | ||
{ | ||
parent::initialize($input, $output); | ||
|
||
$this->inputData = $this->getValueFactory->command($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wrkflow\GetValueTests\Laravel; | ||
|
||
use Closure; | ||
use PHPUnit\Framework\Assert; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
use Wrkflow\GetValue\GetValueFactory; | ||
use Wrkflow\GetValue\Laravel\GetValueFactoryCommand; | ||
|
||
class GetValueFactoryCommandTest extends AbstractLaravelTestCase | ||
{ | ||
/** | ||
* @return array<string|int, array{0: Closure(static):void}> | ||
*/ | ||
public function dataCommand(): array | ||
{ | ||
return [ | ||
'argument only' => [ | ||
fn (self $self) => $self->assertCommand( | ||
input: 'test', | ||
expectedArgument: 'test', | ||
expectedBoolOption: false, | ||
expectedValueOption: null, | ||
), | ||
], | ||
'argument, option' => [ | ||
fn (self $self) => $self->assertCommand( | ||
input: 'test --option', | ||
expectedArgument: 'test', | ||
expectedBoolOption: true, | ||
expectedValueOption: null, | ||
), | ||
], | ||
'argument, option, value' => [ | ||
fn (self $self) => $self->assertCommand( | ||
input: 'test --option --value=test22', | ||
expectedArgument: 'test', | ||
expectedBoolOption: true, | ||
expectedValueOption: 'test22', | ||
), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param Closure(static):void $assert | ||
* | ||
* @dataProvider dataCommand | ||
*/ | ||
public function testCommand(Closure $assert): void | ||
{ | ||
$assert($this); | ||
} | ||
|
||
public function assertCommand( | ||
string $input, | ||
?string $expectedArgument, | ||
bool $expectedBoolOption, | ||
?string $expectedValueOption, | ||
): void { | ||
$command = new class( | ||
new GetValueFactory(), | ||
$expectedArgument, | ||
$expectedBoolOption, | ||
$expectedValueOption, | ||
) extends GetValueFactoryCommand { | ||
protected $signature = 'test {argument} {--option} {--value=}'; | ||
|
||
public function __construct( | ||
private readonly GetValueFactory $getValueFactory, | ||
private readonly ?string $expectedArgument, | ||
private readonly ?bool $expectedBoolOption, | ||
private readonly ?string $expectedValueOption, | ||
) { | ||
parent::__construct($this->getValueFactory); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
Assert::assertSame($this->expectedArgument, $this->inputData->getString('argument')); | ||
Assert::assertSame($this->expectedBoolOption, $this->inputData->getBool('option')); | ||
Assert::assertSame($this->expectedValueOption, $this->inputData->getString('value')); | ||
} | ||
}; | ||
|
||
$command->setLaravel($this->app()); | ||
|
||
$command->run(new StringInput($input), new NullOutput()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wrkflow\GetValueTests\Laravel; | ||
|
||
use Closure; | ||
use Illuminate\Console\Command; | ||
use PHPUnit\Framework\Assert; | ||
use Symfony\Component\Console\Input\StringInput; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
use Wrkflow\GetValue\GetValueFactory; | ||
|
||
class GetValueFactoryLaravelCommandTest extends AbstractLaravelTestCase | ||
{ | ||
private GetValueFactory $factory; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->factory = new GetValueFactory(); | ||
} | ||
|
||
/** | ||
* @return array<string|int, array{0: Closure(static):void}> | ||
*/ | ||
public function dataCommand(): array | ||
{ | ||
return [ | ||
'argument only' => [ | ||
fn (self $self) => $self->assertCommand( | ||
input: 'test', | ||
expectedArgument: 'test', | ||
expectedBoolOption: false, | ||
expectedValueOption: null, | ||
), | ||
], | ||
'argument, option' => [ | ||
fn (self $self) => $self->assertCommand( | ||
input: 'test --option', | ||
expectedArgument: 'test', | ||
expectedBoolOption: true, | ||
expectedValueOption: null, | ||
), | ||
], | ||
'argument, option, value' => [ | ||
fn (self $self) => $self->assertCommand( | ||
input: 'test --option --value=test22', | ||
expectedArgument: 'test', | ||
expectedBoolOption: true, | ||
expectedValueOption: 'test22', | ||
), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param Closure(static):void $assert | ||
* | ||
* @dataProvider dataCommand | ||
*/ | ||
public function testCommand(Closure $assert): void | ||
{ | ||
$assert($this); | ||
} | ||
|
||
public function assertCommand( | ||
string $input, | ||
?string $expectedArgument, | ||
bool $expectedBoolOption, | ||
?string $expectedValueOption, | ||
): void { | ||
$command = new class( | ||
$this->factory, | ||
$expectedArgument, | ||
$expectedBoolOption, | ||
$expectedValueOption, | ||
) extends Command { | ||
protected $signature = 'test {argument} {--option} {--value=}'; | ||
|
||
public function __construct( | ||
private readonly GetValueFactory $getValueFactory, | ||
private readonly ?string $expectedArgument, | ||
private readonly ?bool $expectedBoolOption, | ||
private readonly ?string $expectedValueOption, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$data = $this->getValueFactory->command($this); | ||
|
||
Assert::assertSame($this->expectedArgument, $data->getString('argument')); | ||
Assert::assertSame($this->expectedBoolOption, $data->getBool('option')); | ||
Assert::assertSame($this->expectedValueOption, $data->getString('value')); | ||
} | ||
}; | ||
|
||
$command->setLaravel($this->app()); | ||
|
||
$command->run(new StringInput($input), new NullOutput()); | ||
} | ||
} |