DefaultTimeGeneratorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ramsey\Uuid\Test\Generator;
  4. use Exception;
  5. use Mockery;
  6. use Mockery\MockInterface;
  7. use PHPUnit\Framework\MockObject\MockObject;
  8. use Ramsey\Uuid\BinaryUtils;
  9. use Ramsey\Uuid\Converter\TimeConverterInterface;
  10. use Ramsey\Uuid\Exception\RandomSourceException;
  11. use Ramsey\Uuid\Exception\TimeSourceException;
  12. use Ramsey\Uuid\FeatureSet;
  13. use Ramsey\Uuid\Generator\DefaultTimeGenerator;
  14. use Ramsey\Uuid\Provider\NodeProviderInterface;
  15. use Ramsey\Uuid\Provider\Time\FixedTimeProvider;
  16. use Ramsey\Uuid\Provider\TimeProviderInterface;
  17. use Ramsey\Uuid\Test\TestCase;
  18. use Ramsey\Uuid\Type\Hexadecimal;
  19. use Ramsey\Uuid\Type\Time;
  20. use phpmock\mockery\PHPMockery;
  21. use function hex2bin;
  22. class DefaultTimeGeneratorTest extends TestCase
  23. {
  24. /**
  25. * @var TimeProviderInterface & MockInterface
  26. */
  27. private $timeProvider;
  28. /**
  29. * @var NodeProviderInterface & MockObject
  30. */
  31. private $nodeProvider;
  32. /**
  33. * @var TimeConverterInterface & MockObject
  34. */
  35. private $timeConverter;
  36. /**
  37. * @var string
  38. */
  39. private $nodeId = '122f80ca9e06';
  40. /**
  41. * @var int[]
  42. */
  43. private $currentTime;
  44. /**
  45. * @var Hexadecimal
  46. */
  47. private $calculatedTime;
  48. /**
  49. * @var int
  50. */
  51. private $clockSeq = 4066;
  52. protected function setUp(): void
  53. {
  54. parent::setUp();
  55. $this->nodeProvider = $this->getMockBuilder(NodeProviderInterface::class)->getMock();
  56. $this->timeConverter = $this->getMockBuilder(TimeConverterInterface::class)->getMock();
  57. $this->currentTime = ['sec' => 1458733431, 'usec' => 877449];
  58. $this->calculatedTime = new Hexadecimal('03cb98e083cb98e0');
  59. $time = new Time($this->currentTime['sec'], $this->currentTime['usec']);
  60. $this->timeProvider = Mockery::mock(TimeProviderInterface::class, [
  61. 'getTime' => $time,
  62. ]);
  63. }
  64. protected function tearDown(): void
  65. {
  66. parent::tearDown();
  67. unset($this->timeProvider, $this->nodeProvider, $this->timeConverter);
  68. Mockery::close();
  69. }
  70. public function testGenerateUsesNodeProviderWhenNodeIsNull(): void
  71. {
  72. $this->nodeProvider->expects($this->once())
  73. ->method('getNode')
  74. ->willReturn(new Hexadecimal('122f80ca9e06'));
  75. $this->timeConverter->expects($this->once())
  76. ->method('calculateTime')
  77. ->with($this->currentTime['sec'], $this->currentTime['usec'])
  78. ->willReturn($this->calculatedTime);
  79. $defaultTimeGenerator = new DefaultTimeGenerator(
  80. $this->nodeProvider,
  81. $this->timeConverter,
  82. $this->timeProvider
  83. );
  84. $defaultTimeGenerator->generate(null, $this->clockSeq);
  85. }
  86. public function testGenerateUsesTimeProvidersCurrentTime(): void
  87. {
  88. $this->timeConverter->expects($this->once())
  89. ->method('calculateTime')
  90. ->with($this->currentTime['sec'], $this->currentTime['usec'])
  91. ->willReturn($this->calculatedTime);
  92. $defaultTimeGenerator = new DefaultTimeGenerator(
  93. $this->nodeProvider,
  94. $this->timeConverter,
  95. $this->timeProvider
  96. );
  97. $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq);
  98. }
  99. public function testGenerateCalculatesTimeWithConverter(): void
  100. {
  101. $this->timeConverter->expects($this->once())
  102. ->method('calculateTime')
  103. ->with($this->currentTime['sec'], $this->currentTime['usec'])
  104. ->willReturn($this->calculatedTime);
  105. $defaultTimeGenerator = new DefaultTimeGenerator(
  106. $this->nodeProvider,
  107. $this->timeConverter,
  108. $this->timeProvider
  109. );
  110. $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq);
  111. }
  112. /**
  113. * @runInSeparateProcess
  114. * @preserveGlobalState disabled
  115. */
  116. public function testGenerateDoesNotApplyVersionAndVariant(): void
  117. {
  118. $expectedBytes = hex2bin('83cb98e098e003cb0fe2122f80ca9e06');
  119. $this->timeConverter->method('calculateTime')
  120. ->with($this->currentTime['sec'], $this->currentTime['usec'])
  121. ->willReturn($this->calculatedTime);
  122. $binaryUtils = Mockery::mock('alias:' . BinaryUtils::class);
  123. $binaryUtils->shouldNotReceive('applyVersion');
  124. $binaryUtils->shouldNotReceive('applyVariant');
  125. $defaultTimeGenerator = new DefaultTimeGenerator(
  126. $this->nodeProvider,
  127. $this->timeConverter,
  128. $this->timeProvider
  129. );
  130. $this->assertSame($expectedBytes, $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq));
  131. }
  132. /**
  133. * @runInSeparateProcess
  134. * @preserveGlobalState disabled
  135. */
  136. public function testGenerateUsesRandomSequenceWhenClockSeqNull(): void
  137. {
  138. PHPMockery::mock('Ramsey\Uuid\Generator', 'random_int')
  139. ->once()
  140. ->with(0, 0x3fff)
  141. ->andReturn(9622);
  142. $this->timeConverter->expects($this->once())
  143. ->method('calculateTime')
  144. ->with($this->currentTime['sec'], $this->currentTime['usec'])
  145. ->willReturn($this->calculatedTime);
  146. $defaultTimeGenerator = new DefaultTimeGenerator(
  147. $this->nodeProvider,
  148. $this->timeConverter,
  149. $this->timeProvider
  150. );
  151. $defaultTimeGenerator->generate($this->nodeId);
  152. }
  153. /**
  154. * @runInSeparateProcess
  155. * @preserveGlobalState disabled
  156. */
  157. public function testGenerateThrowsExceptionWhenExceptionThrownByRandomint(): void
  158. {
  159. PHPMockery::mock('Ramsey\Uuid\Generator', 'random_int')
  160. ->once()
  161. ->andThrow(new Exception('Could not gather sufficient random data'));
  162. $defaultTimeGenerator = new DefaultTimeGenerator(
  163. $this->nodeProvider,
  164. $this->timeConverter,
  165. $this->timeProvider
  166. );
  167. $this->expectException(RandomSourceException::class);
  168. $this->expectExceptionMessage('Could not gather sufficient random data');
  169. $defaultTimeGenerator->generate($this->nodeId);
  170. }
  171. public function testDefaultTimeGeneratorThrowsExceptionForLargeGeneratedValue(): void
  172. {
  173. $timeProvider = new FixedTimeProvider(new Time('1832455114570', '955162'));
  174. $featureSet = new FeatureSet();
  175. $timeGenerator = new DefaultTimeGenerator(
  176. $featureSet->getNodeProvider(),
  177. $featureSet->getTimeConverter(),
  178. $timeProvider
  179. );
  180. $this->expectException(TimeSourceException::class);
  181. $this->expectExceptionMessage(
  182. 'The generated time of \'10000000000000004\' is larger than expected'
  183. );
  184. $timeGenerator->generate();
  185. }
  186. }