|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Serializer\Tests\Normalizer; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Jérôme Desjardins <jewome62@gmail.com> |
| 19 | + */ |
| 20 | +class DateTimeZoneNormalizerTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var DateTimeZoneNormalizer |
| 24 | + */ |
| 25 | + private $normalizer; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->normalizer = new DateTimeZoneNormalizer(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSupportsNormalization() |
| 33 | + { |
| 34 | + $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeZone('UTC'))); |
| 35 | + $this->assertFalse($this->normalizer->supportsNormalization(new \DateTimeImmutable())); |
| 36 | + $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); |
| 37 | + } |
| 38 | + |
| 39 | + public function testNormalize() |
| 40 | + { |
| 41 | + $this->assertEquals('UTC', $this->normalizer->normalize(new \DateTimeZone('UTC'))); |
| 42 | + $this->assertEquals('Asia/Tokyo', $this->normalizer->normalize(new \DateTimeZone('Asia/Tokyo'))); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException |
| 47 | + */ |
| 48 | + public function testNormalizeBadObjectTypeThrowsException() |
| 49 | + { |
| 50 | + $this->normalizer->normalize(new \stdClass()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testSupportsDenormalization() |
| 54 | + { |
| 55 | + $this->assertTrue($this->normalizer->supportsDenormalization(null, \DateTimeZone::class)); |
| 56 | + $this->assertFalse($this->normalizer->supportsDenormalization(null, \DateTimeImmutable::class)); |
| 57 | + $this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testDenormalize() |
| 61 | + { |
| 62 | + $this->assertEquals(new \DateTimeZone('UTC'), $this->normalizer->denormalize('UTC', \DateTimeZone::class, null)); |
| 63 | + $this->assertEquals(new \DateTimeZone('Asia/Tokyo'), $this->normalizer->denormalize('Asia/Tokyo', \DateTimeZone::class, null)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException |
| 68 | + */ |
| 69 | + public function testDenormalizeNullTimeZoneThrowsException() |
| 70 | + { |
| 71 | + $this->normalizer->denormalize(null, \DateTimeZone::class, null); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException |
| 76 | + */ |
| 77 | + public function testDenormalizeBadTimeZoneThrowsException() |
| 78 | + { |
| 79 | + $this->normalizer->denormalize('Jupiter/Europa', \DateTimeZone::class, null); |
| 80 | + } |
| 81 | +} |
0 commit comments