Skip to content

Commit 46cda10

Browse files
bug #50644 [VarDumper] Dumping DateTime throws error if getTimezone is false (bogdanmoza)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [VarDumper] Dumping DateTime throws error if getTimezone is false | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | ---- **TL;DR:** In case you have a DateTime object that returns false when calling `getTimezone()`, an error is thrown in DateCaster. **Description** So I have encountered this case when I have tried to dump a mocked DateTime object. The mock will not have a timezone making the `getTimezone()` method return false. I know that this is not really a bug, as a DateTime object not having a DateTimeZone should not be happening, but it is possible in case someone extends the DateTime object and overrides the `getTimezone()` method. The error thrown is 'The DatetimeInterface object has not been correctly initialized by its constructor' **How to reproduce** ``` class A extends \DateTime { public function getTimezone(): \DateTimeZone|false { return false; } } ``` Dump the A class will cause an error. **Final notes** Even though this a special case, I still think that you should be able to dump a mocked DateTime. I hope the description and everything is ok as this is my first pr here. Cheers! Commits ------- d6bc7a4 [VarDumper] Dumping DateTime throws error if getTimezone is false
2 parents 2d2cbad + d6bc7a4 commit 46cda10

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