Skip to content

Commit ca80289

Browse files
authored
Add eq command (#5)
1 parent c78dbaf commit ca80289

7 files changed

Lines changed: 128 additions & 3 deletions

File tree

app/Commander.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Symfony\Component\Console\Application;
88
use Symfony\Component\Console\Command\Command;
9+
use Syntatis\Version\CLI\Commands\EqualCommand;
910
use Syntatis\Version\CLI\Commands\GreaterThanCommand;
1011
use Syntatis\Version\CLI\Commands\IncrementCommand;
1112
use Syntatis\Version\CLI\Commands\LessThanCommand;
@@ -29,10 +30,11 @@ public function __construct()
2930
private function getCommands(): array
3031
{
3132
return [
32-
new IncrementCommand(),
33-
new ValidateCommand(),
33+
new EqualCommand(),
3434
new GreaterThanCommand(),
35+
new IncrementCommand(),
3536
new LessThanCommand(),
37+
new ValidateCommand(),
3638
];
3739
}
3840
}

app/Commands/EqualCommand.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Version\CLI\Commands;
6+
7+
use Assert\Assertion;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
13+
use Throwable;
14+
use Version\Version;
15+
16+
use function sprintf;
17+
18+
final class EqualCommand extends Command
19+
{
20+
/**
21+
* Configure the command options and arguments.
22+
*/
23+
protected function configure(): void
24+
{
25+
$this->setName('eq');
26+
$this->setDescription('Compare if a version is equal to another');
27+
$this->addArgument('version-a', InputArgument::REQUIRED, 'First version to compare');
28+
$this->addArgument('version-b', InputArgument::REQUIRED, 'Second version to compare against the first');
29+
$this->setHelp(<<<'HELP'
30+
This command compares two versions and checks if the first version is equal to the second.
31+
32+
Usage:
33+
<info>version eq 1.0.0 0.9.0</info>
34+
<info>version eq 2.1.0 2.1.0</info>
35+
HELP);
36+
}
37+
38+
protected function execute(InputInterface $input, OutputInterface $output): int
39+
{
40+
$style = new SymfonyStyle($input, $output);
41+
$versionA = $input->getArgument('version-a');
42+
$versionB = $input->getArgument('version-b');
43+
44+
try {
45+
Assertion::string($versionA);
46+
Assertion::string($versionB);
47+
48+
/** @var Version $a */
49+
$a = Version::fromString($versionA);
50+
/** @var Version $b */
51+
$b = Version::fromString($versionB);
52+
53+
if ($a->isEqualTo($b)) {
54+
$style->success(sprintf("Version '%s' is equal to '%s'.", $a, $b));
55+
56+
return Command::SUCCESS;
57+
}
58+
59+
$style->error(sprintf("Version '%s' is not equal to '%s'.", $a, $b));
60+
61+
return Command::FAILURE;
62+
} catch (Throwable $th) {
63+
$style->error($th->getMessage());
64+
65+
return Command::FAILURE;
66+
}
67+
}
68+
}

app/Commands/ValidateCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ protected function configure(): void
2424
{
2525
$this->setName('validate');
2626
$this->setDescription('Validate a version');
27-
$this->setHelp('This command checks if the provided value is a valid Semantic Version (SemVer) version format.');
2827
$this->setAliases(['val']);
2928
$this->addArgument('version', InputArgument::REQUIRED, 'Version to validate');
29+
$this->setHelp(<<<'HELP'
30+
This command validates a version whether it is a valid Semantic Versioning (Semver) string.
31+
32+
Usage:
33+
<info>version validate 1.0.0</info>
34+
<info>version validate v1.0.0</info>
35+
HELP);
3036
}
3137

3238
protected function execute(InputInterface $input, OutputInterface $output): int
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Tests\Commands;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Tester\CommandTester;
9+
use Syntatis\Version\CLI\Commander;
10+
11+
use function sprintf;
12+
13+
class EqualCommandTest extends TestCase
14+
{
15+
private Commander $commander;
16+
private CommandTester $tester;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->commander = new Commander();
23+
$this->tester = new CommandTester($this->commander->get('eq'));
24+
}
25+
26+
/** @dataProvider dataComparison */
27+
public function testComparison(string $versionA, string $versionB, string $expect): void
28+
{
29+
$this->tester->execute(['version-a' => $versionA, 'version-b' => $versionB]);
30+
31+
self::assertStringContainsString(
32+
$expect,
33+
$this->tester->getDisplay(),
34+
);
35+
}
36+
37+
public static function dataComparison(): iterable
38+
{
39+
yield ['1.0.0', '1.0.0', sprintf("[OK] Version '%s' is equal to '%s'.", '1.0.0', '1.0.0')];
40+
yield ['v1.0.0', 'v1.0.0', sprintf("[OK] Version '%s' is equal to '%s'.", 'v1.0.0', 'v1.0.0')];
41+
yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not equal to '%s'.", '1.0.0', '2.0.0')];
42+
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')];
43+
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')];
44+
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')];
45+
}
46+
}

tests/phpunit/Commands/GreaterThanCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static function dataComparison(): iterable
3838
{
3939
yield ['1.0.0', '2.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '2.0.0')];
4040
yield ['2.0.0', '1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", '2.0.0', '1.0.0')];
41+
yield ['v2.0.0', 'v1.0.0', sprintf("[OK] Version '%s' is greater than '%s'.", 'v2.0.0', 'v1.0.0')];
4142
yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not greater than '%s'.", '1.0.0', '1.0.0')];
4243
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')];
4344
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')];

tests/phpunit/Commands/LessThanCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testComparison(string $versionA, string $versionB, string $expec
3737
public static function dataComparison(): iterable
3838
{
3939
yield ['1.0.0', '2.0.0', sprintf("[OK] Version '%s' is less than '%s'.", '1.0.0', '2.0.0')];
40+
yield ['v1.0.0', 'v2.0.0', sprintf("[OK] Version '%s' is less than '%s'.", 'v1.0.0', 'v2.0.0')];
4041
yield ['2.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not less than '%s'.", '2.0.0', '1.0.0')];
4142
yield ['1.0.0', '1.0.0', sprintf("[ERROR] Version '%s' is not less than '%s'.", '1.0.0', '1.0.0')];
4243
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')];

tests/phpunit/Commands/ValidateCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static function dataInvalidVersionArgument(): iterable
5555
public static function dataValidVersionArgument(): iterable
5656
{
5757
yield ['1.0.0'];
58+
yield ['v1.0.0'];
5859
yield ['2.1.7-alpha'];
5960
yield ['1.0.0-beta.1'];
6061
yield ['3.4.5+build.78'];

0 commit comments

Comments
 (0)