Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f965971

Browse files
author
Makdessi Alex
committed
[VarDumper] fixed DateCaster not displaying additional fields
1 parent 9e0a39e commit f965971

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
3131
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
3232
;
3333

34-
$a = [];
34+
unset(
35+
$a[Caster::PREFIX_DYNAMIC.'date'],
36+
$a[Caster::PREFIX_DYNAMIC.'timezone'],
37+
$a[Caster::PREFIX_DYNAMIC.'timezone_type']
38+
);
3539
$a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);
3640

3741
$stub->class .= $d->format(' @U');

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\VarDumper\Caster\DateCaster;
1717
use Symfony\Component\VarDumper\Cloner\Stub;
1818
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
19+
use Symfony\Component\VarDumper\Tests\Fixtures\DateTimeChild;
1920

2021
/**
2122
* @author Dany Maillard <[email protected]>
@@ -55,7 +56,7 @@ public function testCastDateTime($time, $timezone, $xDate, $xTimestamp, $xInfos)
5556

5657
$stub = new Stub();
5758
$date = new \DateTime($time, new \DateTimeZone($timezone));
58-
$cast = DateCaster::castDateTime($date, ['foo' => 'bar'], $stub, false, 0);
59+
$cast = DateCaster::castDateTime($date, Caster::castObject($date, \DateTime::class), $stub, false, 0);
5960

6061
$xDump = <<<EODUMP
6162
array:1 [
@@ -97,6 +98,40 @@ public function provideDateTimes()
9798
];
9899
}
99100

101+
public function testCastDateTimeWithAdditionalChildProperty()
102+
{
103+
$stub = new Stub();
104+
$date = new DateTimeChild('2020-02-13 00:00:00.123456', new \DateTimeZone('Europe/Paris'));
105+
$objectCast = Caster::castObject($date, DateTimeChild::class);
106+
$dateCast = DateCaster::castDateTime($date, $objectCast, $stub, false, 0);
107+
108+
$xDate = '2020-02-13 00:00:00.123456 Europe/Paris (+01:00)';
109+
$xInfo = 'Thursday, February 13, 2020%Afrom now';
110+
$xDump = <<<EODUMP
111+
array:2 [
112+
"\\x00Symfony\Component\VarDumper\Tests\Fixtures\DateTimeChild\\x00addedProperty" => "foo"
113+
"\\x00~\\x00date" => $xDate
114+
]
115+
EODUMP;
116+
117+
$this->assertDumpEquals($xDump, $dateCast);
118+
119+
$xDump = <<<EODUMP
120+
Symfony\Component\VarDumper\Caster\ConstStub {
121+
+type: 1
122+
+class: "$xDate"
123+
+value: "%A$xInfo%A"
124+
+cut: 0
125+
+handle: 0
126+
+refCount: 0
127+
+position: 0
128+
+attr: []
129+
}
130+
EODUMP;
131+
132+
$this->assertDumpMatchesFormat($xDump, $dateCast["\0~\0date"]);
133+
}
134+
100135
/**
101136
* @dataProvider provideIntervals
102137
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\VarDumper\Tests\Fixtures;
4+
5+
class DateTimeChild extends \DateTime
6+
{
7+
private $addedProperty = 'foo';
8+
}

0 commit comments

Comments
 (0)