Skip to content

Commit c5da7e5

Browse files
authored
Add validate command (#1)
1 parent da50570 commit c5da7e5

9 files changed

Lines changed: 166 additions & 37 deletions

File tree

app/Commander.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Version\CLI;
6+
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Command\Command;
9+
use Syntatis\Version\CLI\Commands\ValidateCommand;
10+
11+
final class Commander extends Application
12+
{
13+
public const VERSION = '0.1.0-alpha.0';
14+
15+
public function __construct()
16+
{
17+
parent::__construct('Ver', self::VERSION);
18+
19+
$this->addCommands($this->getCommands());
20+
}
21+
22+
/**
23+
* @return array<Command>
24+
* @phpstan-return list<Command>
25+
*/
26+
private function getCommands(): array
27+
{
28+
return [
29+
new ValidateCommand(),
30+
];
31+
}
32+
}

app/Commands/ValidateCommand.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Version\CLI\Commands;
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\Style\SymfonyStyle;
12+
use Throwable;
13+
use TypeError;
14+
use Version\Version;
15+
16+
use function gettype;
17+
use function is_string;
18+
use function sprintf;
19+
20+
final class ValidateCommand extends Command
21+
{
22+
/**
23+
* Configure the command options and arguments.
24+
*/
25+
protected function configure(): void
26+
{
27+
$this->setName('validate');
28+
$this->setDescription('Validate a version.');
29+
$this->setHelp('This command checks if the provided value is a valid Semantic Version (SemVer) version format.');
30+
$this->setAliases(['val']);
31+
$this->addArgument('version', InputArgument::REQUIRED, 'Version to validate');
32+
}
33+
34+
protected function execute(InputInterface $input, OutputInterface $output): int
35+
{
36+
$style = new SymfonyStyle($input, $output);
37+
$version = $input->getArgument('version');
38+
39+
try {
40+
if (is_string($version)) {
41+
Version::fromString($version);
42+
$style->success(sprintf("Version string '%s' is valid and can be parsed", $version));
43+
44+
return Command::SUCCESS;
45+
}
46+
47+
throw new TypeError(sprintf("Invalid type of value to validate. Expected string, '%s' given", gettype($version)));
48+
} catch (Throwable $th) {
49+
$style->error($th->getMessage());
50+
51+
return Command::FAILURE;
52+
}
53+
}
54+
}

app/Increment.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

bin/version

100644100755
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
#!/usr/bin/env php
22
<?php
33

4-
require __DIR__ . '/../vendor/autoload.php';
4+
use Symfony\Component\Console\Application;
5+
use Syntatis\Version\CLI\Commander;
6+
7+
$autoloadFile = dirname(__DIR__, 4) . '/vendor/autoload.php';
8+
9+
if (file_exists($autoloadFile)) {
10+
require_once $autoloadFile;
11+
} else {
12+
require_once dirname(__DIR__) . '/vendor/autoload.php';
13+
}
14+
15+
$app = new Commander();
16+
$app->run();

inc/functions.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

phpcs.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
<!-- The directories to apply the rules -->
1717
<file>./app/</file>
18-
<file>./inc/</file>
1918

2019
<!-- Show the warning but exit with 0. The Warning is fine -->
2120
<config name="ignore_warnings_on_exit" value="1"/>

phpstan.neon.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ parameters:
22
level: 10
33
paths:
44
- app
5-
- inc
65
- tests/app
6+
ignoreErrors:
7+
-
8+
identifier: missingType.iterableValue
9+
path: tests/*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 ValidateCommandTest extends TestCase
14+
{
15+
private Commander $commander;
16+
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->commander = new Commander();
22+
}
23+
24+
/** @dataProvider dataInvalidVersionArgument */
25+
public function testInvalidVersionArgument(string $version): void
26+
{
27+
$tester = new CommandTester($this->commander->get('validate'));
28+
$tester->execute(['version' => $version]);
29+
30+
self::assertStringContainsString(
31+
sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version),
32+
$tester->getDisplay(),
33+
);
34+
}
35+
36+
/** @dataProvider dataValidVersionArgument */
37+
public function testValidVersionArgument(string $version): void
38+
{
39+
$tester = new CommandTester($this->commander->get('validate'));
40+
$tester->execute(['version' => $version]);
41+
42+
self::assertStringContainsString(
43+
sprintf("[OK] Version string '%s' is valid and can be parsed", $version),
44+
$tester->getDisplay(),
45+
);
46+
}
47+
48+
public static function dataInvalidVersionArgument(): iterable
49+
{
50+
yield ['v'];
51+
yield ['v0'];
52+
yield ['v0.0'];
53+
}
54+
55+
public static function dataValidVersionArgument(): iterable
56+
{
57+
yield ['1.0.0'];
58+
yield ['2.1.7-alpha'];
59+
yield ['1.0.0-beta.1'];
60+
yield ['3.4.5+build.78'];
61+
yield ['0.9.1-alpha.1+exp.sha.5114f85'];
62+
}
63+
}

tests/app/IncrementTest.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)