From 952dc3547cc23a27d56e5c03a7f9cda025567de0 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Sun, 27 Jul 2025 23:06:41 +0700 Subject: [PATCH 1/6] Add `validate` command --- app/Commander.php | 32 +++++++++++++ app/Commands/ValidateCommand.php | 48 +++++++++++++++++++ app/Increment.php | 9 ---- bin/ver | 16 +++++++ bin/version | 4 -- inc/functions.php | 10 ---- tests/app/Commands/ValidateCommandTest.php | 56 ++++++++++++++++++++++ tests/app/IncrementTest.php | 15 ------ 8 files changed, 152 insertions(+), 38 deletions(-) create mode 100644 app/Commander.php create mode 100644 app/Commands/ValidateCommand.php delete mode 100644 app/Increment.php create mode 100755 bin/ver delete mode 100644 bin/version delete mode 100644 inc/functions.php create mode 100644 tests/app/Commands/ValidateCommandTest.php delete mode 100644 tests/app/IncrementTest.php 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..49dc11b --- /dev/null +++ b/app/Commands/ValidateCommand.php @@ -0,0 +1,48 @@ +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 { + $version = Version::fromString($version); + } catch (Throwable $th) { + $style->error($th->getMessage()); + + return Command::FAILURE; + } + + $style->success(sprintf("Version string '%s' is valid and can be parsed", $version)); + + return Command::SUCCESS; + } +} 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/bin/version b/bin/version deleted file mode 100644 index 77f5404..0000000 --- a/bin/version +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env php -get('validate')); + $tester->execute(['version' => $version]); + + $this->assertStringContainsString( + sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version), + $tester->getDisplay(), + ); + } + + /** @dataProvider dataValidVersionArgument */ + public function testValidVersionArgument(string $version): void + { + $command = new Commander(); + $tester = new CommandTester($command->get('validate')); + $tester->execute(['version' => $version]); + + $this->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); - } -} From e86fd8edf2c5489aabb62d9b7a60b20c9d663c94 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Sun, 27 Jul 2025 23:11:10 +0700 Subject: [PATCH 2/6] Fix PHPCS and PHPStan config for missing dir --- phpcs.xml.dist | 1 - phpstan.neon.dist | 1 - 2 files changed, 2 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 0650374..8e9ec78 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -15,7 +15,6 @@ ./app/ - ./inc/ diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a281b06..98ae512 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,5 +2,4 @@ parameters: level: 10 paths: - app - - inc - tests/app From 6d0b0b2ea502d510bcd0e24516db828a97e4a2c1 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Sun, 27 Jul 2025 23:17:21 +0700 Subject: [PATCH 3/6] Fix some static errors --- app/Commands/ValidateCommand.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Commands/ValidateCommand.php b/app/Commands/ValidateCommand.php index 49dc11b..ae48ae9 100644 --- a/app/Commands/ValidateCommand.php +++ b/app/Commands/ValidateCommand.php @@ -10,8 +10,11 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Throwable; +use TypeError; use Version\Version; +use function gettype; +use function is_string; use function sprintf; final class ValidateCommand extends Command @@ -34,15 +37,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int $version = $input->getArgument('version'); try { - $version = Version::fromString($version); + 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; } - - $style->success(sprintf("Version string '%s' is valid and can be parsed", $version)); - - return Command::SUCCESS; } } From 2774f55359d3498ff852ca82cfc29c74b5b63407 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Mon, 28 Jul 2025 07:28:02 +0700 Subject: [PATCH 4/6] Fix static errors --- phpstan.neon.dist | 4 ++++ tests/app/Commands/ValidateCommandTest.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 98ae512..6fcd983 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,3 +3,7 @@ parameters: paths: - app - tests/app + ignoreErrors: + - + identifier: missingType.iterableValue + path: tests/* diff --git a/tests/app/Commands/ValidateCommandTest.php b/tests/app/Commands/ValidateCommandTest.php index 936233a..429f844 100644 --- a/tests/app/Commands/ValidateCommandTest.php +++ b/tests/app/Commands/ValidateCommandTest.php @@ -19,7 +19,7 @@ public function testInvalidVersionArgument(string $version): void $tester = new CommandTester($command->get('validate')); $tester->execute(['version' => $version]); - $this->assertStringContainsString( + self::assertStringContainsString( sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version), $tester->getDisplay(), ); @@ -32,7 +32,7 @@ public function testValidVersionArgument(string $version): void $tester = new CommandTester($command->get('validate')); $tester->execute(['version' => $version]); - $this->assertStringContainsString( + self::assertStringContainsString( sprintf("[OK] Version string '%s' is valid and can be parsed", $version), $tester->getDisplay(), ); From 7157b8aba971c537c581e560bc511c41bb3bc42f Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Mon, 28 Jul 2025 07:30:08 +0700 Subject: [PATCH 5/6] Add setUp --- tests/app/Commands/ValidateCommandTest.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/app/Commands/ValidateCommandTest.php b/tests/app/Commands/ValidateCommandTest.php index 429f844..27fac63 100644 --- a/tests/app/Commands/ValidateCommandTest.php +++ b/tests/app/Commands/ValidateCommandTest.php @@ -12,11 +12,19 @@ class ValidateCommandTest extends TestCase { + private Commander $commander; + + public function setUp(): void + { + parent::setUp(); + + $this->commander = new Commander(); + } + /** @dataProvider dataInvalidVersionArgument */ public function testInvalidVersionArgument(string $version): void { - $command = new Commander(); - $tester = new CommandTester($command->get('validate')); + $tester = new CommandTester($this->commander->get('validate')); $tester->execute(['version' => $version]); self::assertStringContainsString( @@ -28,8 +36,7 @@ public function testInvalidVersionArgument(string $version): void /** @dataProvider dataValidVersionArgument */ public function testValidVersionArgument(string $version): void { - $command = new Commander(); - $tester = new CommandTester($command->get('validate')); + $tester = new CommandTester($this->commander->get('validate')); $tester->execute(['version' => $version]); self::assertStringContainsString( From 9c015129990a1f30f06f9d5f2d59298ddeadde8e Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Mon, 28 Jul 2025 07:32:51 +0700 Subject: [PATCH 6/6] Rename to `version` --- bin/{ver => version} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bin/{ver => version} (100%) diff --git a/bin/ver b/bin/version similarity index 100% rename from bin/ver rename to bin/version