|
| 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