Skip to content

Commit

Permalink
Merge pull request #61 from siketyan/skip-unsupported-version
Browse files Browse the repository at this point in the history
🩹 Skip the package with unknown version type
  • Loading branch information
siketyan authored Jan 30, 2021
2 parents 9cc2c4e + d684959 commit c28a938
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 43 deletions.
3 changes: 3 additions & 0 deletions config/versioning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ services:

Siketyan\Loxcan\Versioning\Simple\SimpleVersionComparator: ~

Siketyan\Loxcan\Versioning\Unknown\UnknownVersionComparator: ~

Siketyan\Loxcan\Versioning\VersionComparatorResolver:
arguments:
$comparators:
- '@Siketyan\Loxcan\Versioning\SemVer\SemVerVersionComparator'
- '@Siketyan\Loxcan\Versioning\Simple\SimpleVersionComparator'
- '@Siketyan\Loxcan\Versioning\Composer\ComposerVersionComparator'
- '@Siketyan\Loxcan\Versioning\Unknown\UnknownVersionComparator'
22 changes: 0 additions & 22 deletions src/Exception/UnsupportedVersionException.php

This file was deleted.

7 changes: 4 additions & 3 deletions src/Versioning/Composer/ComposerVersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Siketyan\Loxcan\Versioning\Composer;

use Siketyan\Loxcan\Exception\UnsupportedVersionException;
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;
use Siketyan\Loxcan\Versioning\VersionInterface;

