96c167d3e44a3d39aa1d7a3091a7fae58a38ca0a
[yaffs-website] / vendor / symfony / var-dumper / Tests / Caster / DateCasterTest.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\VarDumper\Tests\Caster;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\VarDumper\Caster\Caster;
16 use Symfony\Component\VarDumper\Caster\DateCaster;
17 use Symfony\Component\VarDumper\Cloner\Stub;
18 use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
19
20 /**
21  * @author Dany Maillard <danymaillard93b@gmail.com>
22  */
23 class DateCasterTest extends TestCase
24 {
25     use VarDumperTestTrait;
26
27     /**
28      * @dataProvider provideDateTimes
29      */
30     public function testDumpDateTime($time, $timezone, $xDate, $xTimestamp)
31     {
32         if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[-+]\d{2}:\d{2}/', $timezone)) {
33             $this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
34         }
35
36         $date = new \DateTime($time, new \DateTimeZone($timezone));
37
38         $xDump = <<<EODUMP
39 DateTime @$xTimestamp {
40   date: $xDate
41 }
42 EODUMP;
43
44         $this->assertDumpEquals($xDump, $date);
45     }
46
47     /**
48      * @dataProvider provideDateTimes
49      */
50     public function testCastDateTime($time, $timezone, $xDate, $xTimestamp, $xInfos)
51     {
52         if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[-+]\d{2}:\d{2}/', $timezone)) {
53             $this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
54         }
55
56         $stub = new Stub();
57         $date = new \DateTime($time, new \DateTimeZone($timezone));
58         $cast = DateCaster::castDateTime($date, array('foo' => 'bar'), $stub, false, 0);
59
60         $xDump = <<<EODUMP
61 array:1 [
62   "\\x00~\\x00date" => $xDate
63 ]
64 EODUMP;
65
66         $this->assertDumpEquals($xDump, $cast);
67
68         $xDump = <<<EODUMP
69 Symfony\Component\VarDumper\Caster\ConstStub {
70   +type: 1
71   +class: "$xDate"
72   +value: "%A$xInfos%A"
73   +cut: 0
74   +handle: 0
75   +refCount: 0
76   +position: 0
77   +attr: []
78 }
79 EODUMP;
80
81         $this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]);
82     }
83
84     public function provideDateTimes()
85     {
86         return array(
87             array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.0 Europe/Zurich (+02:00)', 1493503200, 'Sunday, April 30, 2017%Afrom now%ADST On'),
88             array('2017-12-31 00:00:00.000000', 'Europe/Zurich', '2017-12-31 00:00:00.0 Europe/Zurich (+01:00)', 1514674800, 'Sunday, December 31, 2017%Afrom now%ADST Off'),
89             array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.0 +02:00', 1493503200, 'Sunday, April 30, 2017%Afrom now'),
90
91             array('2017-04-30 00:00:00.100000', '+00:00', '2017-04-30 00:00:00.100 +00:00', 1493510400, 'Sunday, April 30, 2017%Afrom now'),
92             array('2017-04-30 00:00:00.120000', '+00:00', '2017-04-30 00:00:00.120 +00:00', 1493510400, 'Sunday, April 30, 2017%Afrom now'),
93             array('2017-04-30 00:00:00.123000', '+00:00', '2017-04-30 00:00:00.123 +00:00', 1493510400, 'Sunday, April 30, 2017%Afrom now'),
94             array('2017-04-30 00:00:00.123400', '+00:00', '2017-04-30 00:00:00.123400 +00:00', 1493510400, 'Sunday, April 30, 2017%Afrom now'),
95             array('2017-04-30 00:00:00.123450', '+00:00', '2017-04-30 00:00:00.123450 +00:00', 1493510400, 'Sunday, April 30, 2017%Afrom now'),
96             array('2017-04-30 00:00:00.123456', '+00:00', '2017-04-30 00:00:00.123456 +00:00', 1493510400, 'Sunday, April 30, 2017%Afrom now'),
97         );
98     }
99
100     /**
101      * @dataProvider provideIntervals
102      */
103     public function testDumpInterval($intervalSpec, $ms, $invert, $expected)
104     {
105         if ($ms && PHP_VERSION_ID >= 70200 && version_compare(PHP_VERSION, '7.2.0rc3', '<=')) {
106             $this->markTestSkipped('Skipped on 7.2 before rc4 because of php bug #75354.');
107         }
108
109         $interval = $this->createInterval($intervalSpec, $ms, $invert);
110
111         $xDump = <<<EODUMP
112 DateInterval {
113   interval: $expected
114 %A}
115 EODUMP;
116
117         $this->assertDumpMatchesFormat($xDump, $interval);
118     }
119
120     /**
121      * @dataProvider provideIntervals
122      */
123     public function testDumpIntervalExcludingVerbosity($intervalSpec, $ms, $invert, $expected)
124     {
125         if ($ms && PHP_VERSION_ID >= 70200 && version_compare(PHP_VERSION, '7.2.0rc3', '<=')) {
126             $this->markTestSkipped('Skipped on 7.2 before rc4 because of php bug #75354.');
127         }
128
129         $interval = $this->createInterval($intervalSpec, $ms, $invert);
130
131         $xDump = <<<EODUMP
132 DateInterval {
133   interval: $expected
134 }
135 EODUMP;
136
137         $this->assertDumpEquals($xDump, $interval, Caster::EXCLUDE_VERBOSE);
138     }
139
140     /**
141      * @dataProvider provideIntervals
142      */
143     public function testCastInterval($intervalSpec, $ms, $invert, $xInterval, $xSeconds)
144     {
145         if ($ms && PHP_VERSION_ID >= 70200 && version_compare(PHP_VERSION, '7.2.0rc3', '<=')) {
146             $this->markTestSkipped('Skipped on 7.2 before rc4 because of php bug #75354.');
147         }
148
149         $interval = $this->createInterval($intervalSpec, $ms, $invert);
150         $stub = new Stub();
151
152         $cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
153
154         $xDump = <<<EODUMP
155 array:1 [
156   "\\x00~\\x00interval" => $xInterval
157 ]
158 EODUMP;
159
160         $this->assertDumpEquals($xDump, $cast);
161
162         if (null === $xSeconds) {
163             return;
164         }
165
166         $xDump = <<<EODUMP
167 Symfony\Component\VarDumper\Caster\ConstStub {
168   +type: 1
169   +class: "$xInterval"
170   +value: "$xSeconds"
171   +cut: 0
172   +handle: 0
173   +refCount: 0
174   +position: 0
175   +attr: []
176 }
177 EODUMP;
178
179         $this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]);
180     }
181
182     public function provideIntervals()
183     {
184         $i = new \DateInterval('PT0S');
185         $ms = ($withMs = \PHP_VERSION_ID >= 70100 && isset($i->f)) ? '.0' : '';
186
187         return array(
188             array('PT0S', 0, 0, '0s', '0s'),
189             array('PT0S', 0.1, 0, $withMs ? '+ 00:00:00.100' : '0s', '%is'),
190             array('PT1S', 0, 0, '+ 00:00:01'.$ms, '%is'),
191             array('PT2M', 0, 0, '+ 00:02:00'.$ms, '%is'),
192             array('PT3H', 0, 0, '+ 03:00:00'.$ms, '%ss'),
193             array('P4D', 0, 0, '+ 4d', '%ss'),
194             array('P5M', 0, 0, '+ 5m', null),
195             array('P6Y', 0, 0, '+ 6y', null),
196             array('P1Y2M3DT4H5M6S', 0, 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
197             array('PT1M60S', 0, 0, '+ 00:02:00'.$ms, null),
198             array('PT1H60M', 0, 0, '+ 02:00:00'.$ms, null),
199             array('P1DT24H', 0, 0, '+ 2d', null),
200             array('P1M32D', 0, 0, '+ 1m 32d', null),
201
202             array('PT0S', 0, 1, '0s', '0s'),
203             array('PT0S', 0.1, 1, $withMs ? '- 00:00:00.100' : '0s', '%is'),
204             array('PT1S', 0, 1, '- 00:00:01'.$ms, '%is'),
205             array('PT2M', 0, 1, '- 00:02:00'.$ms, '%is'),
206             array('PT3H', 0, 1, '- 03:00:00'.$ms, '%ss'),
207             array('P4D', 0, 1, '- 4d', '%ss'),
208             array('P5M', 0, 1, '- 5m', null),
209             array('P6Y', 0, 1, '- 6y', null),
210             array('P1Y2M3DT4H5M6S', 0, 1, '- 1y 2m 3d 04:05:06'.$ms, null),
211             array('PT1M60S', 0, 1, '- 00:02:00'.$ms, null),
212             array('PT1H60M', 0, 1, '- 02:00:00'.$ms, null),
213             array('P1DT24H', 0, 1, '- 2d', null),
214             array('P1M32D', 0, 1, '- 1m 32d', null),
215         );
216     }
217
218     /**
219      * @dataProvider provideTimeZones
220      */
221     public function testDumpTimeZone($timezone, $expected)
222     {
223         if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
224             $this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
225         }
226
227         $timezone = new \DateTimeZone($timezone);
228
229         $xDump = <<<EODUMP
230 DateTimeZone {
231   timezone: $expected
232 %A}
233 EODUMP;
234
235         $this->assertDumpMatchesFormat($xDump, $timezone);
236     }
237
238     /**
239      * @dataProvider provideTimeZones
240      */
241     public function testDumpTimeZoneExcludingVerbosity($timezone, $expected)
242     {
243         if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
244             $this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
245         }
246
247         $timezone = new \DateTimeZone($timezone);
248
249         $xDump = <<<EODUMP
250 DateTimeZone {
251   timezone: $expected
252 }
253 EODUMP;
254
255         $this->assertDumpMatchesFormat($xDump, $timezone, Caster::EXCLUDE_VERBOSE);
256     }
257
258     /**
259      * @dataProvider provideTimeZones
260      */
261     public function testCastTimeZone($timezone, $xTimezone, $xRegion)
262     {
263         if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && !preg_match('/\w+\/\w+/', $timezone)) {
264             $this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.');
265         }
266
267         $timezone = new \DateTimeZone($timezone);
268         $stub = new Stub();
269
270         $cast = DateCaster::castTimeZone($timezone, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
271
272         $xDump = <<<EODUMP
273 array:1 [
274   "\\x00~\\x00timezone" => $xTimezone
275 ]
276 EODUMP;
277
278         $this->assertDumpMatchesFormat($xDump, $cast);
279
280         $xDump = <<<EODUMP
281 Symfony\Component\VarDumper\Caster\ConstStub {
282   +type: 1
283   +class: "$xTimezone"
284   +value: "$xRegion"
285   +cut: 0
286   +handle: 0
287   +refCount: 0
288   +position: 0
289   +attr: []
290 }
291 EODUMP;
292
293         $this->assertDumpMatchesFormat($xDump, $cast["\0~\0timezone"]);
294     }
295
296     public function provideTimeZones()
297     {
298         $xRegion = extension_loaded('intl') ? '%s' : '';
299
300         return array(
301             // type 1 (UTC offset)
302             array('-12:00', '-12:00', ''),
303             array('+00:00', '+00:00', ''),
304             array('+14:00', '+14:00', ''),
305
306             // type 2 (timezone abbreviation)
307             array('GMT', '+00:00', ''),
308             array('a', '+01:00', ''),
309             array('b', '+02:00', ''),
310             array('z', '+00:00', ''),
311
312             // type 3 (timezone identifier)
313             array('Africa/Tunis', 'Africa/Tunis (%s:00)', $xRegion),
314             array('America/Panama', 'America/Panama (%s:00)', $xRegion),
315             array('Asia/Jerusalem', 'Asia/Jerusalem (%s:00)', $xRegion),
316             array('Atlantic/Canary', 'Atlantic/Canary (%s:00)', $xRegion),
317             array('Australia/Perth', 'Australia/Perth (%s:00)', $xRegion),
318             array('Europe/Zurich', 'Europe/Zurich (%s:00)', $xRegion),
319             array('Pacific/Tahiti', 'Pacific/Tahiti (%s:00)', $xRegion),
320         );
321     }
322
323     /**
324      * @dataProvider providePeriods
325      */
326     public function testDumpPeriod($start, $interval, $end, $options, $expected)
327     {
328         if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50620 || (\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70005)) {
329             $this->markTestSkipped();
330         }
331
332         $p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
333
334         $xDump = <<<EODUMP
335 DatePeriod {
336   period: $expected
337 %A}
338 EODUMP;
339
340         $this->assertDumpMatchesFormat($xDump, $p);
341     }
342
343     /**
344      * @dataProvider providePeriods
345      */
346     public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDates)
347     {
348         if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50620 || (\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70005)) {
349             $this->markTestSkipped();
350         }
351
352         $p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
353         $stub = new Stub();
354
355         $cast = DateCaster::castPeriod($p, array(), $stub, false, 0);
356
357         $xDump = <<<EODUMP
358 array:1 [
359   "\\x00~\\x00period" => $xPeriod
360 ]
361 EODUMP;
362
363         $this->assertDumpEquals($xDump, $cast);
364
365         $xDump = <<<EODUMP
366 Symfony\Component\VarDumper\Caster\ConstStub {
367   +type: 1
368   +class: "$xPeriod"
369   +value: "%A$xDates%A"
370   +cut: 0
371   +handle: 0
372   +refCount: 0
373   +position: 0
374   +attr: []
375 }
376 EODUMP;
377
378         $this->assertDumpMatchesFormat($xDump, $cast["\0~\0period"]);
379     }
380
381     public function providePeriods()
382     {
383         $i = new \DateInterval('PT0S');
384         $ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
385
386         $periods = array(
387             array('2017-01-01', 'P1D', '2017-01-03', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-03 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02'),
388             array('2017-01-01', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 2 time/s', '1) 2017-01-01%a2) 2017-01-02'),
389
390             array('2017-01-01', 'P1D', '2017-01-04', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-04 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
391             array('2017-01-01', 'P1D', 2, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 3 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03'),
392
393             array('2017-01-01', 'P1D', '2017-01-05', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-05 00:00:00.0', '1) 2017-01-01%a2) 2017-01-02%a1 more'),
394             array('2017-01-01', 'P1D', 3, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 4 time/s', '1) 2017-01-01%a2) 2017-01-02%a3) 2017-01-03%a1 more'),
395
396             array('2017-01-01', 'P1D', '2017-01-21', 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) to 2017-01-21 00:00:00.0', '1) 2017-01-01%a17 more'),
397             array('2017-01-01', 'P1D', 19, 0, 'every + 1d, from 2017-01-01 00:00:00.0 (included) recurring 20 time/s', '1) 2017-01-01%a17 more'),
398
399             array('2017-01-01 01:00:00', 'P1D', '2017-01-03 01:00:00', 0, 'every + 1d, from 2017-01-01 01:00:00.0 (included) to 2017-01-03 01:00:00.0', '1) 2017-01-01 01:00:00.0%a2) 2017-01-02 01:00:00.0'),
400             array('2017-01-01 01:00:00', 'P1D', 1, 0, 'every + 1d, from 2017-01-01 01:00:00.0 (included) recurring 2 time/s', '1) 2017-01-01 01:00:00.0%a2) 2017-01-02 01:00:00.0'),
401
402             array('2017-01-01', 'P1DT1H', '2017-01-03', 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00.0 (included) to 2017-01-03 00:00:00.0", '1) 2017-01-01 00:00:00.0%a2) 2017-01-02 01:00:00.0'),
403             array('2017-01-01', 'P1DT1H', 1, 0, "every + 1d 01:00:00$ms, from 2017-01-01 00:00:00.0 (included) recurring 2 time/s", '1) 2017-01-01 00:00:00.0%a2) 2017-01-02 01:00:00.0'),
404
405             array('2017-01-01', 'P1D', '2017-01-04', \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00.0 (excluded) to 2017-01-04 00:00:00.0', '1) 2017-01-02%a2) 2017-01-03'),
406             array('2017-01-01', 'P1D', 2, \DatePeriod::EXCLUDE_START_DATE, 'every + 1d, from 2017-01-01 00:00:00.0 (excluded) recurring 2 time/s', '1) 2017-01-02%a2) 2017-01-03'),
407         );
408
409         if (\PHP_VERSION_ID < 70107) {
410             array_walk($periods, function (&$i) { $i[5] = ''; });
411         }
412
413         return $periods;
414     }
415
416     private function createInterval($intervalSpec, $ms, $invert)
417     {
418         $interval = new \DateInterval($intervalSpec);
419         if (\PHP_VERSION_ID >= 70100 && isset($interval->f)) {
420             $interval->f = $ms;
421         }
422         $interval->invert = $invert;
423
424         return $interval;
425     }
426 }