123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php declare(strict_types=1);
- /*
- * This file is part of sebastian/cli-parser.
- *
- * (c) Sebastian Bergmann <sebastian@phpunit.de>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace SebastianBergmann\CliParser;
- use PHPUnit\Framework\TestCase;
- /**
- * @covers \SebastianBergmann\CliParser\Parser
- * @covers \SebastianBergmann\CliParser\AmbiguousOptionException
- * @covers \SebastianBergmann\CliParser\OptionDoesNotAllowArgumentException
- * @covers \SebastianBergmann\CliParser\RequiredOptionArgumentMissingException
- * @covers \SebastianBergmann\CliParser\UnknownOptionException
- */
- final class ParserTest extends TestCase
- {
- public function testParsesShortOptionsWithOptionalValues(): void
- {
- $this->assertSame(
- [
- [
- [
- 'f',
- null,
- ],
- ],
- [
- 'myArgument',
- ],
- ],
- (new Parser)->parse(
- [
- 'command',
- 'myArgument',
- '-f',
- ],
- 'f::'
- )
- );
- }
- public function testParsesLongOptionsWithValues(): void
- {
- $this->assertSame(
- [
- [
- ['--exec', null],
- ['--conf', 'config.xml'],
- ['--optn', null],
- ['--optn', 'content-of-o'],
- ],
- [
- 'parameter-0',
- 'parameter-1',
- 'parameter-2',
- 'parameter-n',
- ],
- ],
- (new Parser)->parse(
- [
- 'command',
- 'parameter-0',
- '--exec',
- 'parameter-1',
- '--conf',
- 'config.xml',
- '--optn',
- 'parameter-2',
- '--optn=content-of-o',
- 'parameter-n',
- ],
- '',
- ['exec', 'conf=', 'optn==']
- )
- );
- }
- public function testParsesShortongOptionsWithValues(): void
- {
- $this->assertSame(
- [
- [
- ['x', null],
- ['c', 'config.xml'],
- ['o', null],
- ['o', 'content-of-o'],
- ],
- [
- 'parameter-0',
- 'parameter-1',
- 'parameter-2',
- 'parameter-n',
- ],
- ],
- (new Parser)->parse(
- [
- 'command',
- 'parameter-0',
- '-x',
- 'parameter-1',
- '-c',
- 'config.xml',
- '-o',
- 'parameter-2',
- '-ocontent-of-o',
- 'parameter-n',
- ],
- 'xc:o::'
- )
- );
- }
- public function testParsesLongOptionsAfterArguments(): void
- {
- $this->assertSame(
- [
- [
- [
- '--colors',
- null,
- ],
- ],
- [
- 'myArgument',
- ],
- ],
- (new Parser)->parse(
- [
- 'command',
- 'myArgument',
- '--colors',
- ],
- '',
- ['colors==']
- )
- );
- }
- public function testParsesShortOptionsAfterArguments(): void
- {
- $this->assertSame(
- [
- [
- [
- 'v',
- null,
- ],
- ],
- [
- 'myArgument',
- ],
- ],
- (new Parser)->parse(
- [
- 'command',
- 'myArgument',
- '-v',
- ],
- 'v'
- )
- );
- }
- public function testReturnsEmptyResultWhenNotOptionsArePassed(): void
- {
- $this->assertSame(
- [
- [],
- [],
- ],
- (new Parser)->parse(
- [],
- 'v'
- )
- );
- }
- public function testRaisesAnExceptionForUnknownLongOption(): void
- {
- $this->expectException(UnknownOptionException::class);
- $this->expectExceptionMessage('Unknown option "--foo"');
- /* @noinspection UnusedFunctionResultInspection */
- (new Parser)->parse(
- [
- 'command',
- '--foo',
- ],
- '',
- ['colors']
- );
- }
- public function testRaisesAnExceptionForUnknownShortOption(): void
- {
- $this->expectException(UnknownOptionException::class);
- $this->expectExceptionMessage('Unknown option "-v"');
- /* @noinspection UnusedFunctionResultInspection */
- (new Parser)->parse(
- [
- 'command',
- 'myArgument',
- '-v',
- ],
- ''
- );
- }
- public function testRaisesAnExceptionWhenRequiredArgumentForLongOptionIsMissing(): void
- {
- $this->expectException(RequiredOptionArgumentMissingException::class);
- $this->expectExceptionMessage('Required argument for option "--foo" is missing');
- /* @noinspection UnusedFunctionResultInspection */
- (new Parser)->parse(
- [
- 'command',
- '--foo',
- ],
- '',
- ['foo=']
- );
- }
- public function testRaisesAnExceptionWhenRequiredArgumentForShortOptionIsMissing(): void
- {
- $this->expectException(RequiredOptionArgumentMissingException::class);
- $this->expectExceptionMessage('Required argument for option "-f" is missing');
- /* @noinspection UnusedFunctionResultInspection */
- (new Parser)->parse(
- [
- 'command',
- 'myArgument',
- '-f',
- ],
- 'f:'
- );
- }
- public function testRaisesAnExceptionWhenLongOptionIsAmbiguous(): void
- {
- $this->expectException(AmbiguousOptionException::class);
- $this->expectExceptionMessage('Option "--col" is ambiguous');
- /* @noinspection UnusedFunctionResultInspection */
- (new Parser)->parse(
- [
- 'command',
- '--col',
- ],
- '',
- ['columns', 'colors']
- );
- }
- public function testRaisesAnExceptionWhenAnArgumentIsGivenForLongOptionThatDoesNotAllowAnArgument(): void
- {
- $this->expectException(OptionDoesNotAllowArgumentException::class);
- $this->expectExceptionMessage('Option "--foo" does not allow an argument');
- /* @noinspection UnusedFunctionResultInspection */
- (new Parser)->parse(
- [
- 'command',
- '--foo=bar',
- ],
- '',
- ['foo']
- );
- }
- }
|