From 5d71180c6755ae319dc63ab2c8a783941782b8a3 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 22 Nov 2018 22:51:52 +0000 Subject: [PATCH] Complete ArgumentList tests --- src/Argument/ShortOptionArgument.php | 9 +++- test/unit/ArgumentListTest.php | 74 +++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/Argument/ShortOptionArgument.php b/src/Argument/ShortOptionArgument.php index 9d3ff29..230ffa1 100644 --- a/src/Argument/ShortOptionArgument.php +++ b/src/Argument/ShortOptionArgument.php @@ -3,6 +3,13 @@ class ShortOptionArgument extends Argument { protected function processRawKey(string $rawKey):string { - return substr($rawKey, 1); + $key = substr($rawKey, 1); + $equalsPos = strpos($key, "="); + + if($equalsPos !== false) { + $key = substr($key, 0, $equalsPos); + } + + return $key; } } \ No newline at end of file diff --git a/test/unit/ArgumentListTest.php b/test/unit/ArgumentListTest.php index b039eda..0ee7ff9 100644 --- a/test/unit/ArgumentListTest.php +++ b/test/unit/ArgumentListTest.php @@ -191,7 +191,7 @@ public function testNotContains(string...$args) { } } - /** @dataProvider data_randomEqualsArgs */ + /** @dataProvider data_randomLongEqualsArgs */ public function testKeyValueSetWithLongOptionEqualsSign(string...$args) { $argumentList = new ArgumentList( array_shift($args), @@ -236,13 +236,73 @@ public function testKeyValueSetWithShortOptionEqualsSign(string...$args) { $param->method("getShortOption") ->willReturn($key); + $paramValue = $argumentList->getValueForParameter($param); + + if($paramValue != $value) { + + var_dump($args);die(); + } + self::assertEquals( $value, - $argumentList->getValueForParameter($param) + $paramValue ); } } + /** @dataProvider data_randomShortEqualsArgs */ + public function testGetValueForParameterNotExists(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($args as $i => $arg) { + if($i === 0) { + continue; + } + + $arg = substr($arg, 1); + list($key, $value) = explode("=", $arg); + + $param = self::createMock(Parameter::class); + $param->method("getShortOption") + ->willReturn("Z"); + + self::assertNull($argumentList->getValueForParameter($param)); + } + } + + /** @dataProvider data_randomLongArgs */ + public function testGetValueForParameterWithLongOption(string...$args) { + $argumentList = new ArgumentList( + array_Shift($args), + ...$args + ); + + $param = self::createMock(Parameter::class); + $param->method("getLongOption") + ->willReturn(substr($args[1], 2)); + $value = $argumentList->getValueForParameter($param); + + self::assertEquals($args[2], $value); + } + + /** @dataProvider data_randomShortArgs */ + public function testGetValueParameterWithShortOption(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + $param = self::createMock(Parameter::class); + $param->method("getShortOption") + ->willReturn(substr($args[1], 1)); + $value = $argumentList->getValueForParameter($param); + + self::assertEquals($args[2], $value); + } + public function data_randomNamedArgs():array { $dataSet = []; @@ -313,7 +373,7 @@ public function data_randomShortArgs():array { return $dataSet; } - public function data_randomEqualsArgs():array { + public function data_randomLongEqualsArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -337,6 +397,7 @@ public function data_randomShortEqualsArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { + $charArray = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; $params = []; $params []= uniqid("script-"); @@ -344,11 +405,10 @@ public function data_randomShortEqualsArgs():array { $numParams = rand(1, 10); for($j = 0; $j < $numParams; $j++) { + $char = array_shift($charArray); + $params []= "-" - . substr( - md5(microtime()), - rand(0,26), - 1) + . $char . "=" . uniqid(); }