|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\PieUnitTest\Command; |
| 6 | + |
| 7 | +use Php\Pie\Command\ArgvInput; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputDefinition; |
| 13 | +use Throwable; |
| 14 | + |
| 15 | +#[CoversClass(ArgvInput::class)] |
| 16 | +final class ArgvInputTest extends TestCase |
| 17 | +{ |
| 18 | + /** @return array<string, list<list<string>>> */ |
| 19 | + public static function argvWithInvalidInputProvider(): array |
| 20 | + { |
| 21 | + return [ |
| 22 | + 'simple-option' => [['myapp', '--invalid-option', 'myvalue']], |
| 23 | + 'value-option' => [['myapp', '--invalid-option=foo', 'myvalue']], |
| 24 | + 'short-option' => [['myapp', '-i', 'myvalue']], |
| 25 | + // explicitly not supported for now; we can't tell which is the argument here |
| 26 | + // 'split-option' => [['myapp', '--invalid-option', 'foo', 'myvalue']], |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + /** @param list<string> $argv */ |
| 31 | + #[DataProvider('argvWithInvalidInputProvider')] |
| 32 | + public function testInvalidOptionsDoNotCauseArgumentsToBeMissed(array $argv): void |
| 33 | + { |
| 34 | + $definition = new InputDefinition(); |
| 35 | + $definition->addArgument(new InputArgument('myarg', InputArgument::OPTIONAL)); |
| 36 | + |
| 37 | + $argvInput = new ArgvInput($argv); |
| 38 | + try { |
| 39 | + $argvInput->bind($definition); |
| 40 | + self::fail('Expected an exception to be thrown because `--invalid-option` is not defined'); |
| 41 | + } catch (Throwable) { |
| 42 | + // An exception is expected here, because `--invalid-option` was not defined |
| 43 | + self::addToAssertionCount(1); |
| 44 | + } |
| 45 | + |
| 46 | + // But, crucially, we should have captured the following argument |
| 47 | + self::assertSame('myvalue', $argvInput->getArgument('myarg')); |
| 48 | + } |
| 49 | +} |
0 commit comments