From 6a312ead813ed73f6f1593d3314a0f5a13975d58 Mon Sep 17 00:00:00 2001 From: David Badura Date: Tue, 10 May 2022 08:57:55 +0200 Subject: [PATCH] add timezone normalizer --- .../Normalizer/DateTimeZoneNormalizer.php | 38 +++++++++++++ .../Normalizer/DateTimeZoneNormalizerTest.php | 56 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/Serializer/Normalizer/DateTimeZoneNormalizer.php create mode 100644 tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php diff --git a/src/Serializer/Normalizer/DateTimeZoneNormalizer.php b/src/Serializer/Normalizer/DateTimeZoneNormalizer.php new file mode 100644 index 000000000..a1d04b317 --- /dev/null +++ b/src/Serializer/Normalizer/DateTimeZoneNormalizer.php @@ -0,0 +1,38 @@ +getName(); + } + + public function denormalize(mixed $value): ?DateTimeZone + { + if ($value === null) { + return null; + } + + if (!is_string($value)) { + throw new InvalidArgument(); + } + + return new DateTimeZone($value); + } +} diff --git a/tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php b/tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php new file mode 100644 index 000000000..5d6223f71 --- /dev/null +++ b/tests/Unit/Serializer/Normalizer/DateTimeZoneNormalizerTest.php @@ -0,0 +1,56 @@ +assertEquals(null, $normalizer->normalize(null)); + } + + public function testDenormalizeWithNull(): void + { + $normalizer = new DateTimeZoneNormalizer(); + $this->assertEquals(null, $normalizer->denormalize(null)); + } + + public function testNormalizeWithInvalidArgument(): void + { + $this->expectException(InvalidArgument::class); + + $normalizer = new DateTimeZoneNormalizer(); + $normalizer->normalize(123); + } + + public function testDenormalizeWithInvalidArgument(): void + { + $this->expectException(InvalidArgument::class); + + $normalizer = new DateTimeZoneNormalizer(); + $normalizer->denormalize(123); + } + + public function testNormalizeWithValue(): void + { + $normalizer = new DateTimeZoneNormalizer(); + $this->assertEquals('EDT', $normalizer->normalize(new DateTimeZone('EDT'))); + } + + public function testDenormalizeWithValue(): void + { + $normalizer = new DateTimeZoneNormalizer(); + $this->assertEquals(new DateTimeZone('EDT'), $normalizer->denormalize('EDT')); + } +}