diff --git a/app/Commander.php b/app/Commander.php new file mode 100644 index 0000000..73a566a --- /dev/null +++ b/app/Commander.php @@ -0,0 +1,32 @@ +addCommands($this->getCommands()); + } + + /** + * @return array + * @phpstan-return list + */ + private function getCommands(): array + { + return [ + new ValidateCommand(), + ]; + } +} diff --git a/app/Commands/ValidateCommand.php b/app/Commands/ValidateCommand.php new file mode 100644 index 0000000..ae48ae9 --- /dev/null +++ b/app/Commands/ValidateCommand.php @@ -0,0 +1,54 @@ +setName('validate'); + $this->setDescription('Validate a version.'); + $this->setHelp('This command checks if the provided value is a valid Semantic Version (SemVer) version format.'); + $this->setAliases(['val']); + $this->addArgument('version', InputArgument::REQUIRED, 'Version to validate'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $style = new SymfonyStyle($input, $output); + $version = $input->getArgument('version'); + + try { + if (is_string($version)) { + Version::fromString($version); + $style->success(sprintf("Version string '%s' is valid and can be parsed", $version)); + + return Command::SUCCESS; + } + + throw new TypeError(sprintf("Invalid type of value to validate. Expected string, '%s' given", gettype($version))); + } catch (Throwable $th) { + $style->error($th->getMessage()); + + return Command::FAILURE; + } + } +} diff --git a/app/Increment.php b/app/Increment.php deleted file mode 100644 index 51aed88..0000000 --- a/app/Increment.php +++ /dev/null @@ -1,9 +0,0 @@ -run(); diff --git a/inc/functions.php b/inc/functions.php deleted file mode 100644 index a4de206..0000000 --- a/inc/functions.php +++ /dev/null @@ -1,10 +0,0 @@ - ./app/ - ./inc/ diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a281b06..6fcd983 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,5 +2,8 @@ parameters: level: 10 paths: - app - - inc - tests/app + ignoreErrors: + - + identifier: missingType.iterableValue + path: tests/* diff --git a/tests/app/Commands/ValidateCommandTest.php b/tests/app/Commands/ValidateCommandTest.php new file mode 100644 index 0000000..27fac63 --- /dev/null +++ b/tests/app/Commands/ValidateCommandTest.php @@ -0,0 +1,63 @@ +commander = new Commander(); + } + + /** @dataProvider dataInvalidVersionArgument */ + public function testInvalidVersionArgument(string $version): void + { + $tester = new CommandTester($this->commander->get('validate')); + $tester->execute(['version' => $version]); + + self::assertStringContainsString( + sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version), + $tester->getDisplay(), + ); + } + + /** @dataProvider dataValidVersionArgument */ + public function testValidVersionArgument(string $version): void + { + $tester = new CommandTester($this->commander->get('validate')); + $tester->execute(['version' => $version]); + + self::assertStringContainsString( + sprintf("[OK] Version string '%s' is valid and can be parsed", $version), + $tester->getDisplay(), + ); + } + + public static function dataInvalidVersionArgument(): iterable + { + yield ['v']; + yield ['v0']; + yield ['v0.0']; + } + + public static function dataValidVersionArgument(): iterable + { + yield ['1.0.0']; + yield ['2.1.7-alpha']; + yield ['1.0.0-beta.1']; + yield ['3.4.5+build.78']; + yield ['0.9.1-alpha.1+exp.sha.5114f85']; + } +} diff --git a/tests/app/IncrementTest.php b/tests/app/IncrementTest.php deleted file mode 100644 index 8eb8c1a..0000000 --- a/tests/app/IncrementTest.php +++ /dev/null @@ -1,15 +0,0 @@ -assertTrue(true); - } -}