From c459cfb9ac259c3d6ad522dbe4388c8b52b88e61 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Tue, 29 Jul 2025 08:40:14 +0700 Subject: [PATCH 1/3] Add tests with `v` prefixed value --- tests/phpunit/Commands/GreaterThanCommandTest.php | 1 + tests/phpunit/Commands/LessThanCommandTest.php | 1 + tests/phpunit/Commands/ValidateCommandTest.php | 1 + 3 files changed, 3 insertions(+) diff --git a/tests/phpunit/Commands/GreaterThanCommandTest.php b/tests/phpunit/Commands/GreaterThanCommandTest.php index 0bc1d6d..49d7bea 100644 --- a/tests/phpunit/Commands/GreaterThanCommandTest.php +++ b/tests/phpunit/Commands/GreaterThanCommandTest.php @@ -38,6 +38,7 @@ public static function dataComparison(): iterable { yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '2.0.0')]; yield ['2.0.0', '1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", '2.0.0', '1.0.0')]; + yield ['v2.0.0', 'v1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", 'v2.0.0', 'v1.0.0')]; yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '1.0.0')]; yield ['1.0.0-alpha.1', '1.0.0-alpha.2', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0-alpha.1', '1.0.0-alpha.2')]; yield ['1.0.0-alpha.2', '1.0.0-alpha.1', sprintf("[OK] Version '%s' is greater than '%s'.", '1.0.0-alpha.2', '1.0.0-alpha.1')]; diff --git a/tests/phpunit/Commands/LessThanCommandTest.php b/tests/phpunit/Commands/LessThanCommandTest.php index c125bb7..54e96d9 100644 --- a/tests/phpunit/Commands/LessThanCommandTest.php +++ b/tests/phpunit/Commands/LessThanCommandTest.php @@ -37,6 +37,7 @@ public function testComparison(string $versionA, string $versionB, string $expec public static function dataComparison(): iterable { yield ['1.0.0', '2.0.0', sprintf("[OK] Version '%s' is less than '%s'.", '1.0.0', '2.0.0')]; + yield ['v1.0.0', 'v2.0.0', sprintf("[OK] Version '%s' is less than '%s'.", 'v1.0.0', 'v2.0.0')]; yield ['2.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not less than '%s'.", '2.0.0', '1.0.0')]; yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not less than '%s'.", '1.0.0', '1.0.0')]; yield ['1.0.0-alpha.1', '1.0.0-alpha.2', sprintf("[OK] Version '%s' is less than '%s'.", '1.0.0-alpha.1', '1.0.0-alpha.2')]; diff --git a/tests/phpunit/Commands/ValidateCommandTest.php b/tests/phpunit/Commands/ValidateCommandTest.php index dc09e52..823782d 100644 --- a/tests/phpunit/Commands/ValidateCommandTest.php +++ b/tests/phpunit/Commands/ValidateCommandTest.php @@ -55,6 +55,7 @@ public static function dataInvalidVersionArgument(): iterable public static function dataValidVersionArgument(): iterable { yield ['1.0.0']; + yield ['v1.0.0']; yield ['2.1.7-alpha']; yield ['1.0.0-beta.1']; yield ['3.4.5+build.78']; From 43710437d7d11389fbd22dc016dda2b7e6be228b Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Tue, 29 Jul 2025 08:41:28 +0700 Subject: [PATCH 2/3] Add help text --- app/Commands/ValidateCommand.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Commands/ValidateCommand.php b/app/Commands/ValidateCommand.php index 74f44bb..c07c987 100644 --- a/app/Commands/ValidateCommand.php +++ b/app/Commands/ValidateCommand.php @@ -24,9 +24,15 @@ protected function configure(): void { $this->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'); + $this->setHelp(<<<'HELP' + This command validates a version whether it is a valid Semantic Versioning (Semver) string. + + Usage: + version validate 1.0.0 + version validate v1.0.0 + HELP); } protected function execute(InputInterface $input, OutputInterface $output): int From 529b6e48f62cc81510ab89744c99e613e67067a7 Mon Sep 17 00:00:00 2001 From: Thoriq Firdaus <2067467+tfirdaus@users.noreply.github.com> Date: Tue, 29 Jul 2025 08:46:12 +0700 Subject: [PATCH 3/3] Add `eq` command --- app/Commander.php | 6 +- app/Commands/EqualCommand.php | 68 +++++++++++++++++++++ tests/phpunit/Commands/EqualCommandTest.php | 46 ++++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 app/Commands/EqualCommand.php create mode 100644 tests/phpunit/Commands/EqualCommandTest.php diff --git a/app/Commander.php b/app/Commander.php index 511c0e9..6ddbed7 100644 --- a/app/Commander.php +++ b/app/Commander.php @@ -6,6 +6,7 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; +use Syntatis\Version\CLI\Commands\EqualCommand; use Syntatis\Version\CLI\Commands\GreaterThanCommand; use Syntatis\Version\CLI\Commands\IncrementCommand; use Syntatis\Version\CLI\Commands\LessThanCommand; @@ -29,10 +30,11 @@ public function __construct() private function getCommands(): array { return [ - new IncrementCommand(), - new ValidateCommand(), + new EqualCommand(), new GreaterThanCommand(), + new IncrementCommand(), new LessThanCommand(), + new ValidateCommand(), ]; } } diff --git a/app/Commands/EqualCommand.php b/app/Commands/EqualCommand.php new file mode 100644 index 0000000..f5c70f3 --- /dev/null +++ b/app/Commands/EqualCommand.php @@ -0,0 +1,68 @@ +setName('eq'); + $this->setDescription('Compare if a version is equal to another'); + $this->addArgument('version-a', InputArgument::REQUIRED, 'First version to compare'); + $this->addArgument('version-b', InputArgument::REQUIRED, 'Second version to compare against the first'); + $this->setHelp(<<<'HELP' + This command compares two versions and checks if the first version is equal to the second. + + Usage: + version eq 1.0.0 0.9.0 + version eq 2.1.0 2.1.0 + HELP); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $style = new SymfonyStyle($input, $output); + $versionA = $input->getArgument('version-a'); + $versionB = $input->getArgument('version-b'); + + try { + Assertion::string($versionA); + Assertion::string($versionB); + + /** @var Version $a */ + $a = Version::fromString($versionA); + /** @var Version $b */ + $b = Version::fromString($versionB); + + if ($a->isEqualTo($b)) { + $style->success(sprintf("Version '%s' is equal to '%s'.", $a, $b)); + + return Command::SUCCESS; + } + + $style->error(sprintf("Version '%s' is not equal to '%s'.", $a, $b)); + + return Command::FAILURE; + } catch (Throwable $th) { + $style->error($th->getMessage()); + + return Command::FAILURE; + } + } +} diff --git a/tests/phpunit/Commands/EqualCommandTest.php b/tests/phpunit/Commands/EqualCommandTest.php new file mode 100644 index 0000000..c1075ca --- /dev/null +++ b/tests/phpunit/Commands/EqualCommandTest.php @@ -0,0 +1,46 @@ +commander = new Commander(); + $this->tester = new CommandTester($this->commander->get('eq')); + } + + /** @dataProvider dataComparison */ + public function testComparison(string $versionA, string $versionB, string $expect): void + { + $this->tester->execute(['version-a' => $versionA, 'version-b' => $versionB]); + + self::assertStringContainsString( + $expect, + $this->tester->getDisplay(), + ); + } + + public static function dataComparison(): iterable + { + yield ['1.0.0', '1.0.0', sprintf("[OK] Version '%s' is equal to '%s'.", '1.0.0', '1.0.0')]; + yield ['v1.0.0', 'v1.0.0', sprintf("[OK] Version '%s' is equal to '%s'.", 'v1.0.0', 'v1.0.0')]; + yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not equal to '%s'.", '1.0.0', '2.0.0')]; + yield ['1.0.0-alpha.1', '1.0.0-alpha.1', sprintf("[OK] Version '%s' is equal to '%s'.", '1.0.0-alpha.1', '1.0.0-alpha.1')]; + yield ['v1.0.0-alpha.1', 'v1.0.0-alpha.1', sprintf("[OK] Version '%s' is equal to '%s'.", 'v1.0.0-alpha.1', 'v1.0.0-alpha.1')]; + yield ['1.0.0-alpha.1', '1.0.0-alpha.2', sprintf("[ERROR] Version '%s' is not equal to '%s'.", '1.0.0-alpha.1', '1.0.0-alpha.2')]; + } +}