assertSame((string) $seconds, $time->getSeconds()->toString()); $this->assertSame( (string) $microseconds ?: '0', $time->getMicroseconds()->toString() ); $this->assertSame($timeString, (string) $time); } /** * @return array */ public function provideTimeValues(): array { return [ [ 'seconds' => 103072857659, 'microseconds' => null, ], [ 'seconds' => -12219292800, 'microseconds' => 1234, ], ]; } /** * @param int|float|string|IntegerObject $seconds * @param int|float|string|IntegerObject|null $microseconds * * @dataProvider provideTimeValues */ public function testSerializeUnserializeTime($seconds, $microseconds): void { $params = [$seconds]; if ($microseconds !== null) { $params[] = $microseconds; } $time = new Time(...$params); $serializedTime = serialize($time); /** @var Time $unserializedTime */ $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|IntegerObject $seconds * @param int|float|string|IntegerObject|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)); } }