[VarDumper] fixed DateCaster not displaying additional fields

This commit is contained in:
Makdessi Alex 2020-02-13 15:34:42 +01:00
parent 9e0a39ee05
commit f965971919
3 changed files with 49 additions and 2 deletions

View File

@ -31,7 +31,11 @@ class DateCaster
.($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
;
$a = [];
unset(
$a[Caster::PREFIX_DYNAMIC.'date'],
$a[Caster::PREFIX_DYNAMIC.'timezone'],
$a[Caster::PREFIX_DYNAMIC.'timezone_type']
);
$a[$prefix.'date'] = new ConstStub(self::formatDateTime($d, $location ? ' e (P)' : ' P'), $title);
$stub->class .= $d->format(' @U');

View File

@ -16,6 +16,7 @@ use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Caster\DateCaster;
use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Symfony\Component\VarDumper\Tests\Fixtures\DateTimeChild;
/**
* @author Dany Maillard <danymaillard93b@gmail.com>
@ -55,7 +56,7 @@ EODUMP;
$stub = new Stub();
$date = new \DateTime($time, new \DateTimeZone($timezone));
$cast = DateCaster::castDateTime($date, ['foo' => 'bar'], $stub, false, 0);
$cast = DateCaster::castDateTime($date, Caster::castObject($date, \DateTime::class), $stub, false, 0);
$xDump = <<<EODUMP
array:1 [
@ -97,6 +98,40 @@ EODUMP;
];
}
public function testCastDateTimeWithAdditionalChildProperty()
{
$stub = new Stub();
$date = new DateTimeChild('2020-02-13 00:00:00.123456', new \DateTimeZone('Europe/Paris'));
$objectCast = Caster::castObject($date, DateTimeChild::class);
$dateCast = DateCaster::castDateTime($date, $objectCast, $stub, false, 0);
$xDate = '2020-02-13 00:00:00.123456 Europe/Paris (+01:00)';
$xInfo = 'Thursday, February 13, 2020%Afrom now';
$xDump = <<<EODUMP
array:2 [
"\\x00Symfony\Component\VarDumper\Tests\Fixtures\DateTimeChild\\x00addedProperty" => "foo"
"\\x00~\\x00date" => $xDate
]
EODUMP;
$this->assertDumpEquals($xDump, $dateCast);
$xDump = <<<EODUMP
Symfony\Component\VarDumper\Caster\ConstStub {
+type: 1
+class: "$xDate"
+value: "%A$xInfo%A"
+cut: 0
+handle: 0
+refCount: 0
+position: 0
+attr: []
}
EODUMP;
$this->assertDumpMatchesFormat($xDump, $dateCast["\0~\0date"]);
}
/**
* @dataProvider provideIntervals
*/

View File

@ -0,0 +1,8 @@
<?php
namespace Symfony\Component\VarDumper\Tests\Fixtures;
class DateTimeChild extends \DateTime
{
private $addedProperty = 'foo';
}