Skip to content

Commit

Permalink
Fix Unserialize error with OrderedTimeCodec #494 (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0tonic committed Apr 15, 2023
1 parent abe81c1 commit 628d3df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
22 changes: 11 additions & 11 deletions src/Uuid.php
Expand Up @@ -293,7 +293,7 @@ public function jsonSerialize(): string
*/
public function serialize(): string
{
return $this->getFields()->getBytes();
return $this->codec->encode($this);
}

/**
Expand Down Expand Up @@ -446,20 +446,20 @@ public static function setFactory(UuidFactoryInterface $factory): void
*/
public static function fromBytes(string $bytes): UuidInterface
{
if (! self::$factoryReplaced && strlen($bytes) === 16) {
if (!self::$factoryReplaced && strlen($bytes) === 16) {
$base16Uuid = bin2hex($bytes);

// Note: we are calling `fromString` internally because we don't know if the given `$bytes` is a valid UUID
return self::fromString(
substr($base16Uuid, 0, 8)
. '-'
. substr($base16Uuid, 8, 4)
. '-'
. substr($base16Uuid, 12, 4)
. '-'
. substr($base16Uuid, 16, 4)
. '-'
. substr($base16Uuid, 20, 12)
. '-'
. substr($base16Uuid, 8, 4)
. '-'
. substr($base16Uuid, 12, 4)
. '-'
. substr($base16Uuid, 16, 4)
. '-'
. substr($base16Uuid, 20, 12)
);
}

Expand All @@ -485,7 +485,7 @@ public static function fromBytes(string $bytes): UuidInterface
public static function fromString(string $uuid): UuidInterface
{
$uuid = strtolower($uuid);
if (! self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) {
if (!self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) {
assert($uuid !== '');

return new LazyUuidFromString($uuid);
Expand Down
65 changes: 45 additions & 20 deletions tests/ExpectedBehaviorTest.php
Expand Up @@ -5,6 +5,7 @@
use Brick\Math\BigInteger;
use Ramsey\Uuid\Builder\DegradedUuidBuilder;
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Codec\OrderedTimeCodec;
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
use Ramsey\Uuid\Converter\Number\DegradedNumberConverter;
use Ramsey\Uuid\Converter\Time\DegradedTimeConverter;
Expand Down Expand Up @@ -74,21 +75,21 @@ public function testStaticCreationMethodsAndStandardBehavior($method, $args)
$this->assertSame(
(string) $uuid->getHex(),
$uuid->getTimeLowHex()
. $uuid->getTimeMidHex()
. $uuid->getTimeHiAndVersionHex()
. $uuid->getClockSeqHiAndReservedHex()
. $uuid->getClockSeqLowHex()
. $uuid->getNodeHex()
. $uuid->getTimeMidHex()
. $uuid->getTimeHiAndVersionHex()
. $uuid->getClockSeqHiAndReservedHex()
. $uuid->getClockSeqLowHex()
. $uuid->getNodeHex()
);

$this->assertSame(
(string) $uuid->getHex(),
$uuid->getFieldsHex()['time_low']
. $uuid->getFieldsHex()['time_mid']
. $uuid->getFieldsHex()['time_hi_and_version']
. $uuid->getFieldsHex()['clock_seq_hi_and_reserved']
. $uuid->getFieldsHex()['clock_seq_low']
. $uuid->getFieldsHex()['node']
. $uuid->getFieldsHex()['time_mid']
. $uuid->getFieldsHex()['time_hi_and_version']
. $uuid->getFieldsHex()['clock_seq_hi_and_reserved']
. $uuid->getFieldsHex()['clock_seq_low']
. $uuid->getFieldsHex()['node']
);

$this->assertIsString($uuid->getUrn());
Expand All @@ -100,21 +101,21 @@ public function testStaticCreationMethodsAndStandardBehavior($method, $args)
$this->assertSame(
$uuid->toString(),
$uuid->getTimeLowHex() . '-'
. $uuid->getTimeMidHex() . '-'
. $uuid->getTimeHiAndVersionHex() . '-'
. $uuid->getClockSeqHiAndReservedHex()
. $uuid->getClockSeqLowHex() . '-'
. $uuid->getNodeHex()
. $uuid->getTimeMidHex() . '-'
. $uuid->getTimeHiAndVersionHex() . '-'
. $uuid->getClockSeqHiAndReservedHex()
. $uuid->getClockSeqLowHex() . '-'
. $uuid->getNodeHex()
);

$this->assertSame(
(string) $uuid,
$uuid->getTimeLowHex() . '-'
. $uuid->getTimeMidHex() . '-'
. $uuid->getTimeHiAndVersionHex() . '-'
. $uuid->getClockSeqHiAndReservedHex()
. $uuid->getClockSeqLowHex() . '-'
. $uuid->getNodeHex()
. $uuid->getTimeMidHex() . '-'
. $uuid->getTimeHiAndVersionHex() . '-'
. $uuid->getClockSeqHiAndReservedHex()
. $uuid->getClockSeqLowHex() . '-'
. $uuid->getNodeHex()
);

$this->assertSame(2, $uuid->getVariant());
Expand Down Expand Up @@ -240,6 +241,30 @@ public function testSerialization($string)
$this->assertSame("\"{$string}\"", json_encode($uuid));
}

/**
* @dataProvider provideFromStringInteger
*/
public function testSerializationWithOrderedTimeCodec($string)
{
$factory = new UuidFactory();
$factory->setCodec(new OrderedTimeCodec(
$factory->getUuidBuilder()
));

$previousFactory = Uuid::getFactory();
Uuid::setFactory($factory);
$uuid = Uuid::fromString($string);

$serialized = serialize($uuid);
$unserialized = unserialize($serialized);

Uuid::setFactory($previousFactory);

$this->assertSame(0, $uuid->compareTo($unserialized));
$this->assertTrue($uuid->equals($unserialized));
$this->assertSame("\"{$string}\"", json_encode($uuid));
}

/**
* @dataProvider provideFromStringInteger
*/
Expand Down

0 comments on commit 628d3df

Please sign in to comment.