Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +32,7 @@ private function getCommands(): array
new IncrementCommand(),
new ValidateCommand(),
new GreaterThanCommand(),
new LessThanCommand(),
];
}
}
13 changes: 3 additions & 10 deletions app/Commands/GreaterThanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
68 changes: 68 additions & 0 deletions app/Commands/LessThanCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

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 Throwable;
use Version\Version;

use function sprintf;

final class LessThanCommand extends Command
{
/**
* Configure the command options and arguments.
*/
protected function configure(): void
{
$this->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:
<info>version lt 0.9.0 1.0.0</info>
<info>version lt 2.1.0 2.1.0</info>
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;
}
}
}
14 changes: 5 additions & 9 deletions app/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());

Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/Commands/LessThanCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Syntatis\Tests\Commands;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Syntatis\Version\CLI\Commander;

use function sprintf;

class LessThanCommandTest extends TestCase
{
private Commander $commander;
private CommandTester $tester;

public function setUp(): void
{
parent::setUp();

$this->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')];
}
}