Update TypeInterface to extend JsonSerializable and Serializable

This commit is contained in:
Ben Ramsey
2020-03-04 16:44:05 -06:00
parent 158d34625b
commit a21eb6c3ca
10 changed files with 273 additions and 38 deletions
+65
View File
@@ -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));
}
}