Skip to content

Commit d6bc7a4

Browse files
bogdanmozanicolas-grekas
authored andcommitted
[VarDumper] Dumping DateTime throws error if getTimezone is false
1 parent 124311a commit d6bc7a4

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/Symfony/Component/VarDumper/Caster/DateCaster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DateCaster
2727
public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, bool $isNested, int $filter)
2828
{
2929
$prefix = Caster::PREFIX_VIRTUAL;
30-
$location = $d->getTimezone()->getLocation();
30+
$location = $d->getTimezone() ? $d->getTimezone()->getLocation() : null;
3131
$fromNow = (new \DateTime())->diff($d);
3232

3333
$title = $d->format('l, F j, Y')

src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,52 @@ public static function provideDateTimes()
9090
];
9191
}
9292

93+
/**
94+
* @dataProvider provideNoTimezoneDateTimes
95+
*/
96+
public function testCastDateTimeNoTimezone($time, $xDate, $xInfos)
97+
{
98+
$stub = new Stub();
99+
$date = new NoTimezoneDate($time);
100+
$cast = DateCaster::castDateTime($date, Caster::castObject($date, \DateTime::class), $stub, false, 0);
101+
102+
$xDump = <<<EODUMP
103+
array:1 [
104+
"\\x00~\\x00date" => $xDate
105+
]
106+
EODUMP;
107+
108+
$this->assertDumpEquals($xDump, $cast);
109+
110+
$xDump = <<<EODUMP
111+
Symfony\Component\VarDumper\Caster\ConstStub {
112+
+type: 1
113+
+class: "$xDate"
114+
+value: "%A$xInfos%A"
115+
+cut: 0
116+
+handle: 0
117+
+refCount: 0
118+
+position: 0
119+
+attr: []
120+
}
121+
EODUMP;
122+
123+
$this->assertDumpMatchesFormat($xDump, $cast["\0~\0date"]);
124+
}
125+
126+
public static function provideNoTimezoneDateTimes()
127+
{
128+
return [
129+
['2017-04-30 00:00:00.000000', '2017-04-30 00:00:00.0 +00:00', 'Sunday, April 30, 2017'],
130+
['2017-04-30 00:00:00.100000', '2017-04-30 00:00:00.100 +00:00', 'Sunday, April 30, 2017'],
131+
['2017-04-30 00:00:00.120000', '2017-04-30 00:00:00.120 +00:00', 'Sunday, April 30, 2017'],
132+
['2017-04-30 00:00:00.123000', '2017-04-30 00:00:00.123 +00:00', 'Sunday, April 30, 2017'],
133+
['2017-04-30 00:00:00.123400', '2017-04-30 00:00:00.123400 +00:00', 'Sunday, April 30, 2017'],
134+
['2017-04-30 00:00:00.123450', '2017-04-30 00:00:00.123450 +00:00', 'Sunday, April 30, 2017'],
135+
['2017-04-30 00:00:00.123456', '2017-04-30 00:00:00.123456 +00:00', 'Sunday, April 30, 2017'],
136+
];
137+
}
138+
93139
public function testCastDateTimeWithAdditionalChildProperty()
94140
{
95141
$stub = new Stub();
@@ -407,3 +453,15 @@ private function createInterval($intervalSpec, $ms, $invert)
407453
return $interval;
408454
}
409455
}
456+
457+
class NoTimezoneDate extends \DateTime
458+
{
459+
/**
460+
* @return \DateTimeZone|false
461+
*/
462+
#[\ReturnTypeWillChange]
463+
public function getTimezone()
464+
{
465+
return false;
466+
}
467+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy