--- /dev/null
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Serializer\Tests\Normalizer;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
+
+/**
+ * @author Kévin Dunglas <dunglas@gmail.com>
+ */
+class DateTimeNormalizerTest extends TestCase
+{
+ /**
+ * @var DateTimeNormalizer
+ */
+ private $normalizer;
+
+ protected function setUp()
+ {
+ $this->normalizer = new DateTimeNormalizer();
+ }
+
+ public function testSupportsNormalization()
+ {
+ $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
+ $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
+ $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
+ }
+
+ public function testNormalize()
+ {
+ $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
+ $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC'))));
+ }
+
+ public function testNormalizeUsingFormatPassedInContext()
+ {
+ $this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')));
+ }
+
+ public function testNormalizeUsingFormatPassedInConstructor()
+ {
+ $this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
+ * @expectedExceptionMessage The object must implement the "\DateTimeInterface".
+ */
+ public function testNormalizeInvalidObjectThrowsException()
+ {
+ $this->normalizer->normalize(new \stdClass());
+ }
+
+ public function testSupportsDenormalization()
+ {
+ $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
+ $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class));
+ $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
+ $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
+ }
+
+ public function testDenormalize()
+ {
+ $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
+ $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
+ $this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
+ }
+
+ public function testDenormalizeUsingFormatPassedInContext()
+ {
+ $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
+ $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeImmutable::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
+ $this->assertEquals(new \DateTime('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTime::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ */
+ public function testDenormalizeInvalidDataThrowsException()
+ {
+ $this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ */
+ public function testDenormalizeFormatMismatchThrowsException()
+ {
+ $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|'));
+ }
+}