From eb1645bc7226d41096f5c77e1e67a48e1d09c4df Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Mon, 13 Jan 2020 10:49:53 -0600 Subject: [PATCH] Add behavior test for TimestampFirstCombCodec --- tests/ExpectedBehaviorTest.php | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/ExpectedBehaviorTest.php b/tests/ExpectedBehaviorTest.php index 3f172ef..87de78f 100644 --- a/tests/ExpectedBehaviorTest.php +++ b/tests/ExpectedBehaviorTest.php @@ -4,7 +4,9 @@ namespace Ramsey\Uuid\Test; use Moontoast\Math\BigNumber; use Ramsey\Uuid\Builder\DegradedUuidBuilder; +use Ramsey\Uuid\Codec\TimestampFirstCombCodec; use Ramsey\Uuid\Converter\Number\DegradedNumberConverter; +use Ramsey\Uuid\Generator\CombGenerator; use Ramsey\Uuid\Generator\DefaultTimeGenerator; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidFactory; @@ -585,4 +587,39 @@ class ExpectedBehaviorTest extends TestCase $this->assertSame('aVersion4Uuid', \Ramsey\Uuid\v4()); $this->assertSame('aVersion5Uuid', \Ramsey\Uuid\v5(Uuid::NAMESPACE_URL, 'https://example.com/foo')); } + + /** + * @link https://git.io/JvJZo Use of TimestampFirstCombCodec in laravel/framework + */ + public function testUseOfTimestampFirstCombCodec() + { + $factory = new UuidFactory(); + + $factory->setRandomGenerator(new CombGenerator( + $factory->getRandomGenerator(), + $factory->getNumberConverter() + )); + + $factory->setCodec(new TimestampFirstCombCodec( + $factory->getUuidBuilder() + )); + + $uuid = $factory->uuid4(); + + // Swap fields according to the rules for TimestampFirstCombCodec. + $fields = array_values($uuid->getFieldsHex()); + $last48Bits = $fields[5]; + $fields[5] = $fields[0] . $fields[1]; + $fields[0] = substr($last48Bits, 0, 8); + $fields[1] = substr($last48Bits, 8, 4); + + $expectedHex = implode('', $fields); + $expectedBytes = hex2bin($expectedHex); + + $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); + $this->assertSame(2, $uuid->getVariant()); + $this->assertSame(4, $uuid->getVersion()); + $this->assertSame($expectedBytes, $uuid->getBytes()); + $this->assertSame($expectedHex, $uuid->getHex()); + } }