class ComposerVersionParser
{
private const PATTERN = '/^v?(\d+)\.(\d+)\.(\d+)(?:-(dev|alpha|beta|RC)(\d+)?)?$/';

public function parse(string $version): ComposerVersion
public function parse(string $version): VersionInterface
{
if (!preg_match(self::PATTERN, $version, $matches)) {
throw new UnsupportedVersionException($version);
return new UnknownVersion($version);
}

return new ComposerVersion(
Expand Down
7 changes: 4 additions & 3 deletions src/Versioning/SemVer/SemVerVersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Siketyan\Loxcan\Versioning\SemVer;

use Siketyan\Loxcan\Exception\UnsupportedVersionException;
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;
use Siketyan\Loxcan\Versioning\VersionInterface;

class SemVerVersionParser
{
private const PATTERN = '/^(\d+)\.(\d+)\.(\d+)(?:\-(?<pre>[A-Za-z0-9\-\.]+))?(?:\+(?<build>[A-Za-z0-9\-\.]+))?$/';

public function parse(string $version): SemVerVersion
public function parse(string $version): VersionInterface
{
if (!preg_match(self::PATTERN, $version, $matches)) {
throw new UnsupportedVersionException($version);
return new UnknownVersion($version);
}

$preRelease = [];
Expand Down
7 changes: 4 additions & 3 deletions src/Versioning/Simple/SimpleVersionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Siketyan\Loxcan\Versioning\Simple;

use Siketyan\Loxcan\Exception\UnsupportedVersionException;
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;
use Siketyan\Loxcan\Versioning\VersionInterface;

class SimpleVersionParser
{
private const PATTERN = '/^(\d+)\.(\d+)\.(\d+)(?:\.(\d+))?$/';

public function parse(string $version): SimpleVersion
public function parse(string $version): VersionInterface
{
if (!preg_match(self::PATTERN, $version, $matches)) {
throw new UnsupportedVersionException($version);
return new UnknownVersion($version);
}

return new SimpleVersion(
Expand Down
23 changes: 23 additions & 0 deletions src/Versioning/Unknown/UnknownVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Siketyan\Loxcan\Versioning\Unknown;

use Siketyan\Loxcan\Versioning\VersionInterface;

class UnknownVersion implements VersionInterface
{
private string $version;

public function __construct(
string $version
) {
$this->version = $version;
}

public function __toString(): string
{
return $this->version;
}
}
28 changes: 28 additions & 0 deletions src/Versioning/Unknown/UnknownVersionComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Siketyan\Loxcan\Versioning\Unknown;

use Siketyan\Loxcan\Versioning\VersionComparatorInterface;
use Siketyan\Loxcan\Versioning\VersionDiff;
use Siketyan\Loxcan\Versioning\VersionInterface;

class UnknownVersionComparator implements VersionComparatorInterface
{
public function compare(VersionInterface $before, VersionInterface $after): ?VersionDiff
{
return new VersionDiff(
VersionDiff::UNKNOWN,
$before,
$after,
);
}

public function supports(string $beforeType, string $afterType): bool
{
return is_a($beforeType, VersionInterface::class, true)
&& is_a($afterType, VersionInterface::class, true)
;
}
}
9 changes: 5 additions & 4 deletions tests/Versioning/Composer/ComposerVersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Siketyan\Loxcan\Versioning\Composer;

use PHPUnit\Framework\TestCase;
use Siketyan\Loxcan\Exception\UnsupportedVersionException;
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;

class ComposerVersionParserTest extends TestCase
{
Expand Down Expand Up @@ -35,8 +35,9 @@ public function test(): void

public function testUnsupported(): void
{
$this->expectException(UnsupportedVersionException::class);

$this->parser->parse('v1.2.3_not_supported');
$this->assertInstanceOf(
UnknownVersion::class,
$this->parser->parse('v1.2.3_not_supported'),
);
}
}
9 changes: 5 additions & 4 deletions tests/Versioning/SemVer/SemVerVersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Siketyan\Loxcan\Versioning\SemVer;

use PHPUnit\Framework\TestCase;
use Siketyan\Loxcan\Exception\UnsupportedVersionException;
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;

class SemVerVersionParserTest extends TestCase
{
Expand Down Expand Up @@ -49,8 +49,9 @@ public function test(): void

public function testUnsupported(): void
{
$this->expectException(UnsupportedVersionException::class);

$this->parser->parse('v1.2.3_not_supported');
$this->assertInstanceOf(
UnknownVersion::class,
$this->parser->parse('v1.2.3_not_supported'),
);
}
}
9 changes: 5 additions & 4 deletions tests/Versioning/Simple/SimpleVersionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Siketyan\Loxcan\Versioning\Simple;

use PHPUnit\Framework\TestCase;
use Siketyan\Loxcan\Exception\UnsupportedVersionException;
use Siketyan\Loxcan\Versioning\Unknown\UnknownVersion;

class SimpleVersionParserTest extends TestCase
{
Expand Down Expand Up @@ -33,8 +33,9 @@ public function test(): void

public function testUnsupported(): void
{
$this->expectException(UnsupportedVersionException::class);

$this->parser->parse('v1.2.3_not_supported');
$this->assertInstanceOf(
UnknownVersion::class,
$this->parser->parse('v1.2.3_not_supported'),
);
}
}
76 changes: 76 additions & 0 deletions tests/Versioning/Unknown/UnknownVersionComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Siketyan\Loxcan\Versioning\Unknown;

use PHPUnit\Framework\TestCase;
use Siketyan\Loxcan\Versioning\SemVer\SemVerVersion;
use Siketyan\Loxcan\Versioning\Simple\SimpleVersion;
use Siketyan\Loxcan\Versioning\VersionDiff;
use Siketyan\Loxcan\Versioning\VersionInterface;

class UnknownVersionComparatorTest extends TestCase
{
private UnknownVersionComparator $comparator;

protected function setUp(): void
{
$this->comparator = new UnknownVersionComparator();
}

public function test(): void
{
$this->assertCompare(
VersionDiff::UNKNOWN,
new UnknownVersion('1.2.3.4'),
new SimpleVersion(1, 2, 3, 4),
);

$this->assertCompare(
VersionDiff::UNKNOWN,
new UnknownVersion('1.2.3.4'),
new UnknownVersion('2.3.4.5'),
);

$this->assertCompare(
VersionDiff::UNKNOWN,
new SimpleVersion(1, 2, 3, 4),
new SimpleVersion(2, 3, 4, 5),
);
}

public function testSupports(): void
{
$this->assertTrue(
$this->comparator->supports(UnknownVersion::class, UnknownVersion::class),
);

$this->assertTrue(
$this->comparator->supports(SimpleVersion::class, UnknownVersion::class),
);

$this->assertTrue(
$this->comparator->supports(SemVerVersion::class, SimpleVersion::class),
);

$this->assertFalse(
$this->comparator->supports(UnknownVersion::class, VersionDiff::class),
);
}

private function assertCompare(?int $type, VersionInterface $before, VersionInterface $after): void
{
$diff = $this->comparator->compare($before, $after);

if ($type === null) {
$this->assertNull($diff);

return;
}

$this->assertSame($type, $diff->getType());
$this->assertSame($before, $diff->getBefore());
$this->assertSame($after, $diff->getAfter());
}
}
27 changes: 27 additions & 0 deletions tests/Versioning/Unknown/UnknownVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Siketyan\Loxcan\Versioning\Unknown;

use PHPUnit\Framework\TestCase;

class UnknownVersionTest extends TestCase
{
private const VERSION = '1.2.3.4';

private UnknownVersion $version;

protected function setUp(): void
{
$this->version = new UnknownVersion(self::VERSION);
}

public function testToString(): void
{
$this->assertSame(
self::VERSION,
$this->version->__toString(),
);
}
}

0 comments on commit c28a938

Please sign in to comment.