|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\CLI; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\Attributes\Group; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Group('Others')] |
| 24 | +final class CommandLineParserTest extends CIUnitTestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @param list<string> $tokens |
| 28 | + * @param list<string> $arguments |
| 29 | + * @param array<string, string|null> $options |
| 30 | + */ |
| 31 | + #[DataProvider('provideParseCommand')] |
| 32 | + public function testParseCommand(array $tokens, array $arguments, array $options): void |
| 33 | + { |
| 34 | + $parser = new CommandLineParser(['spark', ...$tokens]); |
| 35 | + |
| 36 | + $this->assertSame($arguments, $parser->getArguments()); |
| 37 | + $this->assertSame($options, $parser->getOptions()); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return iterable<string, array{0: list<string>, 1: list<string>, 2: array<string, string|null>}> |
| 42 | + */ |
| 43 | + public static function provideParseCommand(): iterable |
| 44 | + { |
| 45 | + yield 'no arguments or options' => [ |
| 46 | + [], |
| 47 | + [], |
| 48 | + [], |
| 49 | + ]; |
| 50 | + |
| 51 | + yield 'arguments only' => [ |
| 52 | + ['foo', 'bar'], |
| 53 | + ['foo', 'bar'], |
| 54 | + [], |
| 55 | + ]; |
| 56 | + |
| 57 | + yield 'options only' => [ |
| 58 | + ['--foo', '1', '--bar', '2'], |
| 59 | + [], |
| 60 | + ['foo' => '1', 'bar' => '2'], |
| 61 | + ]; |
| 62 | + |
| 63 | + yield 'arguments and options' => [ |
| 64 | + ['foo', '--bar', '2', 'baz', '--qux', '3'], |
| 65 | + ['foo', 'baz'], |
| 66 | + ['bar' => '2', 'qux' => '3'], |
| 67 | + ]; |
| 68 | + |
| 69 | + yield 'options with null value' => [ |
| 70 | + ['--foo', '--bar', '2'], |
| 71 | + [], |
| 72 | + ['foo' => null, 'bar' => '2'], |
| 73 | + ]; |
| 74 | + |
| 75 | + yield 'options before double hyphen' => [ |
| 76 | + ['b', 'c', '--key', 'value', '--', 'd'], |
| 77 | + ['b', 'c', 'd'], |
| 78 | + ['key' => 'value'], |
| 79 | + ]; |
| 80 | + |
| 81 | + yield 'options after double hyphen' => [ |
| 82 | + ['b', 'c', '--', '--key', 'value', 'd'], |
| 83 | + ['b', 'c', '--key', 'value', 'd'], |
| 84 | + [], |
| 85 | + ]; |
| 86 | + |
| 87 | + yield 'options before and after double hyphen' => [ |
| 88 | + ['b', 'c', '--key', 'value', '--', '--p2', 'value 2', 'd'], |
| 89 | + ['b', 'c', '--p2', 'value 2', 'd'], |
| 90 | + ['key' => 'value'], |
| 91 | + ]; |
| 92 | + |
| 93 | + yield 'double hyphen only' => [ |
| 94 | + ['b', 'c', '--', 'd'], |
| 95 | + ['b', 'c', 'd'], |
| 96 | + [], |
| 97 | + ]; |
| 98 | + |
| 99 | + yield 'options before segments with double hyphen' => [ |
| 100 | + ['--key', 'value', '--foo', '--', 'b', 'c', 'd'], |
| 101 | + ['b', 'c', 'd'], |
| 102 | + ['key' => 'value', 'foo' => null], |
| 103 | + ]; |
| 104 | + |
| 105 | + yield 'options before segments with double hyphen and no options' => [ |
| 106 | + ['--', 'b', 'c', 'd'], |
| 107 | + ['b', 'c', 'd'], |
| 108 | + [], |
| 109 | + ]; |
| 110 | + } |
| 111 | +} |
0 commit comments