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
32 changes: 32 additions & 0 deletions app/Commander.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Syntatis\Version\CLI;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Syntatis\Version\CLI\Commands\ValidateCommand;

final class Commander extends Application
{
public const VERSION = '0.1.0-alpha.0';

public function __construct()
{
parent::__construct('Ver', self::VERSION);

$this->addCommands($this->getCommands());
}

/**
* @return array<Command>
* @phpstan-return list<Command>
*/
private function getCommands(): array
{
return [
new ValidateCommand(),
];
}
}
54 changes: 54 additions & 0 deletions app/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Syntatis\Version\CLI\Commands;

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

use function gettype;
use function is_string;
use function sprintf;

final class ValidateCommand extends Command
{
/**
* Configure the command options and arguments.
*/
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');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
$version = $input->getArgument('version');

try {
if (is_string($version)) {
Version::fromString($version);
$style->success(sprintf("Version string '%s' is valid and can be parsed", $version));

return Command::SUCCESS;
}

throw new TypeError(sprintf("Invalid type of value to validate. Expected string, '%s' given", gettype($version)));
} catch (Throwable $th) {
$style->error($th->getMessage());

return Command::FAILURE;
}
}
}
9 changes: 0 additions & 9 deletions app/Increment.php

This file was deleted.

14 changes: 13 additions & 1 deletion bin/version
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Console\Application;
use Syntatis\Version\CLI\Commander;

$autoloadFile = dirname(__DIR__, 4) . '/vendor/autoload.php';

if (file_exists($autoloadFile)) {
require_once $autoloadFile;
} else {
require_once dirname(__DIR__) . '/vendor/autoload.php';
}

$app = new Commander();
$app->run();
10 changes: 0 additions & 10 deletions inc/functions.php

This file was deleted.

1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

<!-- The directories to apply the rules -->
<file>./app/</file>
<file>./inc/</file>

<!-- Show the warning but exit with 0. The Warning is fine -->
<config name="ignore_warnings_on_exit" value="1"/>
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ parameters:
level: 10
paths:
- app
- inc
- tests/app
ignoreErrors:
-
identifier: missingType.iterableValue
path: tests/*
63 changes: 63 additions & 0 deletions tests/app/Commands/ValidateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 ValidateCommandTest extends TestCase
{
private Commander $commander;

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

$this->commander = new Commander();
}

/** @dataProvider dataInvalidVersionArgument */
public function testInvalidVersionArgument(string $version): void
{
$tester = new CommandTester($this->commander->get('validate'));
$tester->execute(['version' => $version]);

self::assertStringContainsString(
sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version),
$tester->getDisplay(),
);
}

/** @dataProvider dataValidVersionArgument */
public function testValidVersionArgument(string $version): void
{
$tester = new CommandTester($this->commander->get('validate'));
$tester->execute(['version' => $version]);

self::assertStringContainsString(
sprintf("[OK] Version string '%s' is valid and can be parsed", $version),
$tester->getDisplay(),
);
}

public static function dataInvalidVersionArgument(): iterable
{
yield ['v'];
yield ['v0'];
yield ['v0.0'];
}

public static function dataValidVersionArgument(): iterable
{
yield ['1.0.0'];
yield ['2.1.7-alpha'];
yield ['1.0.0-beta.1'];
yield ['3.4.5+build.78'];
yield ['0.9.1-alpha.1+exp.sha.5114f85'];
}
}
15 changes: 0 additions & 15 deletions tests/app/IncrementTest.php

This file was deleted.