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
8 changes: 4 additions & 4 deletions library/Rules/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Validates whether the input is a valid UUID.
*
* It also supports validation of specific versions 1, 3, 4 and 5.
* It also supports validation of specific versions 1 to 8.
*
* @author Dick van der Heiden <d.vanderheiden@inthere.nl>
* @author Henrique Moody <henriquemoody@gmail.com>
Expand Down Expand Up @@ -46,7 +46,7 @@ final class Uuid extends AbstractRule
public function __construct(?int $version = null)
{
if ($version !== null && !$this->isSupportedVersion($version)) {
throw new ComponentException(sprintf('Only versions 1, 3, 4, and 5 are supported: %d given', $version));
throw new ComponentException(sprintf('Only versions 1 to 8 are supported: %d given', $version));
}

$this->version = $version;
Expand All @@ -66,7 +66,7 @@ public function validate($input): bool

private function isSupportedVersion(int $version): bool
{
return $version >= 1 && $version <= 5 && $version !== 2;
return $version >= 1 && $version <= 8;
}

private function getPattern(): string
Expand All @@ -75,6 +75,6 @@ private function getPattern(): string
return sprintf(self::PATTERN_FORMAT, $this->version);
}

return sprintf(self::PATTERN_FORMAT, '[13-5]');
return sprintf(self::PATTERN_FORMAT, '[1-8]');
}
}
38 changes: 24 additions & 14 deletions tests/unit/Rules/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,24 @@
final class UuidTest extends RuleTestCase
{
private const UUID_VERSION_1 = 'e4eaaaf2-d142-11e1-b3e4-080027620cdd';
private const UUID_VERSION_2 = '000003e8-3702-21f0-9f00-325096b39f47';
private const UUID_VERSION_3 = '11a38b9a-b3da-360f-9353-a5a725514269';
private const UUID_VERSION_4 = '25769c6c-d34d-4bfe-ba98-e0ee856f3e7a';
private const UUID_VERSION_5 = 'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62';
private const UUID_VERSION_6 = '1f1239e6-c96d-6964-ae1d-9da658bd666c';
private const UUID_VERSION_7 = '019d0676-4cf1-77c4-834e-2b847eb7d6f3';
private const UUID_VERSION_8 = '019d0676-dc57-8649-ae91-5913434dbdc2';

/**
* @test
*/
public function itShouldThrowExceptionWhenVersionIsTwo(): void
{
self::expectException(ComponentException::class);
self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: 2 given');

new Uuid(2);
}

/**
* @test
*/
public function itShouldThrowExceptionWhenVersionIsGreaterThanFive(): void
public function itShouldThrowExceptionWhenVersionIsGreaterThanEight(): void
{
$version = random_int(6, PHP_INT_MAX);
$version = random_int(9, PHP_INT_MAX);

self::expectException(ComponentException::class);
self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given');
self::expectExceptionMessage('Only versions 1 to 8 are supported: ' . $version . ' given');

new Uuid($version);
}
Expand All @@ -66,7 +60,7 @@ public function itShouldThrowExceptionWhenVersionIsLessThanOne(): void
$version = random_int(PHP_INT_MIN, 0);

self::expectException(ComponentException::class);
self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given');
self::expectExceptionMessage('Only versions 1 to 8 are supported: ' . $version . ' given');

new Uuid($version);
}
Expand All @@ -80,13 +74,21 @@ public static function providerForValidInput(): array

return [
'any version with version 1' => [$sut, self::UUID_VERSION_1],
'any version with version 2' => [$sut, self::UUID_VERSION_2],
'any version with version 3' => [$sut, self::UUID_VERSION_3],
'any version with version 4' => [$sut, self::UUID_VERSION_4],
'any version with version 5' => [$sut, self::UUID_VERSION_5],
'any version with version 6' => [$sut, self::UUID_VERSION_6],
'any version with version 7' => [$sut, self::UUID_VERSION_7],
'any version with version 8' => [$sut, self::UUID_VERSION_8],
'version 1 with version 1' => [new Uuid(1), self::UUID_VERSION_1],
'version 2 with version 2' => [new Uuid(2), self::UUID_VERSION_2],
'version 3 with version 3' => [new Uuid(3), self::UUID_VERSION_3],
'version 4 with version 4' => [new Uuid(4), self::UUID_VERSION_4],
'version 5 with version 5' => [new Uuid(5), self::UUID_VERSION_5],
'version 6 with version 6' => [new Uuid(6), self::UUID_VERSION_6],
'version 7 with version 7' => [new Uuid(7), self::UUID_VERSION_7],
'version 8 with version 8' => [new Uuid(8), self::UUID_VERSION_8],
];
}

Expand All @@ -97,9 +99,13 @@ public static function providerForInvalidInput(): array
{
$sut = new Uuid();
$sutVersion1 = new Uuid(1);
$sutVersion2 = new Uuid(2);
$sutVersion3 = new Uuid(3);
$sutVersion4 = new Uuid(4);
$sutVersion5 = new Uuid(5);
$sutVersion6 = new Uuid(6);
$sutVersion7 = new Uuid(7);
$sutVersion8 = new Uuid(8);

return [
'empty' => [$sut, ''],
Expand All @@ -110,6 +116,7 @@ public static function providerForInvalidInput(): array
'version 1 with version 3' => [$sutVersion1, self::UUID_VERSION_3],
'version 1 with version 4' => [$sutVersion1, self::UUID_VERSION_4],
'version 1 with version 5' => [$sutVersion1, self::UUID_VERSION_5],
'version 2 with version 5' => [$sutVersion2, self::UUID_VERSION_5],
'version 3 with version 1' => [$sutVersion3, self::UUID_VERSION_1],
'version 3 with version 4' => [$sutVersion3, self::UUID_VERSION_4],
'version 3 with version 5' => [$sutVersion3, self::UUID_VERSION_5],
Expand All @@ -119,6 +126,9 @@ public static function providerForInvalidInput(): array
'version 5 with version 1' => [$sutVersion5, self::UUID_VERSION_1],
'version 5 with version 3' => [$sutVersion5, self::UUID_VERSION_3],
'version 5 with version 4' => [$sutVersion5, self::UUID_VERSION_4],
'version 6 with version 4' => [$sutVersion6, self::UUID_VERSION_4],
'version 7 with version 3' => [$sutVersion7, self::UUID_VERSION_3],
'version 8 with version 5' => [$sutVersion8, self::UUID_VERSION_5],
'array' => [$sut, []],
'boolean true' => [$sut, true],
'boolean false' => [$sut, false],
Expand Down
Loading