UuidBuilderTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Rfc4122;
  4. use Mockery;
  5. use Ramsey\Uuid\Codec\StringCodec;
  6. use Ramsey\Uuid\Converter\Number\GenericNumberConverter;
  7. use Ramsey\Uuid\Converter\Time\GenericTimeConverter;
  8. use Ramsey\Uuid\Exception\UnableToBuildUuidException;
  9. use Ramsey\Uuid\Math\BrickMathCalculator;
  10. use Ramsey\Uuid\Nonstandard\UuidV6 as NonstandardUuidV6;
  11. use Ramsey\Uuid\Rfc4122\Fields;
  12. use Ramsey\Uuid\Rfc4122\FieldsInterface;
  13. use Ramsey\Uuid\Rfc4122\MaxUuid;
  14. use Ramsey\Uuid\Rfc4122\NilUuid;
  15. use Ramsey\Uuid\Rfc4122\UuidBuilder;
  16. use Ramsey\Uuid\Rfc4122\UuidV1;
  17. use Ramsey\Uuid\Rfc4122\UuidV2;
  18. use Ramsey\Uuid\Rfc4122\UuidV3;
  19. use Ramsey\Uuid\Rfc4122\UuidV4;
  20. use Ramsey\Uuid\Rfc4122\UuidV5;
  21. use Ramsey\Uuid\Rfc4122\UuidV6;
  22. use Ramsey\Uuid\Rfc4122\UuidV7;
  23. use Ramsey\Uuid\Rfc4122\UuidV8;
  24. use Ramsey\Uuid\Test\TestCase;
  25. use function hex2bin;
  26. use function str_replace;
  27. class UuidBuilderTest extends TestCase
  28. {
  29. /**
  30. * @param class-string $expectedClass
  31. *
  32. * @dataProvider provideBuildTestValues
  33. */
  34. public function testBuild(string $uuid, string $expectedClass, ?int $expectedVersion): void
  35. {
  36. $bytes = (string) hex2bin(str_replace('-', '', $uuid));
  37. $calculator = new BrickMathCalculator();
  38. $numberConverter = new GenericNumberConverter($calculator);
  39. $timeConverter = new GenericTimeConverter($calculator);
  40. $builder = new UuidBuilder($numberConverter, $timeConverter);
  41. $codec = new StringCodec($builder);
  42. $result = $builder->build($codec, $bytes);
  43. /** @var Fields $fields */
  44. $fields = $result->getFields();
  45. $this->assertInstanceOf($expectedClass, $result);
  46. $this->assertSame($expectedVersion, $fields->getVersion());
  47. }
  48. /**
  49. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  50. */
  51. public function provideBuildTestValues(): array
  52. {
  53. return [
  54. [
  55. 'uuid' => '00000000-0000-0000-0000-000000000000',
  56. 'expectedClass' => NilUuid::class,
  57. 'expectedVersion' => null,
  58. ],
  59. [
  60. 'uuid' => 'ffffffff-ffff-ffff-ffff-ffffffffffff',
  61. 'expectedClass' => MaxUuid::class,
  62. 'expectedVersion' => null,
  63. ],
  64. [
  65. 'uuid' => 'ff6f8cb0-c57d-11e1-9b21-0800200c9a66',
  66. 'expectedClass' => UuidV1::class,
  67. 'expectedVersion' => 1,
  68. ],
  69. [
  70. 'uuid' => 'ff6f8cb0-c57d-21e1-9b21-0800200c9a66',
  71. 'expectedClass' => UuidV2::class,
  72. 'expectedVersion' => 2,
  73. ],
  74. [
  75. 'uuid' => 'ff6f8cb0-c57d-31e1-9b21-0800200c9a66',
  76. 'expectedClass' => UuidV3::class,
  77. 'expectedVersion' => 3,
  78. ],
  79. [
  80. 'uuid' => 'ff6f8cb0-c57d-41e1-9b21-0800200c9a66',
  81. 'expectedClass' => UuidV4::class,
  82. 'expectedVersion' => 4,
  83. ],
  84. [
  85. 'uuid' => 'ff6f8cb0-c57d-51e1-9b21-0800200c9a66',
  86. 'expectedClass' => UuidV5::class,
  87. 'expectedVersion' => 5,
  88. ],
  89. [
  90. 'uuid' => 'ff6f8cb0-c57d-61e1-9b21-0800200c9a66',
  91. 'expectedClass' => UuidV6::class,
  92. 'expectedVersion' => 6,
  93. ],
  94. // The same UUIDv6 will also be of the expected class type
  95. // \Ramsey\Uuid\Nonstandard\UuidV6.
  96. [
  97. 'uuid' => 'ff6f8cb0-c57d-61e1-9b21-0800200c9a66',
  98. 'expectedClass' => NonstandardUuidV6::class,
  99. 'expectedVersion' => 6,
  100. ],
  101. [
  102. 'uuid' => 'ff6f8cb0-c57d-71e1-9b21-0800200c9a66',
  103. 'expectedClass' => UuidV7::class,
  104. 'expectedVersion' => 7,
  105. ],
  106. [
  107. 'uuid' => 'ff6f8cb0-c57d-81e1-9b21-0800200c9a66',
  108. 'expectedClass' => UuidV8::class,
  109. 'expectedVersion' => 8,
  110. ],
  111. ];
  112. }
  113. public function testBuildThrowsUnableToBuildException(): void
  114. {
  115. $bytes = (string) hex2bin(str_replace('-', '', 'ff6f8cb0-c57d-51e1-9b21-0800200c9a'));
  116. $calculator = new BrickMathCalculator();
  117. $numberConverter = new GenericNumberConverter($calculator);
  118. $timeConverter = new GenericTimeConverter($calculator);
  119. $builder = new UuidBuilder($numberConverter, $timeConverter);
  120. $codec = new StringCodec($builder);
  121. $this->expectException(UnableToBuildUuidException::class);
  122. $this->expectExceptionMessage(
  123. 'The byte string must be 16 bytes long; received 15 bytes'
  124. );
  125. $builder->build($codec, $bytes);
  126. }
  127. public function testBuildThrowsUnableToBuildExceptionForIncorrectVersionFields(): void
  128. {
  129. $fields = Mockery::mock(FieldsInterface::class, [
  130. 'isNil' => false,
  131. 'isMax' => false,
  132. 'getVersion' => 255,
  133. ]);
  134. $builder = Mockery::mock(UuidBuilder::class);
  135. $builder->shouldAllowMockingProtectedMethods();
  136. $builder->shouldReceive('buildFields')->andReturn($fields);
  137. $builder->shouldReceive('build')->passthru();
  138. $codec = Mockery::mock(StringCodec::class);
  139. $this->expectException(UnableToBuildUuidException::class);
  140. $this->expectExceptionMessage(
  141. 'The UUID version in the given fields is not supported by this UUID builder'
  142. );
  143. $builder->build($codec, 'foobar');
  144. }
  145. }