Skip to content

Commit

Permalink
bug #35702 [VarDumper] fixed DateCaster not displaying additional fie…
Browse files Browse the repository at this point in the history
…lds (Makdessi Alex)

This PR was merged into the 3.4 branch.

Discussion
----------

[VarDumper] fixed DateCaster not displaying additional fields

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #35699
| License       | MIT
| Doc PR        | ~

----

Here's the result with [Holiday](https://github.com/azuyalabs/yasumi/blob/develop/src/Yasumi/Holiday.php) class

| before  | after
| ------- | -----
| ![before](https://user-images.githubusercontent.com/4425529/74445818-48f03d00-4e77-11ea-97e1-58d88ac52cba.png) | ![after](https://user-images.githubusercontent.com/4425529/74445825-4c83c400-4e77-11ea-8e8e-1fbbb1040438.png)

Commits
-------

f965971 [VarDumper] fixed DateCaster not displaying additional fields
  • Loading branch information
nicolas-grekas committed Feb 15, 2020
2 parents 1a51d34 + f965971 commit b92168c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Symfony/Component/VarDumper/Caster/DateCaster.php
Expand Up @@ -31,7 +31,11 @@ public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub,
.($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');
Expand Down
37 changes: 36 additions & 1 deletion src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
Expand Up @@ -16,6 +16,7 @@
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>
Expand Down Expand Up @@ -55,7 +56,7 @@ public function testCastDateTime($time, $timezone, $xDate, $xTimestamp, $xInfos)

$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 [
Expand Down Expand Up @@ -97,6 +98,40 @@ public function provideDateTimes()
];
}

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
*/
Expand Down
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\VarDumper\Tests\Fixtures;

class DateTimeChild extends \DateTime
{
private $addedProperty = 'foo';
}

0 comments on commit b92168c

Please sign in to comment.