diff --git a/src/Symfony/Component/Intl/Tests/TimezonesTest.php b/src/Symfony/Component/Intl/Tests/TimezonesTest.php index 4152c6d99c738..61f4e6765a481 100644 --- a/src/Symfony/Component/Intl/Tests/TimezonesTest.php +++ b/src/Symfony/Component/Intl/Tests/TimezonesTest.php @@ -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; /** @@ -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() { @@ -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)); + $this->assertInternalType('string', Timezones::getGmtOffset($timezone)); + } + + /** + * @dataProvider provideTimezones + */ + public function testGetCountryCodeAvailability(string $timezone) + { + try { + $this->assertInternalType('string', Timezones::getCountryCode($timezone)); + } catch (MissingResourceException $e) { + 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()); + } } diff --git a/src/Symfony/Component/Intl/Timezones.php b/src/Symfony/Component/Intl/Timezones.php index 89445212c7443..94b8b27e301f7 100644 --- a/src/Symfony/Component/Intl/Timezones.php +++ b/src/Symfony/Component/Intl/Timezones.php @@ -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