|
12 | 12 | use PHPUnit\Framework\Attributes\DataProvider; |
13 | 13 | use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; |
14 | 14 | use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Clock\DatePoint; |
15 | 16 |
|
16 | 17 | class DateHandlerTest extends TestCase |
17 | 18 | { |
@@ -188,4 +189,65 @@ public function testDefaultFormat() |
188 | 189 | $this->handler->deserializeDateTimeFromJson($visitor, '2017-06-18T17:32:11Z', $type), |
189 | 190 | ); |
190 | 191 | } |
| 192 | + |
| 193 | + public function testDatePointSerializationJson() |
| 194 | + { |
| 195 | + if (!class_exists(DatePoint::class)) { |
| 196 | + self::markTestSkipped('Symfony Clock component is not available'); |
| 197 | + } |
| 198 | + |
| 199 | + $context = $this->getMockBuilder(SerializationContext::class)->getMock(); |
| 200 | + |
| 201 | + $visitor = $this->getMockBuilder(SerializationVisitorInterface::class)->getMock(); |
| 202 | + $visitor->method('visitString')->with('2017-06-18'); |
| 203 | + |
| 204 | + $datePoint = new DatePoint('2017-06-18 14:30:59', $this->timezone); |
| 205 | + $type = ['name' => DatePoint::class, 'params' => ['Y-m-d']]; |
| 206 | + $this->handler->serializeSymfonyComponentClockDatePoint($visitor, $datePoint, $type, $context); |
| 207 | + } |
| 208 | + |
| 209 | + public function testDatePointDeserializationJson() |
| 210 | + { |
| 211 | + if (!class_exists(DatePoint::class)) { |
| 212 | + self::markTestSkipped('Symfony Clock component is not available'); |
| 213 | + } |
| 214 | + |
| 215 | + $visitor = new JsonDeserializationVisitor(); |
| 216 | + |
| 217 | + $type = ['name' => DatePoint::class, 'params' => ['Y-m-d', '', 'Y-m-d|']]; |
| 218 | + $result = $this->handler->deserializeSymfonyComponentClockDatePointFromJson($visitor, '2017-06-18', $type); |
| 219 | + |
| 220 | + self::assertInstanceOf(DatePoint::class, $result); |
| 221 | + self::assertEquals('2017-06-18', $result->format('Y-m-d')); |
| 222 | + } |
| 223 | + |
| 224 | + public function testDatePointDeserializationReturnsNullForEmptyString() |
| 225 | + { |
| 226 | + if (!class_exists(DatePoint::class)) { |
| 227 | + self::markTestSkipped('Symfony Clock component is not available'); |
| 228 | + } |
| 229 | + |
| 230 | + $visitor = new JsonDeserializationVisitor(); |
| 231 | + |
| 232 | + $type = ['name' => DatePoint::class, 'params' => ['Y-m-d']]; |
| 233 | + self::assertNull($this->handler->deserializeSymfonyComponentClockDatePointFromJson($visitor, '', $type)); |
| 234 | + } |
| 235 | + |
| 236 | + public function testDatePointWithTimezone() |
| 237 | + { |
| 238 | + if (!class_exists(DatePoint::class)) { |
| 239 | + self::markTestSkipped('Symfony Clock component is not available'); |
| 240 | + } |
| 241 | + |
| 242 | + $visitor = new JsonDeserializationVisitor(); |
| 243 | + |
| 244 | + $timestamp = (string) time(); |
| 245 | + $timezone = 'Europe/Brussels'; |
| 246 | + $type = ['name' => DatePoint::class, 'params' => ['U', $timezone]]; |
| 247 | + |
| 248 | + $result = $this->handler->deserializeSymfonyComponentClockDatePointFromJson($visitor, $timestamp, $type); |
| 249 | + |
| 250 | + self::assertInstanceOf(DatePoint::class, $result); |
| 251 | + self::assertEquals($timezone, $result->getTimezone()->getName()); |
| 252 | + } |
191 | 253 | } |
0 commit comments