diff --git a/src/Cli/Attribute/InvalidArgument.php b/src/Cli/Attribute/InvalidArgument.php new file mode 100644 index 0000000..5578186 --- /dev/null +++ b/src/Cli/Attribute/InvalidArgument.php @@ -0,0 +1,13 @@ +assertIsValid(); } public function equalTo(self $that): bool @@ -21,4 +22,15 @@ public function __toString(): string { return $this->value; } + + private function assertIsValid(): void + { + $alphaNumeric = 'A-Za-z0-9'; + $allowed = "$alphaNumeric-_"; + $word = "[$alphaNumeric][$allowed]*"; + + if (0 === preg_match("/^$word(:$word)*$/", $this->value)) { + throw new InvalidArgument($this->value); + } + } } diff --git a/src/Cli/Command.php b/src/Cli/Command.php index bc956aa..3b86bfe 100644 --- a/src/Cli/Command.php +++ b/src/Cli/Command.php @@ -35,7 +35,7 @@ public function execute(Input $input, Output $output): void public function match(Input $input): bool { - return $this->id->equalTo($input->commandId()); + return $this->id->equalTo($input->commandName()); } private function extractEntrypointAttributes(): void diff --git a/src/Cli/Input.php b/src/Cli/Input.php index 03cf11b..59e0f07 100644 --- a/src/Cli/Input.php +++ b/src/Cli/Input.php @@ -7,7 +7,7 @@ final class Input { private array $argv; - private Name $commandId; + private Name $commandName; private function __construct(string ...$rawInput) { @@ -25,13 +25,13 @@ public static function fromString(string $string): self return new self(...explode(' ', $string)); } - public function commandId(): Name + public function commandName(): Name { - return $this->commandId; + return $this->commandName; } private function parseInput(): void { - $this->commandId = new Name($this->argv[1]); + $this->commandName = new Name($this->argv[1]); } } diff --git a/tests/Cli/Attribute/NameTest.php b/tests/Cli/Attribute/NameTest.php new file mode 100644 index 0000000..42b26bd --- /dev/null +++ b/tests/Cli/Attribute/NameTest.php @@ -0,0 +1,78 @@ +expectException(InvalidArgument::class); + $this->expectExceptionMessage("Invalid cli argument: '$invalid'"); + + new Name($invalid); + } + + public function validNames(): array + { + return [ + ['valid'], + ['val:id'], + ['va:aa:lid'], + ['v:aa:lid'], + ['v:a:lid'], + ['val_id'], + ['val-id'], + ['val-i_d'], + ['val_-id'], + ]; + } + + /** + * @test + * @dataProvider validNames + */ + public function convertToString(string $str): void + { + $name = new Name($str); + + $this->assertEquals($str, (string) $name); + } + + /** + * @test + */ + public function returnTrueIfTwoNameAreEqual(): void + { + $name = new Name('something'); + + $this->assertTrue($name->equalTo(new Name('something'))); + } + + /** + * @test + */ + public function returnFalseIfTwoNameAreNotEqual(): void + { + $name = new Name('something'); + + $this->assertFalse($name->equalTo(new Name('anotherThing'))); + } +} diff --git a/tests/Cli/InputTest.php b/tests/Cli/InputTest.php index f9aff39..ca5e3c0 100644 --- a/tests/Cli/InputTest.php +++ b/tests/Cli/InputTest.php @@ -15,7 +15,7 @@ public function determineCommandIdFromArgv(): void $expected = new Name('command-id'); $input = Input::fromArgv('./bin/console', 'command-id'); - $commandId = $input->commandId(); + $commandId = $input->commandName(); $this->assertEquals($expected, $commandId); }