Updated to Drupal 8.5. Core Media not yet in use.
[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     public function testNormalizeUsingTimeZonePassedInConstructor()
56     {
57         $normalizer = new DateTimeNormalizer(\DateTime::RFC3339, new \DateTimeZone('Japan'));
58
59         $this->assertSame('2016-12-01T00:00:00+09:00', $normalizer->normalize(new \DateTime('2016/12/01', new \DateTimeZone('Japan'))));
60         $this->assertSame('2016-12-01T09:00:00+09:00', $normalizer->normalize(new \DateTime('2016/12/01', new \DateTimeZone('UTC'))));
61     }
62
63     /**
64      * @dataProvider normalizeUsingTimeZonePassedInContextProvider
65      */
66     public function testNormalizeUsingTimeZonePassedInContext($expected, $input, $timezone)
67     {
68         $this->assertSame($expected, $this->normalizer->normalize($input, null, array(
69             DateTimeNormalizer::TIMEZONE_KEY => $timezone,
70         )));
71     }
72
73     public function normalizeUsingTimeZonePassedInContextProvider()
74     {
75         yield array('2016-12-01T00:00:00+00:00', new \DateTime('2016/12/01', new \DateTimeZone('UTC')), null);
76         yield array('2016-12-01T00:00:00+09:00', new \DateTime('2016/12/01', new \DateTimeZone('Japan')), new \DateTimeZone('Japan'));
77         yield array('2016-12-01T09:00:00+09:00', new \DateTime('2016/12/01', new \DateTimeZone('UTC')), new \DateTimeZone('Japan'));
78         yield array('2016-12-01T09:00:00+09:00', new \DateTimeImmutable('2016/12/01', new \DateTimeZone('UTC')), new \DateTimeZone('Japan'));
79     }
80
81     /**
82      * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
83      * @expectedExceptionMessage The object must implement the "\DateTimeInterface".
84      */
85     public function testNormalizeInvalidObjectThrowsException()
86     {
87         $this->normalizer->normalize(new \stdClass());
88     }
89
90     public function testSupportsDenormalization()
91     {
92         $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
93         $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class));
94         $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
95         $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
96     }
97
98     public function testDenormalize()
99     {
100         $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
101         $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
102         $this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
103     }
104
105     public function testDenormalizeUsingTimezonePassedInConstructor()
106     {
107         $timezone = new \DateTimeZone('Japan');
108         $expected = new \DateTime('2016/12/01 17:35:00', $timezone);
109         $normalizer = new DateTimeNormalizer(null, $timezone);
110
111         $this->assertEquals($expected, $normalizer->denormalize('2016.12.01 17:35:00', \DateTime::class, null, array(
112             DateTimeNormalizer::FORMAT_KEY => 'Y.m.d H:i:s',
113         )));
114     }
115
116     public function testDenormalizeUsingFormatPassedInContext()
117     {
118         $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
119         $this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeImmutable::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
120         $this->assertEquals(new \DateTime('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTime::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
121     }
122
123     /**
124      * @dataProvider denormalizeUsingTimezonePassedInContextProvider
125      */
126     public function testDenormalizeUsingTimezonePassedInContext($input, $expected, $timezone, $format = null)
127     {
128         $actual = $this->normalizer->denormalize($input, \DateTimeInterface::class, null, array(
129             DateTimeNormalizer::TIMEZONE_KEY => $timezone,
130             DateTimeNormalizer::FORMAT_KEY => $format,
131         ));
132
133         $this->assertEquals($expected, $actual);
134     }
135
136     public function denormalizeUsingTimezonePassedInContextProvider()
137     {
138         yield 'with timezone' => array(
139             '2016/12/01 17:35:00',
140             new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
141             new \DateTimeZone('Japan'),
142         );
143         yield 'with timezone as string' => array(
144             '2016/12/01 17:35:00',
145             new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
146             'Japan',
147         );
148         yield 'with format without timezone information' => array(
149             '2016.12.01 17:35:00',
150             new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
151             new \DateTimeZone('Japan'),
152             'Y.m.d H:i:s',
153         );
154         yield 'ignored with format with timezone information' => array(
155             '2016-12-01T17:35:00Z',
156             new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('UTC')),
157             'Europe/Paris',
158             \DateTime::RFC3339,
159         );
160     }
161
162     /**
163      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
164      */
165     public function testDenormalizeInvalidDataThrowsException()
166     {
167         $this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
168     }
169
170     /**
171      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
172      * @expectedExceptionMessage The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.
173      */
174     public function testDenormalizeNullThrowsException()
175     {
176         $this->normalizer->denormalize(null, \DateTimeInterface::class);
177     }
178
179     /**
180      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
181      * @expectedExceptionMessage The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.
182      */
183     public function testDenormalizeEmptyStringThrowsException()
184     {
185         $this->normalizer->denormalize('', \DateTimeInterface::class);
186     }
187
188     /**
189      * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
190      */
191     public function testDenormalizeFormatMismatchThrowsException()
192     {
193         $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|'));
194     }
195 }