|
| 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 Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Kévin Dunglas <dunglas@gmail.com> |
| 18 | + */ |
| 19 | +class DateTimeNormalizerTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var DateTimeNormalizer |
| 23 | + */ |
| 24 | + private $normalizer; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->normalizer = new DateTimeNormalizer(); |
| 29 | + } |
| 30 | + |
| 31 | + public function testSupportNormalization() |
| 32 | + { |
| 33 | + $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime())); |
| 34 | + $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable())); |
| 35 | + $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); |
| 36 | + } |
| 37 | + |
| 38 | + public function testNormalize() |
| 39 | + { |
| 40 | + $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01'))); |
| 41 | + $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01'))); |
| 42 | + } |
| 43 | + |
| 44 | + public function testContextFormat() |
| 45 | + { |
| 46 | + $this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y'))); |
| 47 | + } |
| 48 | + |
| 49 | + public function testConstructorFormat() |
| 50 | + { |
| 51 | + $this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01'))); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException |
| 56 | + * @expectedExceptionMessage The object must implement the "\DateTimeInterface". |
| 57 | + */ |
| 58 | + public function testInvalidDataThrowException() |
| 59 | + { |
| 60 | + $this->normalizer->normalize(new \stdClass()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testSupportDenormalization() |
| 64 | + { |
| 65 | + $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class)); |
| 66 | + $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class)); |
| 67 | + $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class)); |
| 68 | + $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar')); |
| 69 | + } |
| 70 | + |
| 71 | + public function testDenormalize() |
| 72 | + { |
| 73 | + $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class)); |
| 74 | + $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class)); |
| 75 | + $this->assertEquals(new \DateTime('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException |
| 80 | + */ |
| 81 | + public function testInvalidDateThrowException() |
| 82 | + { |
| 83 | + $this->normalizer->denormalize('invalid date', \DateTimeInterface::class); |
| 84 | + } |
| 85 | +} |
0 commit comments