UuidV6Test.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Rfc4122;
  4. use DateTimeImmutable;
  5. use Mockery;
  6. use Ramsey\Uuid\Codec\CodecInterface;
  7. use Ramsey\Uuid\Converter\NumberConverterInterface;
  8. use Ramsey\Uuid\Converter\TimeConverterInterface;
  9. use Ramsey\Uuid\Exception\DateTimeException;
  10. use Ramsey\Uuid\Exception\InvalidArgumentException;
  11. use Ramsey\Uuid\Lazy\LazyUuidFromString;
  12. use Ramsey\Uuid\Rfc4122\FieldsInterface;
  13. use Ramsey\Uuid\Rfc4122\UuidV6;
  14. use Ramsey\Uuid\Test\TestCase;
  15. use Ramsey\Uuid\Type\Hexadecimal;
  16. use Ramsey\Uuid\Type\Time;
  17. use Ramsey\Uuid\Uuid;
  18. class UuidV6Test extends TestCase
  19. {
  20. /**
  21. * @dataProvider provideTestVersions
  22. */
  23. public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
  24. {
  25. $fields = Mockery::mock(FieldsInterface::class, [
  26. 'getVersion' => $version,
  27. ]);
  28. $numberConverter = Mockery::mock(NumberConverterInterface::class);
  29. $codec = Mockery::mock(CodecInterface::class);
  30. $timeConverter = Mockery::mock(TimeConverterInterface::class);
  31. $this->expectException(InvalidArgumentException::class);
  32. $this->expectExceptionMessage(
  33. 'Fields used to create a UuidV6 must represent a '
  34. . 'version 6 (reordered time) UUID'
  35. );
  36. new UuidV6($fields, $numberConverter, $codec, $timeConverter);
  37. }
  38. /**
  39. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  40. */
  41. public function provideTestVersions(): array
  42. {
  43. return [
  44. ['version' => 0],
  45. ['version' => 1],
  46. ['version' => 2],
  47. ['version' => 3],
  48. ['version' => 4],
  49. ['version' => 5],
  50. ['version' => 7],
  51. ['version' => 8],
  52. ['version' => 9],
  53. ];
  54. }
  55. /**
  56. * @param non-empty-string $uuid
  57. *
  58. * @dataProvider provideUuidV6WithOddMicroseconds
  59. */
  60. public function testGetDateTimeProperlyHandlesLongMicroseconds(string $uuid, string $expected): void
  61. {
  62. /** @var UuidV6 $object */
  63. $object = Uuid::fromString($uuid);
  64. $date = $object->getDateTime();
  65. $this->assertInstanceOf(DateTimeImmutable::class, $date);
  66. $this->assertSame($expected, $date->format('U.u'));
  67. }
  68. /**
  69. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  70. */
  71. public function provideUuidV6WithOddMicroseconds(): array
  72. {
  73. return [
  74. [
  75. 'uuid' => '1b21dd21-4814-6000-9669-00007ffffffe',
  76. 'expected' => '1.677722',
  77. ],
  78. [
  79. 'uuid' => '1b21dd21-3714-6000-9669-00007ffffffe',
  80. 'expected' => '0.104858',
  81. ],
  82. [
  83. 'uuid' => '1b21dd21-3713-6000-9669-00007ffffffe',
  84. 'expected' => '0.105267',
  85. ],
  86. [
  87. 'uuid' => '1b21dd21-2e8a-6980-8d4f-acde48001122',
  88. 'expected' => '-1.000000',
  89. ],
  90. ];
  91. }
  92. /**
  93. * @param non-empty-string $uuidv6
  94. * @param non-empty-string $uuidv1
  95. *
  96. * @dataProvider provideUuidV1UuidV6Equivalents
  97. */
  98. public function testToUuidV1(string $uuidv6, string $uuidv1): void
  99. {
  100. /** @var UuidV6 $uuid6 */
  101. $uuid6 = Uuid::fromString($uuidv6);
  102. $uuid1 = $uuid6->toUuidV1();
  103. $this->assertSame($uuidv6, $uuid6->toString());
  104. $this->assertSame($uuidv1, $uuid1->toString());
  105. $this->assertSame(
  106. $uuid6->getDateTime()->format('U.u'),
  107. $uuid1->getDateTime()->format('U.u')
  108. );
  109. }
  110. /**
  111. * @param non-empty-string $uuidv6
  112. * @param non-empty-string $uuidv1
  113. *
  114. * @dataProvider provideUuidV1UuidV6Equivalents
  115. */
  116. public function testFromUuidV1(string $uuidv6, string $uuidv1): void
  117. {
  118. /** @var LazyUuidFromString $uuid */
  119. $uuid = Uuid::fromString($uuidv1);
  120. $uuid1 = $uuid->toUuidV1();
  121. $uuid6 = UuidV6::fromUuidV1($uuid1);
  122. $this->assertSame($uuidv1, $uuid1->toString());
  123. $this->assertSame($uuidv6, $uuid6->toString());
  124. $this->assertSame(
  125. $uuid1->getDateTime()->format('U.u'),
  126. $uuid6->getDateTime()->format('U.u')
  127. );
  128. }
  129. /**
  130. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  131. */
  132. public function provideUuidV1UuidV6Equivalents(): array
  133. {
  134. return [
  135. [
  136. 'uuidv6' => '1b21dd21-4814-6000-9669-00007ffffffe',
  137. 'uuidv1' => '14814000-1dd2-11b2-9669-00007ffffffe',
  138. ],
  139. [
  140. 'uuidv6' => '1b21dd21-3714-6000-9669-00007ffffffe',
  141. 'uuidv1' => '13714000-1dd2-11b2-9669-00007ffffffe',
  142. ],
  143. [
  144. 'uuidv6' => '1b21dd21-3713-6000-9669-00007ffffffe',
  145. 'uuidv1' => '13713000-1dd2-11b2-9669-00007ffffffe',
  146. ],
  147. [
  148. 'uuidv6' => '1b21dd21-2e8a-6980-8d4f-acde48001122',
  149. 'uuidv1' => '12e8a980-1dd2-11b2-8d4f-acde48001122',
  150. ],
  151. ];
  152. }
  153. public function testGetDateTimeThrowsException(): void
  154. {
  155. $fields = Mockery::mock(FieldsInterface::class, [
  156. 'getVersion' => 6,
  157. 'getTimestamp' => new Hexadecimal('0'),
  158. ]);
  159. $numberConverter = Mockery::mock(NumberConverterInterface::class);
  160. $codec = Mockery::mock(CodecInterface::class);
  161. $timeConverter = Mockery::mock(TimeConverterInterface::class, [
  162. 'convertTime' => new Time('0', '1234567'),
  163. ]);
  164. $uuid = new UuidV6($fields, $numberConverter, $codec, $timeConverter);
  165. $this->expectException(DateTimeException::class);
  166. $uuid->getDateTime();
  167. }
  168. /**
  169. * @link https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04#appendix-B.1
  170. */
  171. public function testUsingDraftPeabodyUuidV6TestVector(): void
  172. {
  173. $testVector = '1EC9414C-232A-6B00-B3C8-9E6BDECED846';
  174. /** @var UuidV6 $uuidv6 */
  175. $uuidv6 = Uuid::fromString($testVector);
  176. $uuidv1 = $uuidv6->toUuidV1();
  177. /** @var FieldsInterface $fields */
  178. $fields = $uuidv6->getFields();
  179. $this->assertSame('1ec9414c', $fields->getTimeLow()->toString());
  180. $this->assertSame('232a', $fields->getTimeMid()->toString());
  181. $this->assertSame('6b00', $fields->getTimeHiAndVersion()->toString());
  182. $this->assertSame('b3', $fields->getClockSeqHiAndReserved()->toString());
  183. $this->assertSame('c8', $fields->getClockSeqLow()->toString());
  184. $this->assertSame('9e6bdeced846', $fields->getNode()->toString());
  185. $this->assertSame(1645557742, $uuidv6->getDateTime()->getTimestamp());
  186. $this->assertSame(
  187. 'c232ab00-9414-11ec-b3c8-9e6bdeced846',
  188. $uuidv1->toString(),
  189. );
  190. }
  191. }