Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / serializer / Tests / Normalizer / DateTimeNormalizerTest.php
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\DateTimeNormalizer;
16
17 /**
18  * @author Kévin Dunglas <dunglas@gmail.com>
19  */
20 class DateTimeNormalizerTest extends TestCase
21 {
22     /**
23      * @var DateTimeNormalizer
24      */
25     private $normalizer;
26
27     protected function setUp()
28     {
29         $this->normalizer = new DateTimeNormalizer();
30     }
31
32     public function testSupportsNormalization()
33     {
34         $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
35         $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
36         $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
37     }
38
39     public function testNormalize()
40     {
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'))));
43     }
44
45     public function testNormalizeUsingFormatPassedInContext()
46     {
47         $this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')));
48     }
49
50     public function testNormalizeUsingFormatPassedInConstructor()
51     {
52         $this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
53     }
54
55     /**
56      * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
57      * @expectedExceptionMessage The object must implement the "\DateTimeInterface".
58      */
59     public function testNormalizeInvalidObjectThrowsException()
60     {
61         $this->normalizer->normalize(new \stdClass());
62     }
63
64     public function testSupportsDenormalization()
65     {
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'));
70     }
71
72     public function testDenormalize()
73     {
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));
77     }
78
79     public function testDenormalizeUsingFormatPassedInContext()
80     {
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|')));
84     }
85
86     /**
87      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
88      */
89     public function testDenormalizeInvalidDataThrowsException()
90     {
91         $this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
92     }
93
94     /**
95      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
96      */
97     public function testDenormalizeFormatMismatchThrowsException()
98     {
99         $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|'));
100     }
101 }