mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-26 17:46:38 +03:00
Update TypeInterface to extend JsonSerializable and Serializable
This commit is contained in:
@@ -4,9 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Test\Type;
|
||||
|
||||
use Ramsey\Uuid\Exception\UnsupportedOperationException;
|
||||
use Ramsey\Uuid\Test\TestCase;
|
||||
use Ramsey\Uuid\Type\Time;
|
||||
|
||||
use function json_encode;
|
||||
use function serialize;
|
||||
use function unserialize;
|
||||
|
||||
class TimeTest extends TestCase
|
||||
{
|
||||
/**
|
||||
@@ -55,4 +60,64 @@ class TimeTest extends TestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|float|string $seconds
|
||||
* @param int|float|string|null $microSeconds
|
||||
*
|
||||
* @dataProvider provideTimeValues
|
||||
*/
|
||||
public function testSerializeUnserializeTime($seconds, $microSeconds): void
|
||||
{
|
||||
$params = [$seconds];
|
||||
if ($microSeconds !== null) {
|
||||
$params[] = $microSeconds;
|
||||
}
|
||||
|
||||
$time = new Time(...$params);
|
||||
$serializedTime = serialize($time);
|
||||
$unserializedTime = unserialize($serializedTime);
|
||||
|
||||
$this->assertSame((string) $seconds, $unserializedTime->getSeconds()->toString());
|
||||
|
||||
$this->assertSame(
|
||||
(string) $microSeconds ?: '0',
|
||||
$unserializedTime->getMicroSeconds()->toString()
|
||||
);
|
||||
}
|
||||
|
||||
public function testUnserializeOfInvalidValueException(): void
|
||||
{
|
||||
$invalidSerialization = 'C:21:"Ramsey\\Uuid\\Type\\Time":13:{{"foo":"bar"}}';
|
||||
|
||||
$this->expectException(UnsupportedOperationException::class);
|
||||
$this->expectExceptionMessage('Attempted to unserialize an invalid value');
|
||||
|
||||
unserialize($invalidSerialization);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|float|string $seconds
|
||||
* @param int|float|string|null $microSeconds
|
||||
*
|
||||
* @dataProvider provideTimeValues
|
||||
*/
|
||||
public function testJsonSerialize($seconds, $microSeconds): void
|
||||
{
|
||||
$time = [
|
||||
'seconds' => (string) $seconds,
|
||||
'microseconds' => (string) $microSeconds ?: '0',
|
||||
];
|
||||
|
||||
$expectedJson = json_encode($time);
|
||||
|
||||
$params = [$seconds];
|
||||
if ($microSeconds !== null) {
|
||||
$params[] = $microSeconds;
|
||||
}
|
||||
|
||||
$time = new Time(...$params);
|
||||
|
||||
$this->assertSame($expectedJson, json_encode($time));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user