timeProvider = $this->getMockBuilder(TimeProviderInterface::class)->getMock(); $this->nodeProvider = $this->getMockBuilder(NodeProviderInterface::class)->getMock(); $this->timeConverter = $this->getMockBuilder(TimeConverterInterface::class)->getMock(); $this->currentTime = ["sec" => 1458733431, "usec" => 877449]; $this->calculatedTime = ["low" => "83cb98e0", "mid" => "98e0", "hi" => "03cb"]; } protected function tearDown(): void { parent::tearDown(); unset($this->timeProvider, $this->nodeProvider, $this->timeConverter); Mockery::close(); AspectMock::clean(); } public function testGenerateUsesNodeProviderWhenNodeIsNull() { $this->nodeProvider->expects($this->once()) ->method('getNode') ->willReturn('122f80ca9e06'); $this->timeProvider->method('currentTime') ->willReturn($this->currentTime); $this->timeConverter->expects($this->once()) ->method('calculateTime') ->with($this->currentTime['sec'], $this->currentTime['usec']) ->willReturn($this->calculatedTime); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, $this->timeConverter, $this->timeProvider ); $defaultTimeGenerator->generate(null, $this->clockSeq); } public function testGenerateUsesTimeProvidersCurrentTime() { $this->timeProvider->expects($this->once()) ->method('currentTime') ->willReturn($this->currentTime); $this->timeConverter->expects($this->once()) ->method('calculateTime') ->with($this->currentTime['sec'], $this->currentTime['usec']) ->willReturn($this->calculatedTime); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, $this->timeConverter, $this->timeProvider ); $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq); } public function testGenerateCalculatesTimeWithConverter() { $this->timeProvider->method('currentTime') ->willReturn($this->currentTime); $this->timeConverter->expects($this->once()) ->method('calculateTime') ->with($this->currentTime['sec'], $this->currentTime['usec']) ->willReturn($this->calculatedTime); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, $this->timeConverter, $this->timeProvider ); $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq); } /** * @runInSeparateProcess * @preserveGlobalState disabled */ public function testGenerateAppliesVersionAndVariant() { $expectedBytes = hex2bin('83cb98e098e003cb8fe2122f80ca9e06'); $this->timeProvider->method('currentTime') ->willReturn($this->currentTime); $this->timeConverter->method('calculateTime') ->with($this->currentTime['sec'], $this->currentTime['usec']) ->willReturn($this->calculatedTime); $binaryUtils = Mockery::mock('alias:'.BinaryUtils::class); $binaryUtils->shouldReceive('applyVersion') ->with($this->calculatedTime['hi'], 1) ->andReturn(971); $clockSeqShifted = 15; $binaryUtils->shouldReceive('applyVariant') ->with($clockSeqShifted) ->andReturn(143); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, $this->timeConverter, $this->timeProvider ); $this->assertSame($expectedBytes, $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq)); } /** * @runInSeparateProcess * @preserveGlobalState disabled */ public function testGenerateReturnsBinaryStringInUuidFormat() { $this->timeProvider->method('currentTime')->willReturn($this->currentTime); $this->timeConverter->method('calculateTime')->willReturn($this->calculatedTime); $binaryUtils = Mockery::mock('alias:'.BinaryUtils::class); $binaryUtils->shouldReceive('applyVersion')->andReturn(971); $binaryUtils->shouldReceive('applyVariant')->andReturn(143); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, $this->timeConverter, $this->timeProvider ); $result = $defaultTimeGenerator->generate($this->nodeId, $this->clockSeq); /** * // Given we use values: * $low = '83cb98e0'; * $mid = '98e0'; * $timeHi = 971; * $clockSeqHi = 143; * $clockSeq = 4066; * $node = '122f80ca9e06'; * * $values = [$low, $mid, * sprintf('%04x', $timeHi), sprintf('%02x', $clockSeqHi), * sprintf('%02x', $clockSeq & 0xff), $node]; * * // then: * $hex = vsprintf('%08s%04s%04s%02s%02s%012s', $values); */ $hex = '83cb98e098e003cb8fe2122f80ca9e06'; $binary = hex2bin($hex); $this->assertEquals($binary, $result); } /** * @runInSeparateProcess * @preserveGlobalState disabled */ public function testGenerateUsesRandomSequenceWhenClockSeqNull() { $random_int = AspectMock::func('Ramsey\Uuid\Generator', 'random_int', 9622); $this->timeProvider->method('currentTime') ->willReturn($this->currentTime); $this->timeConverter->expects($this->once()) ->method('calculateTime') ->with($this->currentTime['sec'], $this->currentTime['usec']) ->willReturn($this->calculatedTime); $defaultTimeGenerator = new DefaultTimeGenerator( $this->nodeProvider, $this->timeConverter, $this->timeProvider ); $defaultTimeGenerator->generate($this->nodeId); $random_int->verifyInvokedOnce([0, 0x3fff]); } }