|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\DateCaster; |
| 16 | +use Symfony\Component\VarDumper\Cloner\Stub; |
| 17 | +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Dany Maillard <[email protected]> |
| 21 | + */ |
| 22 | +class DateCasterTest extends TestCase |
| 23 | +{ |
| 24 | + use VarDumperTestTrait; |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider provideDates |
| 28 | + */ |
| 29 | + public function testDumpDate($time, $timezone, $expected) |
| 30 | + { |
| 31 | + if ((defined('HHVM_VERSION_ID') || PHP_VERSION_ID <= 50509) && preg_match('/[-+]\d{2}:\d{2}/', $timezone)) { |
| 32 | + $this->markTestSkipped('DateTimeZone GMT offsets are supported since 5.5.10. See https://github.com/facebook/hhvm/issues/5875 for HHVM.'); |
| 33 | + } |
| 34 | + |
| 35 | + $date = new \DateTime($time, new \DateTimeZone($timezone)); |
| 36 | + |
| 37 | + $xDump = <<<EODUMP |
| 38 | +DateTime @1493503200 { |
| 39 | + date: $expected |
| 40 | +} |
| 41 | +EODUMP; |
| 42 | + |
| 43 | + $this->assertDumpMatchesFormat($xDump, $date); |
| 44 | + } |
| 45 | + |
| 46 | + public function testCastDate() |
| 47 | + { |
| 48 | + $stub = new Stub(); |
| 49 | + $date = new \DateTime('2017-08-30 00:00:00.000000', new \DateTimeZone('Europe/Zurich')); |
| 50 | + $cast = DateCaster::castDate($date, array('foo' => 'bar'), $stub, false, 0); |
| 51 | + |
| 52 | + $xDump = <<<'EODUMP' |
| 53 | +array:1 [ |
| 54 | + "\x00~\x00date" => 2017-08-30 00:00:00.000000 Europe/Zurich (+02:00) |
| 55 | +] |
| 56 | +EODUMP; |
| 57 | + |
| 58 | + $this->assertDumpMatchesFormat($xDump, $cast); |
| 59 | + |
| 60 | + $xDump = <<<'EODUMP' |
| 61 | +Symfony\Component\VarDumper\Caster\ConstStub { |
| 62 | + +type: "ref" |
| 63 | + +class: "2017-08-30 00:00:00.000000 Europe/Zurich (+02:00)" |
| 64 | + +value: """ |
| 65 | + Wednesday, August 30, 2017\n |
| 66 | + +%a from now\n |
| 67 | + DST On |
| 68 | + """ |
| 69 | + +cut: 0 |
| 70 | + +handle: 0 |
| 71 | + +refCount: 0 |
| 72 | + +position: 0 |
| 73 | + +attr: [] |
| 74 | +} |
| 75 | +EODUMP; |
| 76 | + |
| 77 | + $this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]); |
| 78 | + } |
| 79 | + |
| 80 | + public function provideDates() |
| 81 | + { |
| 82 | + return array( |
| 83 | + array('2017-04-30 00:00:00.000000', 'Europe/Zurich', '2017-04-30 00:00:00.000000 Europe/Zurich (+02:00)'), |
| 84 | + array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'), |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments