diff --git a/app/Commander.php b/app/Commander.php index 6ba4b4d..511c0e9 100644 --- a/app/Commander.php +++ b/app/Commander.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Command\Command; use Syntatis\Version\CLI\Commands\GreaterThanCommand; use Syntatis\Version\CLI\Commands\IncrementCommand; +use Syntatis\Version\CLI\Commands\LessThanCommand; use Syntatis\Version\CLI\Commands\ValidateCommand; final class Commander extends Application @@ -31,6 +32,7 @@ private function getCommands(): array new IncrementCommand(), new ValidateCommand(), new GreaterThanCommand(), + new LessThanCommand(), ]; } } diff --git a/app/Commands/GreaterThanCommand.php b/app/Commands/GreaterThanCommand.php index 70a6280..1da27eb 100644 --- a/app/Commands/GreaterThanCommand.php +++ b/app/Commands/GreaterThanCommand.php @@ -4,16 +4,15 @@ namespace Syntatis\Version\CLI\Commands; +use Assert\Assertion; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use Syntatis\Version\CLI\Exceptions\InvalidArgumentType; use Throwable; use Version\Version; -use function is_string; use function sprintf; final class GreaterThanCommand extends Command @@ -43,17 +42,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $versionB = $input->getArgument('version-b'); try { - if (! is_string($versionA)) { - throw new InvalidArgumentType($versionA); - } - - if (! is_string($versionB)) { - throw new InvalidArgumentType($versionB); - } + Assertion::string($versionA); + Assertion::string($versionB); /** @var Version $a */ $a = Version::fromString($versionA); - /** @var Version $b */ $b = Version::fromString($versionB); diff --git a/app/Commands/LessThanCommand.php b/app/Commands/LessThanCommand.php new file mode 100644 index 0000000..8b591ee --- /dev/null +++ b/app/Commands/LessThanCommand.php @@ -0,0 +1,68 @@ +setName('lt'); + $this->setDescription('Compare if a version is less than 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 less than the second. + + Usage: + version lt 0.9.0 1.0.0 + version lt 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->isLessThan($b)) { + $style->success(sprintf("Version '%s' is less than '%s'.", $a, $b)); + + return Command::SUCCESS; + } + + $style->error(sprintf("Version '%s' is not less than '%s'.", $a, $b)); + + return Command::FAILURE; + } catch (Throwable $th) { + $style->error($th->getMessage()); + + return Command::FAILURE; + } + } +} diff --git a/app/Commands/ValidateCommand.php b/app/Commands/ValidateCommand.php index 7d62b47..74f44bb 100644 --- a/app/Commands/ValidateCommand.php +++ b/app/Commands/ValidateCommand.php @@ -4,16 +4,15 @@ namespace Syntatis\Version\CLI\Commands; +use Assert\Assertion; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use Syntatis\Version\CLI\Exceptions\InvalidArgumentType; use Throwable; use Version\Version; -use function is_string; use function sprintf; final class ValidateCommand extends Command @@ -36,15 +35,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $version = $input->getArgument('version'); try { - if (is_string($version)) { - Version::fromString($version); + Assertion::string($version); + Version::fromString($version); - $style->success(sprintf("Version string '%s' is valid and can be parsed", $version)); + $style->success(sprintf("Version string '%s' is valid and can be parsed", $version)); - return Command::SUCCESS; - } - - throw new InvalidArgumentType($version); + return Command::SUCCESS; } catch (Throwable $th) { $style->error($th->getMessage()); diff --git a/composer.json b/composer.json index 38f57f5..f0137d7 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ }, "require": { "php": "^7.4 || ^8.0", + "beberlei/assert": "^3.3", "nikolaposa/version": "^4.1.1", "symfony/console": "^5.4 || ^6.0 || ^7.0" }, @@ -37,6 +38,7 @@ "phpcompatibility/php-compatibility": "^9.3", "phpstan/extension-installer": "^1.4", "phpstan/phpstan": "^2.1", + "phpstan/phpstan-beberlei-assert": "^2.0", "phpstan/phpstan-deprecation-rules": "^2.0", "phpstan/phpstan-phpunit": "^2.0", "phpstan/phpstan-strict-rules": "^2.0", diff --git a/tests/phpunit/Commands/LessThanCommandTest.php b/tests/phpunit/Commands/LessThanCommandTest.php new file mode 100644 index 0000000..c125bb7 --- /dev/null +++ b/tests/phpunit/Commands/LessThanCommandTest.php @@ -0,0 +1,45 @@ +commander = new Commander(); + $this->tester = new CommandTester($this->commander->get('lt')); + } + + /** @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', '2.0.0', sprintf("[OK] Version '%s' is less than '%s'.", '1.0.0', '2.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')]; + yield ['1.0.0-alpha.2', '1.0.0-alpha.1', sprintf("[ERROR] Version '%s' is not less than '%s'.", '1.0.0-alpha.2', '1.0.0-alpha.1')]; + } +}