|
| 1 | +<?php |
| 2 | + |
| 3 | +use Artemeon\Console\Parser; |
| 4 | + |
| 5 | +covers(Parser::class); |
| 6 | + |
| 7 | +it('can parse the name', function (): void { |
| 8 | + [$name] = Parser::parse('foo:bar'); |
| 9 | + |
| 10 | + expect($name)->toBe('foo:bar'); |
| 11 | +}); |
| 12 | + |
| 13 | +it('throws an exception if the name is invalid', function (): void { |
| 14 | + expect(static fn () => Parser::parse('')) |
| 15 | + ->toThrow(InvalidArgumentException::class, 'Unable to determine command name from signature.'); |
| 16 | +}); |
| 17 | + |
| 18 | +it('can parse simple arguments', function (): void { |
| 19 | + [, $arguments] = Parser::parse('foo:bar {hello} {world}'); |
| 20 | + |
| 21 | + expect($arguments) |
| 22 | + ->toHaveCount(2) |
| 23 | + ->and($arguments[0]->getName())->toBe('hello') |
| 24 | + ->and($arguments[0]->isRequired())->toBeTrue() |
| 25 | + ->and($arguments[0]->getDescription())->toBe('') |
| 26 | + ->and($arguments[1]->getName())->toBe('world') |
| 27 | + ->and($arguments[1]->isRequired())->toBeTrue() |
| 28 | + ->and($arguments[1]->getDescription())->toBe(''); |
| 29 | +}); |
| 30 | + |
| 31 | +it('can parse optional arguments', function (): void { |
| 32 | + [, $arguments] = Parser::parse('foo:bar {hello?} {world?}'); |
| 33 | + |
| 34 | + expect($arguments) |
| 35 | + ->toHaveCount(2) |
| 36 | + ->and($arguments[0]->getName())->toBe('hello') |
| 37 | + ->and($arguments[0]->isRequired())->toBeFalse() |
| 38 | + ->and($arguments[1]->getName())->toBe('world') |
| 39 | + ->and($arguments[1]->isRequired())->toBeFalse(); |
| 40 | +}); |
| 41 | + |
| 42 | +it('can parse optional arguments with default value', function (): void { |
| 43 | + [, $arguments] = Parser::parse('foo:bar {hello=foo} {world=bar}'); |
| 44 | + |
| 45 | + expect($arguments) |
| 46 | + ->toHaveCount(2) |
| 47 | + ->and($arguments[0]->getName())->toBe('hello') |
| 48 | + ->and($arguments[0]->isRequired())->toBeFalse() |
| 49 | + ->and($arguments[0]->getDefault())->toBe('foo') |
| 50 | + ->and($arguments[1]->getName())->toBe('world') |
| 51 | + ->and($arguments[1]->isRequired())->toBeFalse() |
| 52 | + ->and($arguments[1]->getDefault())->toBe('bar'); |
| 53 | +}); |
| 54 | + |
| 55 | +it('can parse arguments with description', function (): void { |
| 56 | + [, $arguments] = Parser::parse('foo:bar {hello : Foo} {world : Bar : Baz }'); |
| 57 | + |
| 58 | + expect($arguments) |
| 59 | + ->toHaveCount(2) |
| 60 | + ->and($arguments[0]->getName())->toBe('hello') |
| 61 | + ->and($arguments[0]->isRequired())->toBeTrue() |
| 62 | + ->and($arguments[0]->getDescription())->toBe('Foo') |
| 63 | + ->and($arguments[1]->getName())->toBe('world') |
| 64 | + ->and($arguments[1]->isRequired())->toBeTrue() |
| 65 | + ->and($arguments[1]->getDescription())->toBe('Bar : Baz'); |
| 66 | +}); |
| 67 | + |
| 68 | +it('can parse simple options', function (): void { |
| 69 | + [,, $options] = Parser::parse('foo:bar {--hello} {--world}'); |
| 70 | + |
| 71 | + expect($options) |
| 72 | + ->toHaveCount(2) |
| 73 | + ->and($options[0]->getName())->toBe('hello') |
| 74 | + ->and($options[0]->isArray())->toBeFalse() |
| 75 | + ->and($options[0]->getDefault())->toBeFalse() |
| 76 | + ->and($options[1]->getName())->toBe('world') |
| 77 | + ->and($options[1]->isArray())->toBeFalse() |
| 78 | + ->and($options[1]->getDefault())->toBeFalse(); |
| 79 | +}); |
| 80 | + |
| 81 | +it('can parse options with shortcut', function (): void { |
| 82 | + [,, $options] = Parser::parse('foo:bar {--h|hello} {--w|world}'); |
| 83 | + |
| 84 | + expect($options) |
| 85 | + ->toHaveCount(2) |
| 86 | + ->and($options[0]->getName())->toBe('hello') |
| 87 | + ->and($options[0]->isArray())->toBeFalse() |
| 88 | + ->and($options[0]->getDefault())->toBeFalse() |
| 89 | + ->and($options[0]->getShortcut())->toBe('h') |
| 90 | + ->and($options[1]->getName())->toBe('world') |
| 91 | + ->and($options[1]->isArray())->toBeFalse() |
| 92 | + ->and($options[1]->getDefault())->toBeFalse() |
| 93 | + ->and($options[1]->getShortcut())->toBe('w'); |
| 94 | +}); |
0 commit comments