8000 [VarDumper] Fix interval caster with microseconds by maidmaid · Pull Request #24055 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper] Fix interval caster with microseconds #24055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Caster/DateCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static function formatInterval(\DateInterval $i)
}

if (\PHP_VERSION_ID >= 70100 && isset($i->f)) {
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, $i->f) : '';
$format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:'.self::formatSeconds($i->s, substr($i->f, 2)) : '';
} else {
$format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
}
Expand Down
81 changes: 46 additions & 35 deletions src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ public function provideDateTimes()
/**
* @dataProvider provideIntervals
*/
public function testDumpInterval($intervalSpec, $invert, $expected)
public function testDumpInterval($intervalSpec, $ms, $invert, $expected)
{
$interval = new \DateInterval($intervalSpec);
$interval->invert = $invert;
$interval = $this->createInterval($intervalSpec, $ms, $invert);

$xDump = <<<EODUMP
DateInterval {
Expand All @@ -117,10 +116,9 @@ public function testDumpInterval($intervalSpec, $invert, $expected)
/**
* @dataProvider provideIntervals
*/
public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expected)
public function testDumpIntervalExcludingVerbosity($intervalSpec, $ms, $invert, $expected)
{
$interval = new \DateInterval($intervalSpec);
$interval->invert = $invert;
$interval = $this->createInterval($intervalSpec, $ms, $invert);

$xDump = <<<EODUMP
DateInterval {
Expand All @@ -134,10 +132,9 @@ public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expe
/**
* @dataProvider provideIntervals
*/
public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
public function testCastInterval($intervalSpec, $ms, $invert, $xInterval, $xSeconds)
{
$interval = new \DateInterval($intervalSpec);
$interval->invert = $invert;
$interval = $this->createInterval($intervalSpec, $ms, $invert);
$stub = new Stub();

$cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE);
Expand Down Expand Up @@ -173,34 +170,37 @@ public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds)
public function provideIntervals()
{
$i = new \DateInterval('PT0S');
$ms = \PHP_VERSION_ID >= 70100 && isset($i->f) ? '.0' : '';
$ms = ($withMs = \PHP_VERSION_ID >= 70100 && isset($i->f)) ? '.0' : '';
$ms1 = $withMs ? '.100' : '';

return array(
array('PT0S', 0, '0s', '0s'),
array('PT1S', 0, '+ 00:00:01'.$ms, '1s'),
array('PT2M', 0, '+ 00:02:00'.$ms, '120s'),
array('PT3H', 0, '+ 03:00:00'.$ms, '10 800s'),
array('P4D', 0, '+ 4d', '345 600s'),
array('P5M', 0, '+ 5m', null),
array('P6Y', 0, '+ 6y', null),
array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 0, '+ 00:02:00'.$ms, null),
array('PT1H60M', 0, '+ 02:00:00'.$ms, null),
array('P1DT24H', 0, '+ 2d', null),
array('P1M32D', 0, '+ 1m 32d', null),

array('PT0S', 1, '0s', '0s'),
array('PT1S', 1, '- 00:00:01'.$ms, '-1s'),
array('PT2M', 1, '- 00:02:00'.$ms, '-120s'),
array('PT3H', 1, '- 03:00:00'.$ms, '-10 800s'),
array('P4D', 1, '- 4d', '-345 600s'),
array('P5M', 1, '- 5m', null),
array('P6Y', 1, '- 6y', null),
array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 1, '- 00:02:00'.$ms, null),
array('PT1H60M', 1, '- 02:00:00'.$ms, null),
array('P1DT24H', 1, '- 2d', null),
array('P1M32D', 1, '- 1m 32d', null),
array('PT0S', 0, 0, '0s', '0s'),
array('PT0S', 0.1, 0, '+ 00:00:00'.$ms1, '0s'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's here the added test.

< 8000 !-- '"` -->

array('PT1S', 0, 0, '+ 00:00:01'.$ms, '1s'),
array('PT2M', 0, 0, '+ 00:02:00'.$ms, '120s'),
array('PT3H', 0, 0, '+ 03:00:00'.$ms, '10 800s'),
array('P4D', 0, 0, '+ 4d', '345 600s'),
array('P5M', 0, 0, '+ 5m', null),
array('P6Y', 0, 0, '+ 6y', null),
array('P1Y2M3DT4H5M6S', 0, 0, '+ 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 0, 0, '+ 00:02:00'.$ms, null),
array('PT1H60M', 0, 0, '+ 02:00:00'.$ms, null),
array('P1DT24H', 0, 0, '+ 2d', null),
array('P1M32D', 0, 0, '+ 1m 32d', null),

array('PT0S', 0, 1, '0s', '0s'),
array('PT0S', 0.1, 1, '- 00:00:00'.$ms1, '0s'),
array('PT1S', 0, 1, '- 00:00:01'.$ms, '-1s'),
array('PT2M', 0, 1, '- 00:02:00'.$ms, '-120s'),
array('PT3H', 0, 1, '- 03:00:00'.$ms, '-10 800s'),
array('P4D', 0, 1, '- 4d', '-345 600s'),
array('P5M', 0, 1, '- 5m', null),
array('P6Y', 0, 1, '- 6y', null),
array('P1Y2M3DT4H5M6S', 0, 1, '- 1y 2m 3d 04:05:06'.$ms, null),
array('PT1M60S', 0, 1, '- 00:02:00'.$ms, null),
array('PT1H60M', 0, 1, '- 02:00:00'.$ms, null),
array('P1DT24H', 0, 1, '- 2d', null),
array('P1M32D', 0, 1, '- 1m 32d', null),
);
}

Expand Down Expand Up @@ -401,4 +401,15 @@ public function providePeriods()

return $periods;
}

private function createInterval($intervalSpec, $ms, $invert)
{
$interval = new \DateInterval($intervalSpec);
if (\PHP_VERSION_ID >= 70100 && isset($interval->f)) {
$interval->f = $ms;
}
$interval->invert = $invert;

return $interval;
}
}
0