FieldsTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Nonstandard;
  4. use Ramsey\Uuid\Exception\InvalidArgumentException;
  5. use Ramsey\Uuid\Nonstandard\Fields;
  6. use Ramsey\Uuid\Test\TestCase;
  7. use Ramsey\Uuid\Type\Hexadecimal;
  8. use Ramsey\Uuid\Uuid;
  9. use function hex2bin;
  10. use function serialize;
  11. use function str_replace;
  12. use function unserialize;
  13. class FieldsTest extends TestCase
  14. {
  15. public function testConstructorThrowsExceptionIfNotSixteenByteString(): void
  16. {
  17. $this->expectException(InvalidArgumentException::class);
  18. $this->expectExceptionMessage(
  19. 'The byte string must be 16 bytes long; received 6 bytes'
  20. );
  21. new Fields('foobar');
  22. }
  23. /**
  24. * @param string|int $expectedValue
  25. *
  26. * @dataProvider fieldGetterMethodProvider
  27. */
  28. public function testFieldGetterMethods(string $uuid, string $methodName, $expectedValue): void
  29. {
  30. $bytes = (string) hex2bin(str_replace('-', '', $uuid));
  31. $fields = new Fields($bytes);
  32. $result = $fields->$methodName();
  33. if ($result instanceof Hexadecimal) {
  34. $this->assertSame($expectedValue, $result->toString());
  35. } else {
  36. $this->assertSame($expectedValue, $result);
  37. }
  38. }
  39. /**
  40. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  41. */
  42. public function fieldGetterMethodProvider(): array
  43. {
  44. return [
  45. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getClockSeq', '0b21'],
  46. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getClockSeqHiAndReserved', '0b'],
  47. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getClockSeqLow', '21'],
  48. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getNode', '0800200c9a66'],
  49. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeHiAndVersion', '91e1'],
  50. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'],
  51. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeMid', 'c57d'],
  52. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'],
  53. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVariant', Uuid::RESERVED_NCS],
  54. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVersion', null],
  55. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'isNil', false],
  56. ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'isMax', false],
  57. ];
  58. }
  59. public function testSerializingFields(): void
  60. {
  61. $bytes = (string) hex2bin(str_replace('-', '', 'ff6f8cb0-c57d-91e1-0b21-0800200c9a66'));
  62. $fields = new Fields($bytes);
  63. $serializedFields = serialize($fields);
  64. /** @var Fields $unserializedFields */
  65. $unserializedFields = unserialize($serializedFields);
  66. $this->assertSame($fields->getBytes(), $unserializedFields->getBytes());
  67. }
  68. }