From a3cac2b4c94f0dd2b5ebfbbe88cae7640c4a7d4c Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Tue, 30 Apr 2019 09:25:43 +0200 Subject: [PATCH] [Intl] Extra timezone tests --- .../Component/Intl/Tests/TimezonesTest.php | 74 +++++++++++++++++++ src/Symfony/Component/Intl/Timezones.php | 10 ++- 2 files changed, 83 insertions(+), 1 deletion(-) 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy