Support version 6 UUIDs

See the following:

* https://github.com/uuid6/uuid6-ietf-draft
* http://gh.peabody.io/uuidv6/
This commit is contained in:
Ben Ramsey
2020-02-22 22:41:15 -06:00
parent 65b0c091c0
commit cc2bf6f8ef
12 changed files with 473 additions and 4 deletions
-1
View File
@@ -89,7 +89,6 @@ class FieldsTest extends TestCase
// representations, which are never in GUID byte order.
return [
['b08c6fff7dc5e1018b210800200c9a66'],
['b08c6fff7dc5e1618b210800200c9a66'],
['b08c6fff7dc5e1719b210800200c9a66'],
['b08c6fff7dc5e181ab210800200c9a66'],
['b08c6fff7dc5e191bb210800200c9a66'],
+160
View File
@@ -0,0 +1,160 @@
<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Nonstandard;
use DateTimeImmutable;
use Mockery;
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Nonstandard\UuidV6;
use Ramsey\Uuid\Rfc4122\FieldsInterface;
use Ramsey\Uuid\Rfc4122\UuidV1;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Uuid;
class UuidV6Test extends TestCase
{
/**
* @dataProvider provideTestVersions
*/
public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
{
$fields = Mockery::mock(FieldsInterface::class, [
'getVersion' => $version,
]);
$numberConverter = Mockery::mock(NumberConverterInterface::class);
$codec = Mockery::mock(CodecInterface::class);
$timeConverter = Mockery::mock(TimeConverterInterface::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Fields used to create a UuidV6 must represent a '
. 'version 6 (ordered-time) UUID'
);
new UuidV6($fields, $numberConverter, $codec, $timeConverter);
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function provideTestVersions(): array
{
return [
['version' => 0],
['version' => 1],
['version' => 2],
['version' => 3],
['version' => 4],
['version' => 5],
['version' => 7],
['version' => 8],
['version' => 9],
];
}
/**
* @dataProvider provideUuidV6WithOddMicroseconds
*/
public function testGetDateTimeProperlyHandlesLongMicroseconds(string $uuid, string $expected): void
{
/** @var UuidV6 $object */
$object = Uuid::fromString($uuid);
$date = $object->getDateTime();
$this->assertInstanceOf(DateTimeImmutable::class, $date);
$this->assertSame($expected, $date->format('U.u'));
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function provideUuidV6WithOddMicroseconds(): array
{
return [
[
'uuid' => '1b21dd21-4814-6000-9669-00007ffffffe',
'expected' => '1.677722',
],
[
'uuid' => '1b21dd21-3714-6000-9669-00007ffffffe',
'expected' => '0.104858',
],
[
'uuid' => '1b21dd21-3713-6000-9669-00007ffffffe',
'expected' => '0.105267',
],
[
'uuid' => '1b21dd21-2e8a-6980-8d4f-acde48001122',
'expected' => '-1.000000',
],
];
}
/**
* @dataProvider provideUuidV1UuidV6Equivalents
*/
public function testToUuidV1(string $uuidv6, string $uuidv1): void
{
/** @var UuidV6 $uuid6 */
$uuid6 = Uuid::fromString($uuidv6);
$uuid1 = $uuid6->toUuidV1();
$this->assertSame($uuidv6, $uuid6->toString());
$this->assertSame($uuidv1, $uuid1->toString());
$this->assertSame(
$uuid6->getDateTime()->format('U.u'),
$uuid1->getDateTime()->format('U.u')
);
}
/**
* @dataProvider provideUuidV1UuidV6Equivalents
*/
public function testFromUuidV1(string $uuidv6, string $uuidv1): void
{
/** @var UuidV1 $uuid1 */
$uuid1 = Uuid::fromString($uuidv1);
$uuid6 = UuidV6::fromUuidV1($uuid1);
$this->assertSame($uuidv1, $uuid1->toString());
$this->assertSame($uuidv6, $uuid6->toString());
$this->assertSame(
$uuid1->getDateTime()->format('U.u'),
$uuid6->getDateTime()->format('U.u')
);
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function provideUuidV1UuidV6Equivalents(): array
{
return [
[
'uuidv6' => '1b21dd21-4814-6000-9669-00007ffffffe',
'uuidv1' => '14814000-1dd2-11b2-9669-00007ffffffe',
],
[
'uuidv6' => '1b21dd21-3714-6000-9669-00007ffffffe',
'uuidv1' => '13714000-1dd2-11b2-9669-00007ffffffe',
],
[
'uuidv6' => '1b21dd21-3713-6000-9669-00007ffffffe',
'uuidv1' => '13713000-1dd2-11b2-9669-00007ffffffe',
],
[
'uuidv6' => '1b21dd21-2e8a-6980-8d4f-acde48001122',
'uuidv1' => '12e8a980-1dd2-11b2-8d4f-acde48001122',
],
];
}
}
+12 -1
View File
@@ -84,7 +84,6 @@ class FieldsTest extends TestCase
{
return [
['ff6f8cb0-c57d-01e1-8b21-0800200c9a66'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66'],
['ff6f8cb0-c57d-71e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-81e1-ab21-0800200c9a66'],
['ff6f8cb0-c57d-91e1-bb21-0800200c9a66'],
@@ -165,6 +164,18 @@ class FieldsTest extends TestCase
['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVersion', 5],
['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'isNil', false],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getClockSeq', '0b21'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getClockSeqHiAndReserved', '8b'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getClockSeqLow', '21'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getNode', '0800200c9a66'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimeHiAndVersion', '61e1'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimeMid', 'c57d'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimestamp', 'ff6f8cb0c57d1e1'],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVariant', 2],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVersion', 6],
['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'isNil', false],
['00000000-0000-0000-0000-000000000000', 'getClockSeq', '0000'],
['00000000-0000-0000-0000-000000000000', 'getClockSeqHiAndReserved', '00'],
['00000000-0000-0000-0000-000000000000', 'getClockSeqLow', '00'],
+6
View File
@@ -9,6 +9,7 @@ use Ramsey\Uuid\Converter\Number\GenericNumberConverter;
use Ramsey\Uuid\Converter\Time\GenericTimeConverter;
use Ramsey\Uuid\Exception\UnableToBuildUuidException;
use Ramsey\Uuid\Math\BrickMathCalculator;
use Ramsey\Uuid\Nonstandard\UuidV6;
use Ramsey\Uuid\Rfc4122\Fields;
use Ramsey\Uuid\Rfc4122\UuidBuilder;
use Ramsey\Uuid\Rfc4122\UuidV1;
@@ -78,6 +79,11 @@ class UuidBuilderTest extends TestCase
'expectedClass' => UuidV5::class,
'expectedVersion' => 5,
],
[
'uuid' => 'ff6f8cb0-c57d-61e1-9b21-0800200c9a66',
'expectedClass' => UuidV6::class,
'expectedVersion' => 6,
],
];
}
+85
View File
@@ -25,6 +25,7 @@ use Ramsey\Uuid\Generator\RandomGeneratorFactory;
use Ramsey\Uuid\Generator\RandomGeneratorInterface;
use Ramsey\Uuid\Guid\Guid;
use Ramsey\Uuid\Nonstandard\Uuid as NonstandardUuid;
use Ramsey\Uuid\Nonstandard\UuidV6;
use Ramsey\Uuid\Provider\Time\FixedTimeProvider;
use Ramsey\Uuid\Rfc4122\FieldsInterface;
use Ramsey\Uuid\Rfc4122\NilUuid;
@@ -541,6 +542,84 @@ class UuidTest extends TestCase
$this->assertEquals(1, $uuid->getVersion());
}
public function testUuid6(): void
{
$uuid = Uuid::uuid6();
$this->assertInstanceOf(UuidV6::class, $uuid);
$this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(6, $uuid->getVersion());
}
public function testUuid6WithNodeAndClockSequence(): void
{
$uuid = Uuid::uuid6('0800200c9a66', 0x1669);
$this->assertInstanceOf(UuidV6::class, $uuid);
$this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
$this->assertSame(2, $uuid->getVariant());
$this->assertSame(6, $uuid->getVersion());
$this->assertSame('1669', $uuid->getClockSequenceHex());
$this->assertSame('0800200c9a66', $uuid->getNodeHex());
$this->assertSame('9669-0800200c9a66', substr($uuid->toString(), 19));
}
public function testUuid6WithHexadecimalNode(): void
{
$uuid = Uuid::uuid6('7160355e');
$this->assertInstanceOf(UuidV6::class, $uuid);
$this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
$this->assertSame(2, $uuid->getVariant());
$this->assertSame(6, $uuid->getVersion());
$this->assertSame('00007160355e', $uuid->getNodeHex());
}
public function testUuid6WithMixedCaseHexadecimalNode(): void
{
$uuid = Uuid::uuid6('71B0aD5e');
$this->assertInstanceOf(Uuid::class, $uuid);
$this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
$this->assertSame(2, $uuid->getVariant());
$this->assertSame(6, $uuid->getVersion());
$this->assertSame('000071b0ad5e', $uuid->getNodeHex());
}
public function testUuid6WithOutOfBoundsNode(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid node value');
Uuid::uuid6('9223372036854775808');
}
public function testUuid6WithNonHexadecimalNode(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid node value');
Uuid::uuid6('db77e160355g');
}
public function testUuid6WithNon48bitNumber(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid node value');
Uuid::uuid6('db77e160355ef');
}
public function testUuid6WithRandomNode(): void
{
Uuid::setFactory(new UuidFactory(new FeatureSet(false, false, false, true)));
$uuid = Uuid::uuid6();
$this->assertInstanceOf(UuidV6::class, $uuid);
$this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(6, $uuid->getVersion());
}
/**
* Tests known version-3 UUIDs
*
@@ -1470,6 +1549,12 @@ class UuidTest extends TestCase
$this->assertEquals($uuid->getVersion(), Uuid::UUID_TYPE_HASH_SHA1);
}
public function testUuidVersionConstantForVersion6(): void
{
$uuid = Uuid::fromString('886313e1-3b8a-6372-9b90-0c9aee199e5d');
$this->assertEquals($uuid->getVersion(), Uuid::UUID_TYPE_PEABODY);
}
public function testGetDateTimeThrowsExceptionWhenDateTimeCannotParseDate(): void
{
$numberConverter = new BigNumberConverter();