4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Normalizer;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
18 * @author Kévin Dunglas <dunglas@gmail.com>
20 class DateTimeNormalizerTest extends TestCase
23 * @var DateTimeNormalizer
27 protected function setUp()
29 $this->normalizer = new DateTimeNormalizer();
32 public function testSupportsNormalization()
34 $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
35 $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
36 $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
39 public function testNormalize()
41 $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
42 $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC'))));
45 public function testNormalizeUsingFormatPassedInContext()
47 $this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')));
50 public function testNormalizeUsingFormatPassedInConstructor()
52 $this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
56 * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
57 * @expectedExceptionMessage The object must implement the "\DateTimeInterface".
59 public function testNormalizeInvalidObjectThrowsException()
61 $this->normalizer->normalize(new \stdClass());
64 public function testSupportsDenormalization()
66 $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
67 $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class));
68 $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
69 $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
72 public function testDenormalize()
74 $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
75 $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
76 $this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
79 public function testDenormalizeUsingFormatPassedInContext()
81 $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
82 $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeImmutable::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
83 $this->assertEquals(new \DateTime('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTime::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
87 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
89 public function testDenormalizeInvalidDataThrowsException()
91 $this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
95 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
97 public function testDenormalizeFormatMismatchThrowsException()
99 $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|'));