PhpTimeConverterTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Converter\Time;
  4. use Brick\Math\BigInteger;
  5. use Mockery;
  6. use Ramsey\Uuid\Converter\Time\GenericTimeConverter;
  7. use Ramsey\Uuid\Converter\Time\PhpTimeConverter;
  8. use Ramsey\Uuid\Exception\InvalidArgumentException;
  9. use Ramsey\Uuid\Math\BrickMathCalculator;
  10. use Ramsey\Uuid\Test\TestCase;
  11. use Ramsey\Uuid\Type\Hexadecimal;
  12. use function sprintf;
  13. class PhpTimeConverterTest extends TestCase
  14. {
  15. public function testCalculateTimeReturnsArrayOfTimeSegments(): void
  16. {
  17. $seconds = BigInteger::of(5);
  18. $microseconds = BigInteger::of(3);
  19. $calculatedTime = BigInteger::zero()
  20. ->plus($seconds->multipliedBy(10000000))
  21. ->plus($microseconds->multipliedBy(10))
  22. ->plus(BigInteger::fromBase('01b21dd213814000', 16));
  23. $maskLow = BigInteger::fromBase('ffffffff', 16);
  24. $maskMid = BigInteger::fromBase('ffff', 16);
  25. $maskHi = BigInteger::fromBase('0fff', 16);
  26. $expected = sprintf('%04s', $calculatedTime->shiftedRight(48)->and($maskHi)->toBase(16));
  27. $expected .= sprintf('%04s', $calculatedTime->shiftedRight(32)->and($maskMid)->toBase(16));
  28. $expected .= sprintf('%08s', $calculatedTime->and($maskLow)->toBase(16));
  29. $converter = new PhpTimeConverter();
  30. $returned = $converter->calculateTime((string) $seconds, (string) $microseconds);
  31. $this->assertSame($expected, $returned->toString());
  32. }
  33. public function testCalculateTimeThrowsExceptionWhenSecondsIsNotOnlyDigits(): void
  34. {
  35. /** @var Mockery\MockInterface & PhpTimeConverter $converter */
  36. $converter = Mockery::mock(PhpTimeConverter::class)->makePartial();
  37. $this->expectException(InvalidArgumentException::class);
  38. $this->expectExceptionMessage(
  39. 'Value must be a signed integer or a string containing only digits '
  40. . '0-9 and, optionally, a sign (+ or -)'
  41. );
  42. $converter->calculateTime('12.34', '5678');
  43. }
  44. public function testCalculateTimeThrowsExceptionWhenMicrosecondsIsNotOnlyDigits(): void
  45. {
  46. /** @var Mockery\MockInterface & PhpTimeConverter $converter */
  47. $converter = Mockery::mock(PhpTimeConverter::class)->makePartial();
  48. $this->expectException(InvalidArgumentException::class);
  49. $this->expectExceptionMessage(
  50. 'Value must be a signed integer or a string containing only digits '
  51. . '0-9 and, optionally, a sign (+ or -)'
  52. );
  53. $converter->calculateTime('1234', '56.78');
  54. }
  55. /**
  56. * @dataProvider provideConvertTime
  57. */
  58. public function testConvertTime(Hexadecimal $uuidTimestamp, string $unixTimestamp, string $microseconds): void
  59. {
  60. $calculator = new BrickMathCalculator();
  61. $fallbackConverter = new GenericTimeConverter($calculator);
  62. $converter = new PhpTimeConverter($calculator, $fallbackConverter);
  63. $result = $converter->convertTime($uuidTimestamp);
  64. $this->assertSame($unixTimestamp, $result->getSeconds()->toString());
  65. $this->assertSame($microseconds, $result->getMicroseconds()->toString());
  66. }
  67. /**
  68. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  69. */
  70. public function provideConvertTime(): array
  71. {
  72. return [
  73. [
  74. 'uuidTimestamp' => new Hexadecimal('1e1c57dff6f8cb0'),
  75. 'unixTimestamp' => '1341368074',
  76. 'microseconds' => '491000',
  77. ],
  78. [
  79. 'uuidTimestamp' => new Hexadecimal('1ea333764c71df6'),
  80. 'unixTimestamp' => '1578612359',
  81. 'microseconds' => '521023',
  82. ],
  83. [
  84. 'uuidTimestamp' => new Hexadecimal('fffffffff9785f6'),
  85. 'unixTimestamp' => '103072857659',
  86. 'microseconds' => '999999',
  87. ],
  88. // This is the last possible time supported by v1 UUIDs. When
  89. // converted to a Unix timestamp, the microseconds are lost.
  90. // 60038-03-11 05:36:10.955161
  91. [
  92. 'uuidTimestamp' => new Hexadecimal('fffffffffffffffa'),
  93. 'unixTimestamp' => '1832455114570',
  94. 'microseconds' => '955161',
  95. ],
  96. // This is the earliest possible date supported by v1 UUIDs:
  97. // 1582-10-15 00:00:00.000000
  98. [
  99. 'uuidTimestamp' => new Hexadecimal('000000000000'),
  100. 'unixTimestamp' => '-12219292800',
  101. 'microseconds' => '0',
  102. ],
  103. // This is the Unix epoch:
  104. // 1970-01-01 00:00:00.000000
  105. [
  106. 'uuidTimestamp' => new Hexadecimal('1b21dd213814000'),
  107. 'unixTimestamp' => '0',
  108. 'microseconds' => '0',
  109. ],
  110. ];
  111. }
  112. /**
  113. * @dataProvider provideCalculateTime
  114. */
  115. public function testCalculateTime(string $seconds, string $microseconds, string $expected): void
  116. {
  117. $calculator = new BrickMathCalculator();
  118. $fallbackConverter = new GenericTimeConverter($calculator);
  119. $converter = new PhpTimeConverter($calculator, $fallbackConverter);
  120. $result = $converter->calculateTime($seconds, $microseconds);
  121. $this->assertSame($expected, $result->toString());
  122. }
  123. /**
  124. * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
  125. */
  126. public function provideCalculateTime(): array
  127. {
  128. return [
  129. [
  130. 'seconds' => '-12219146756',
  131. 'microseconds' => '0',
  132. 'expected' => '000001540901e600',
  133. ],
  134. [
  135. 'seconds' => '103072857659',
  136. 'microseconds' => '999999',
  137. 'expected' => '0fffffffff9785f6',
  138. ],
  139. [
  140. 'seconds' => '1578612359',
  141. 'microseconds' => '521023',
  142. 'expected' => '01ea333764c71df6',
  143. ],
  144. // This is the earliest possible date supported by v1 UUIDs:
  145. // 1582-10-15 00:00:00.000000
  146. [
  147. 'seconds' => '-12219292800',
  148. 'microseconds' => '0',
  149. 'expected' => '0000000000000000',
  150. ],
  151. // This is the last possible time supported by v1 UUIDs:
  152. // 60038-03-11 05:36:10.955161
  153. [
  154. 'seconds' => '1832455114570',
  155. 'microseconds' => '955161',
  156. 'expected' => 'fffffffffffffffa',
  157. ],
  158. ];
  159. }
  160. }