From 9514faa477743b5b621af9a5e2313e698af601aa Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Mon, 14 Jan 2019 16:32:26 +0000 Subject: [PATCH] Test exception thrown when required arguments are passed incorrectly --- test/unit/Command/CommandTest.php | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/unit/Command/CommandTest.php b/test/unit/Command/CommandTest.php index 822e4b6..dae7ccc 100644 --- a/test/unit/Command/CommandTest.php +++ b/test/unit/Command/CommandTest.php @@ -11,6 +11,7 @@ use Gt\Cli\Argument\NotEnoughArgumentsException; use Gt\Cli\CliException; use Gt\Cli\Command\HelpCommand; +use Gt\Cli\Parameter\MissingRequiredParameterException; use Gt\Cli\Parameter\Parameter; use Gt\Cli\Stream; use Gt\Cli\Test\Helper\Command\MultipleRequiredParameterCommand; @@ -174,6 +175,70 @@ public function testCheckArgumentsMultipleGood() { self::assertNull($exception, "No exception should be thrown"); } + public function testCheckArgumentsMultipleBad() { +// Almost identical test to above, but the "framework" arg will not be provided. + $args = [ + self::createMock(NamedArgument::class), + self::createMock(NamedArgument::class), + self::createMock(LongOptionArgument::class), + self::createMock(LongOptionArgument::class), + ]; + $longArgs = [ + null, + null, + ["age" => "123"], + "example" + ]; + + /** @var ArgumentList|MockObject $argList */ + $argList = $this->createIteratorMock( + ArgumentList::class, + $args + ); + +// TODO: Extract the "contains" and "getValueForParmater" functionality +// into a "createArgumentListMock" function. + $argList->method("contains") + ->willReturnCallback(function(Parameter $param)use($longArgs) { + $longOption = $param->getLongOption(); + foreach($longArgs as $a) { + if(is_array($a)) { + if(key($a) === $longOption) { + return true; + } + } + else { + if($a === $longOption) { + return true; + } + } + } + return false; + }); + + $argList->method("getValueForParameter") + ->willReturnCallback(function(Parameter $param)use($longArgs) { + $longOption = $param->getLongOption(); + foreach($longArgs as $a) { + if(!is_array($a)) { + continue; + } + + $key = key($a); + if($key !== $longOption) { + continue; + } + + return $a[$key]; + } + return null; + }); + + $command = new MultipleRequiredParameterCommand(); + $this->expectException(MissingRequiredParameterException::class); + $command->checkArguments($argList); + } + protected function createIteratorMock( string $className, array $items = []