4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Tests\Caster;
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;
21 * @author Dany Maillard <danymaillard93b@gmail.com>
23 class DateCasterTest extends TestCase
25 use VarDumperTestTrait;
28 * @dataProvider provideDateTimes
30 public function testDumpDateTime($time, $timezone, $xDate, $xTimestamp)
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.');
36 $date = new \DateTime($time, new \DateTimeZone($timezone));
39 DateTime @$xTimestamp {
44 $this->assertDumpEquals($xDump, $date);
48 * @dataProvider provideDateTimes
50 public function testCastDateTime($time, $timezone, $xDate, $xTimestamp, $xInfos)
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.');
57 $date = new \DateTime($time, new \DateTimeZone($timezone));
58 $cast = DateCaster::castDateTime($date, array('foo' => 'bar'), $stub, false, 0);
62 "\\x00~\\x00date" => $xDate
66 $this->assertDumpEquals($xDump, $cast);
69 Symfony\Component\VarDumper\Caster\ConstStub {
81 $this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]);
84 public function provideDateTimes()
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'),
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'),
101 * @dataProvider provideIntervals
103 public function testDumpInterval($intervalSpec, $ms, $invert, $expected)
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.');
109 $interval = $this->createInterval($intervalSpec, $ms, $invert);
117 $this->assertDumpMatchesFormat($xDump, $interval);
121 * @dataProvider provideIntervals
123 public function testDumpIntervalExcludingVerbosity($intervalSpec, $ms, $invert, $expected)
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.');
129 $interval = $this->createInterval($intervalSpec, $ms, $invert);
137 $this->assertDumpEquals($xDump, $interval, Caster::EXCLUDE_VERBOSE);
141 * @dataProvider provideIntervals
143 public function testCastInterval($intervalSpec, $ms, $invert, $xInterval, $xSeconds)
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.');
149 $interval = $this->createInterval($intervalSpec, $ms, $invert);
152 $cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
156 "\\x00~\\x00interval" => $xInterval
160 $this->assertDumpEquals($xDump, $cast);
162 if (null === $xSeconds) {
167 Symfony\Component\VarDumper\Caster\ConstStub {
179 $this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]);
182 public function provideIntervals()
184 $i = new \DateInterval('PT0S');
185 $ms = ($withMs = \PHP_VERSION_ID >= 70100 && isset($i->f)) ? '.0' : '';
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),
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),
219 * @dataProvider provideTimeZones
221 public function testDumpTimeZone($timezone, $expected)
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.');
227 $timezone = new \DateTimeZone($timezone);
235 $this->assertDumpMatchesFormat($xDump, $timezone);
239 * @dataProvider provideTimeZones
241 public function testDumpTimeZoneExcludingVerbosity($timezone, $expected)
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.');
247 $timezone = new \DateTimeZone($timezone);
255 $this->assertDumpMatchesFormat($xDump, $timezone, Caster::EXCLUDE_VERBOSE);
259 * @dataProvider provideTimeZones
261 public function testCastTimeZone($timezone, $xTimezone, $xRegion)
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.');
267 $timezone = new \DateTimeZone($timezone);
270 $cast = DateCaster::castTimeZone($timezone, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
274 "\\x00~\\x00timezone" => $xTimezone
278 $this->assertDumpMatchesFormat($xDump, $cast);
281 Symfony\Component\VarDumper\Caster\ConstStub {
293 $this->assertDumpMatchesFormat($xDump, $cast["\0~\0timezone"]);
296 public function provideTimeZones()
298 $xRegion = extension_loaded('intl') ? '%s' : '';
301 // type 1 (UTC offset)
302 array('-12:00', '-12:00', ''),
303 array('+00:00', '+00:00', ''),
304 array('+14:00', '+14:00', ''),
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', ''),
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),
324 * @dataProvider providePeriods
326 public function testDumpPeriod($start, $interval, $end, $options, $expected)
328 if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50620 || (\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70005)) {
329 $this->markTestSkipped();
332 $p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
340 $this->assertDumpMatchesFormat($xDump, $p);
344 * @dataProvider providePeriods
346 public function testCastPeriod($start, $interval, $end, $options, $xPeriod, $xDates)
348 if (defined('HHVM_VERSION_ID') || \PHP_VERSION_ID < 50620 || (\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70005)) {
349 $this->markTestSkipped();
352 $p = new \DatePeriod(new \DateTime($start), new \DateInterval($interval), is_int($end) ? $end : new \DateTime($end), $options);
355 $cast = DateCaster::castPeriod($p, array(), $stub, false, 0);
359 "\\x00~\\x00period" => $xPeriod
363 $this->assertDumpEquals($xDump, $cast);
366 Symfony\Component\VarDumper\Caster\ConstStub {
369 +value: "%A$xDates%A"
378 $this->assertDumpMatchesFormat($xDump, $cast["\0~\0period"]);
381 public function providePeriods()
383 $i = new \DateInterval('PT0S');
384 $ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
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'),
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'),
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'),
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'),
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'),
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'),
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'),
409 if (\PHP_VERSION_ID < 70107) {
410 array_walk($periods, function (&$i) { $i[5] = ''; });
416 private function createInterval($intervalSpec, $ms, $invert)
418 $interval = new \DateInterval($intervalSpec);
419 if (\PHP_VERSION_ID >= 70100 && isset($interval->f)) {
422 $interval->invert = $invert;