Skip to content
Permalink
Browse files

Fix failing tests for setDefaultOutputTimezone()

A few of the tests were wrong. They didn't take into account that the
time should change as a default output timezone was enabled.

I had to add a condition to prevent no-op timezone changes. While these
shouldn't mutate the underlying time value, they do.
  • Loading branch information...
markstory committed Jun 3, 2016
1 parent b3c2013 commit 62ae2ba9694d22e41512005c2b79bd3a5f8666fa
Showing with 23 additions and 19 deletions.
  1. +9 −9 src/I18n/DateFormatTrait.php
  2. +14 −10 tests/TestCase/I18n/TimeTest.php
@@ -206,17 +206,17 @@ public function i18nFormat($format = null, $timezone = null, $locale = null)
$time = $this;
if ($time instanceof Time || $time instanceof FrozenTime) {
// Remove before merge:
// if ((is_string($time->timezone) && $time->timezone === 'UTC')
// || ($time->timezone instanceof \DateTimeZone && $time->timezone->getName() === '+0:00')
// ) {
// }
// if ($time->getOffset() !== 0) {
// $time = clone $time;
// $time->add(new \DateInterval('PT' . $time->getOffset() / 60 . 'M'));
// }
$timezone = $timezone ?: static::getDefaultOutputTimezone();
}
// Detect and prevent null timezone transitions, as datefmt_create will
// doubly apply the TZ offset.
$currentTimezone = $time->getTimezone();
if ($timezone && (
(is_string($timezone) && $currentTimezone->getName() === $timezone) ||
($timezone instanceof DateTimeZone && $currentTimezone->getName() === $timezone->getName())
)) {
$timezone = null;
}
if ($timezone) {
// Handle the immutable and mutable object cases.
@@ -586,16 +586,19 @@ public function testI18nFormatWithDefaultOutputTimezone($class)
*/
public function testI18nFormatWithOffsetTimezone($class)
{
// Default output format is in UTC
$time = new $class('2014-01-01T00:00:00+00');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT';
$this->assertTimeFormat($expected, $result);
$class::setDefaultOutputTimezone('GMT+09:00');
$time = new $class('2014-01-01T00:00:00+09');

This comment has been minimized.

Copy link
@htstudios

htstudios Jun 4, 2016

Contributor

I was trying to make these tests pass without calling the global setter instead by taking the explicit offset from the ISO format, e.g +09.

Shouldn't that be the teat case?

This comment has been minimized.

Copy link
@markstory

markstory Jun 4, 2016

Author Member

But the default output format is currently UTC as of line 589. I don't think that using an offset timezone should exempt a datetime object from the defaultOutputTimezone.

$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT+09:00';
$this->assertTimeFormat($expected, $result);
$class::setDefaultOutputTimezone('GMT-01:30');
$time = new $class('2014-01-01T00:00:00-01:30');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT-01:30';
@@ -611,22 +614,23 @@ public function testI18nFormatWithOffsetTimezone($class)
*/
public function testI18nFormatWithOffsetTimezoneWithDefaultOutputTimezone($class)
{
// America/Vancouver is GMT-8 in the winter
$class::setDefaultOutputTimezone('America/Vancouver');
$time = new $class('2014-01-01T00:00:00+00');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT';
$this->assertTimeFormat($expected, $result);
$expected = 'Tuesday December 31 2013 4:00:00 PM Pacific Standard Time';
$this->assertTimeFormat($expected, $result, 'GMT to GMT-8 should be 8 hours');
$time = new $class('2014-01-01T00:00:00+09');
$time = new $class('2014-01-01T00:00:00+09:00');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT+09:00';
$this->assertTimeFormat($expected, $result);
$expected = 'Tuesday December 31 2013 7:00:00 AM Pacific Standard Time';
$this->assertTimeFormat($expected, $result, 'GMT+9 to GMT-8 should be 17hrs');
$time = new $class('2014-01-01T00:00:00-01:30');
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'Wednesday January 1 2014 12:00:00 AM GMT-01:30';
$this->assertTimeFormat($expected, $result);
$expected = 'Tuesday December 31 2013 5:30:00 PM Pacific Standard Time';
$this->assertTimeFormat($expected, $result, 'GMT-1:30 to GMT-8 is 6.5hrs');
}
/**
@@ -682,13 +686,13 @@ public function testListTimezones($class)
* @dataProvider classNameProvider
* @return void
*/
public function testToStringWithdefaultOutputTimezone($class)
public function testToStringWithDefaultOutputTimezone($class)
{
$class::setDefaultOutputTimezone('America/Vancouver');
$time = new $class('2014-04-20 22:10');
$time = new $class('2014-04-20 22:10 UTC');
$class::setDefaultLocale('fr-FR');
$class::setToStringFormat(\IntlDateFormatter::FULL);
$this->assertTimeFormat('dimanche 20 avril 2014 15:10:00 UTC', (string)$time);
$this->assertTimeFormat('dimanche 20 avril 2014 15:10:00 heure d’été du Pacifique', (string)$time);
}
/**

0 comments on commit 62ae2ba

Please sign in to comment.
You can’t perform that action at this time.