Skip to content
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

[Intl] Extra timezone tests #31325

Merged
merged 1 commit into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Symfony/Component/Intl/Tests/TimezonesTest.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Intl\Tests;

use Symfony\Component\Intl\Exception\MissingResourceException;
use Symfony\Component\Intl\Regions;
use Symfony\Component\Intl\Timezones;

/**
Expand Down Expand Up @@ -456,6 +458,15 @@ class TimezonesTest extends ResourceBundleTestCase
'Pacific/Wake',
'Pacific/Wallis',
];
private static $zonesNoCountry = [
'Antarctica/Troll',
'CST6CDT',
'EST5EDT',
'MST7MDT',
'PST8PDT',
'Etc/GMT',
'Etc/UTC',
];

public function testGetTimezones()
{
Expand Down Expand Up @@ -562,4 +573,67 @@ public function testForCountryCode()
$this->assertSame(['Europe/Amsterdam'], Timezones::forCountryCode('NL'));
$this->assertSame(['Europe/Berlin', 'Europe/Busingen'], Timezones::forCountryCode('DE'));
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
*/
public function testForCountryCodeWithUnknownCountry()
{
Timezones::forCountryCode('foobar');
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
*/
public function testGetCountryCodeWithUnknownTimezone()
{
Timezones::getCountryCode('foobar');
}

/**
* @dataProvider provideTimezones
*/
public function testGetGmtOffsetAvailability(string $timezone)
{
$this->assertInternalType('int', Timezones::getRawOffset($timezone));
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we ensure that with return types instead ?

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 does :D im on it.

$this->assertInternalType('string', Timezones::getGmtOffset($timezone));
}

/**
* @dataProvider provideTimezones
*/
public function testGetCountryCodeAvailability(string $timezone)
{
try {
$this->assertInternalType('string', Timezones::getCountryCode($timezone));
} catch (MissingResourceException $e) {
Copy link
Member

Choose a reason for hiding this comment

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

if it is possible for valid timezone ids to throw a MissingResourceException when trying to get their code, we need to document that in the interface, as it becomes necessary to catch it when using it (or maybe the Timezones class should handle that internally)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see #31332

if (\in_array($timezone, self::$zonesNoCountry, true)) {
$this->markTestSkipped();
} else {
$this->fail();
}
}
}

public function provideTimezones(): iterable
{
return array_map(function ($timezone) {
return [$timezone];
}, self::$zones);
}

/**
* @dataProvider provideCountries
*/
public function testForCountryCodeAvailability(string $country)
{
$this->assertInternalType('array', Timezones::forCountryCode($country));
}

public function provideCountries(): iterable
{
return array_map(function ($country) {
return [$country];
}, Regions::getRegionCodes());
}
}
10 changes: 9 additions & 1 deletion src/Symfony/Component/Intl/Timezones.php
Expand Up @@ -83,7 +83,15 @@ public static function getCountryCode(string $timezone): string

public static function forCountryCode(string $country): array
{
return self::readEntry(['CountryToZone', $country], 'meta');
try {
return self::readEntry(['CountryToZone', $country], 'meta');
} catch (MissingResourceException $e) {
if (Regions::exists($country)) {
return [];
}

throw $e;
}
}

protected static function getPath(): string
Expand Down