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
6 changes: 4 additions & 2 deletions app/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
];
}
}
68 changes: 68 additions & 0 deletions app/Commands/EqualCommand.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 EqualCommand extends Command
{
/**
* Configure the command options and arguments.
*/
protected function configure(): void
{
$this->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:
<info>version eq 1.0.0 0.9.0</info>
<info>version eq 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->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;
}
}
}
8 changes: 7 additions & 1 deletion app/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<info>version validate 1.0.0</info>
<info>version validate v1.0.0</info>
HELP);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/Commands/EqualCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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 EqualCommandTest 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('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')];
}
}
1 change: 1 addition & 0 deletions tests/phpunit/Commands/GreaterThanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')];
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/Commands/LessThanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')];
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/Commands/ValidateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down