VariantTraitTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Rfc4122;
  4. use Mockery;
  5. use Ramsey\Uuid\Exception\InvalidBytesException;
  6. use Ramsey\Uuid\Rfc4122\Fields;
  7. use Ramsey\Uuid\Rfc4122\VariantTrait;
  8. use Ramsey\Uuid\Test\TestCase;
  9. use function hex2bin;
  10. use function str_replace;
  11. class VariantTraitTest extends TestCase
  12. {
  13. /**
  14. * @dataProvider invalidBytesProvider
  15. */
  16. public function testGetVariantThrowsExceptionForWrongNumberOfBytes(string $bytes): void
  17. {
  18. /** @var Fields $trait */
  19. $trait = Mockery::mock(VariantTrait::class, [
  20. 'getBytes' => $bytes,
  21. 'isMax' => false,
  22. 'isNil' => false,
  23. ]);
  24. $this->expectException(InvalidBytesException::class);
  25. $this->expectExceptionMessage('Invalid number of bytes');
  26. $trait->getVariant();
  27. }
  28. /**
  29. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  30. */
  31. public function invalidBytesProvider(): array
  32. {
  33. return [
  34. ['not16Bytes_abcd'],
  35. ['not16Bytes_abcdef'],
  36. ];
  37. }
  38. /**
  39. * @dataProvider uuidVariantProvider
  40. */
  41. public function testGetVariant(string $uuid, int $expectedVariant): void
  42. {
  43. $bytes = (string) hex2bin(str_replace('-', '', $uuid));
  44. /** @var Fields $trait */
  45. $trait = Mockery::mock(VariantTrait::class, [
  46. 'getBytes' => $bytes,
  47. 'isMax' => false,
  48. 'isNil' => false,
  49. ]);
  50. $this->assertSame($expectedVariant, $trait->getVariant());
  51. }
  52. /**
  53. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  54. */
  55. public function uuidVariantProvider(): array
  56. {
  57. return [
  58. ['ff6f8cb0-c57d-11e1-0b21-0800200c9a66', 0],
  59. ['ff6f8cb0-c57d-11e1-1b21-0800200c9a66', 0],
  60. ['ff6f8cb0-c57d-11e1-2b21-0800200c9a66', 0],
  61. ['ff6f8cb0-c57d-11e1-3b21-0800200c9a66', 0],
  62. ['ff6f8cb0-c57d-11e1-4b21-0800200c9a66', 0],
  63. ['ff6f8cb0-c57d-11e1-5b21-0800200c9a66', 0],
  64. ['ff6f8cb0-c57d-11e1-6b21-0800200c9a66', 0],
  65. ['ff6f8cb0-c57d-11e1-7b21-0800200c9a66', 0],
  66. ['ff6f8cb0-c57d-11e1-8b21-0800200c9a66', 2],
  67. ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 2],
  68. ['ff6f8cb0-c57d-11e1-ab21-0800200c9a66', 2],
  69. ['ff6f8cb0-c57d-11e1-bb21-0800200c9a66', 2],
  70. ['ff6f8cb0-c57d-11e1-cb21-0800200c9a66', 6],
  71. ['ff6f8cb0-c57d-11e1-db21-0800200c9a66', 6],
  72. ['ff6f8cb0-c57d-11e1-eb21-0800200c9a66', 7],
  73. ['ff6f8cb0-c57d-11e1-fb21-0800200c9a66', 7],
  74. // The following are the same UUIDs in GUID byte order. Dashes have
  75. // been removed in the tests to distinguish these from string
  76. // representations, which are never in GUID byte order.
  77. ['b08c6fff7dc5e1110b210800200c9a66', 0],
  78. ['b08c6fff7dc5e1111b210800200c9a66', 0],
  79. ['b08c6fff7dc5e1112b210800200c9a66', 0],
  80. ['b08c6fff7dc5e1113b210800200c9a66', 0],
  81. ['b08c6fff7dc5e1114b210800200c9a66', 0],
  82. ['b08c6fff7dc5e1115b210800200c9a66', 0],
  83. ['b08c6fff7dc5e1116b210800200c9a66', 0],
  84. ['b08c6fff7dc5e1117b210800200c9a66', 0],
  85. ['b08c6fff7dc5e1118b210800200c9a66', 2],
  86. ['b08c6fff7dc5e1119b210800200c9a66', 2],
  87. ['b08c6fff7dc5e111ab210800200c9a66', 2],
  88. ['b08c6fff7dc5e111bb210800200c9a66', 2],
  89. ['b08c6fff7dc5e111cb210800200c9a66', 6],
  90. ['b08c6fff7dc5e111db210800200c9a66', 6],
  91. ['b08c6fff7dc5e111eb210800200c9a66', 7],
  92. ['b08c6fff7dc5e111fb210800200c9a66', 7],
  93. ];
  94. }
  95. }