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 4606f30..0ee7ff9 100644 --- a/test/unit/ArgumentListTest.php +++ b/test/unit/ArgumentListTest.php @@ -1,20 +1,309 @@ getCommandName() + ); + } + + /** @dataProvider data_randomNamedArgs */ + public function testIteratorWithNamedArgs(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($argumentList as $i => $argument) { + /** @var Argument $argument */ + self::assertInstanceOf( + Argument::class, + $argument + ); + + self::assertEquals( + $args[$i], + $argument + ); + } + } + + /** @dataProvider data_randomLongArgs */ + public function testIteratorWithLongArgs(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($argumentList as $i => $argument) { + /** @var Argument $argument */ + self::assertInstanceOf( + Argument::class, + $argument + ); + + if($i === 0) { + self::assertEquals( + $args[0], + $argument + ); + continue; + } + + $originalKey = $args[($i - 1) * 2 + 1]; + $originalValue = $args[($i - 1) * 2 + 2]; + + self::assertEquals( + substr($originalKey, 2), + $argument->getKey() + ); + self::assertEquals( + $originalValue, + $argument->getValue() + ); + } + } + + /** @dataProvider data_randomShortArgs */ + public function testIteratorWithShortArgs(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($argumentList as $i => $argument) { + /** @var Argument $argument */ + self::assertInstanceOf( + Argument::class, + $argument + ); + + if($i === 0) { + self::assertEquals( + $args[0], + $argument + ); + continue; + } + + $originalKey = $args[($i - 1) * 2 + 1]; + $originalValue = $args[($i - 1) * 2 + 2]; + + self::assertEquals( + substr($originalKey, 1), + $argument->getKey() + ); + self::assertEquals( + $originalValue, + $argument->getValue() + ); + } + } + + /** @dataProvider data_randomShortArgs */ + public function testContainsWithShortArgs(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($args as $i => $arg) { + if($i === 0) { + continue; + } + + if($i % 2 === 0) { +// Because args are passed in as "--key value" every other original arg is skipped. + continue; + } + + $param = self::createMock(Parameter::class); + $param->method("getShortOption") + ->willReturn(substr($arg, 1)); + + /** @var Parameter $param */ + self::assertTrue($argumentList->contains($param)); + } + } + + /** @dataProvider data_randomLongArgs */ + public function testContainsWithLongArgs(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($args as $i => $arg) { + if($i === 0) { + continue; + } + + if($i % 2 === 0) { +// Because args are passed in as "--key value" every other original arg is skipped. + continue; + } + + $param = self::createMock(Parameter::class); + $param->method("getLongOption") + ->willReturn(substr($arg, 2)); + + /** @var Parameter $param */ + self::assertTrue($argumentList->contains($param)); + } + } + + /** @dataProvider data_randomLongArgs */ + public function testNotContains(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($args as $i => $arg) { + if($i === 0) { + continue; + } + + if($i % 2 === 0) { +// Because args are passed in as "--key value" every other original arg is skipped. + continue; + } + + $param = self::createMock(Parameter::class); + $param->method("getLongOption") + ->willReturn(uniqid()); + + /** @var Parameter $param */ + self::assertFalse($argumentList->contains($param)); + } + } + + /** @dataProvider data_randomLongEqualsArgs */ + public function testKeyValueSetWithLongOptionEqualsSign(string...$args) { + $argumentList = new ArgumentList( + array_shift($args), + ...$args + ); + + foreach($args as $i => $arg) { + if($i === 0) { + continue; + } + + $arg = substr($arg, 2); + list($key, $value) = explode("=", $arg); + + $param = self::createMock(Parameter::class); + $param->method("getLongOption") + ->willReturn($key); + + self::assertEquals( + $value, + $argumentList->getValueForParameter($param) + ); + } + } + + /** @dataProvider data_randomShortEqualsArgs */ + public function testKeyValueSetWithShortOptionEqualsSign(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($key); + + $paramValue = $argumentList->getValueForParameter($param); + + if($paramValue != $value) { + + var_dump($args);die(); + } + + self::assertEquals( + $value, + $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_randomArgs():array { + public function data_randomNamedArgs():array { $dataSet = []; for($i = 0; $i < 10; $i++) { @@ -33,4 +322,100 @@ public function data_randomArgs():array { return $dataSet; } + + public function data_randomLongArgs():array { + $dataSet = []; + + for($i = 0; $i < 10; $i++) { + $params = []; + + $params []= uniqid("script-"); + $params []= uniqid("command-"); + + $numParams = rand(1, 10); + for($j = 0; $j < $numParams; $j++) { + if($j % 2 === 0) { + $params []= "--" . uniqid(); + } + else { + $params []= uniqid(); + } + } + + $dataSet []= $params; + } + + return $dataSet; + } + + public function data_randomShortArgs():array { + $dataSet = []; + + for($i = 0; $i < 10; $i++) { + $params = []; + + $params []= uniqid("script-"); + $params []= uniqid("command-"); + + $numParams = rand(1, 10); + for($j = 0; $j < $numParams; $j++) { + if($j % 2 === 0) { + $params []= "-" . uniqid(); + } + else { + $params []= uniqid(); + } + } + + $dataSet []= $params; + } + + return $dataSet; + } + + public function data_randomLongEqualsArgs():array { + $dataSet = []; + + for($i = 0; $i < 10; $i++) { + $params = []; + + $params []= uniqid("script-"); + $params []= uniqid("command-"); + + $numParams = rand(1, 10); + for($j = 0; $j < $numParams; $j++) { + $params []= "--" . uniqid() . "=" . uniqid(); + } + + $dataSet []= $params; + } + + return $dataSet; + } + + 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-"); + $params []= uniqid("command-"); + + $numParams = rand(1, 10); + for($j = 0; $j < $numParams; $j++) { + $char = array_shift($charArray); + + $params []= "-" + . $char + . "=" + . uniqid(); + } + + $dataSet []= $params; + } + + return $dataSet; + } } \ No newline at end of file