From 90d6dc379149e1a8d924f97be8c236b9e3bd2de5 Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Tue, 31 Jul 2012 01:50:20 -0300 Subject: [PATCH] [Locale] fixed tests --- ...teTimeToLocalizedStringTransformerTest.php | 8 ++- .../Stub/DateFormat/FullTransformer.php | 6 +- .../Stub/DateFormat/TimeZoneTransformer.php | 5 +- .../Component/Locale/Stub/StubLocale.php | 19 +++++- .../Tests/Stub/StubIntlDateFormatterTest.php | 65 ++++++++++++------- .../Locale/Tests/Stub/StubLocaleTest.php | 8 ++- .../Tests/Stub/StubNumberFormatterTest.php | 8 +-- 7 files changed, 86 insertions(+), 33 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php index 9a5421056f..5fb0068f80 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php @@ -89,7 +89,9 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase { $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter::LONG); - $this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime)); + $expected = $this->isLowerThanIcuVersion('4.8') ? '03.02.2010 04:05:06 GMT+00:00' : '03.02.2010 04:05:06 GMT'; + + $this->assertEquals($expected, $transformer->transform($this->dateTime)); } public function testTransformFullTime() @@ -100,7 +102,9 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter::FULL); - $this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime)); + $expected = $this->isLowerThanIcuVersion('4.8') ? '03.02.2010 04:05:06 GMT+00:00' : '03.02.2010 04:05:06 GMT'; + + $this->assertEquals($expected, $transformer->transform($this->dateTime)); } public function testTransformToDifferentLocale() diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php index 6c883c99c4..b8eb00138a 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php +++ b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php @@ -175,7 +175,11 @@ class FullTransformer { $that = $this; - $escapedPattern = preg_quote($pattern, '/'); + // $escapedPattern = preg_quote($pattern, '/'); + + // ICU 4.8 recognizes slash ("/") in a value to be parsed as a dash ("-") when parsing a value that + // TODO: how to escape the regex metachars and still recognize "/" as "-" and vice-versa? + $escapedPattern = preg_replace('/\-|\//', '[\/\-]', $pattern); $reverseMatchingRegExp = preg_replace_callback($this->regExp, function($matches) use ($that) { $length = strlen($matches[0]); diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php index e9cf0ebef9..ad7b78825e 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php +++ b/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php @@ -33,7 +33,10 @@ class TimeZoneTransformer extends Transformer throw new NotImplementedException('Time zone different than GMT or UTC is not supported as a formatting output.'); } - return $dateTime->format('\G\M\TP'); + // From ICU >= 4.8, the zero offset is not more used, example: GMT instead of GMT+00:00 + $format = (0 !== (int) $dateTime->format('O')) ? '\G\M\TP' : '\G\M\T'; + + return $dateTime->format($format); } /** diff --git a/src/Symfony/Component/Locale/Stub/StubLocale.php b/src/Symfony/Component/Locale/Stub/StubLocale.php index eec8918ff8..71712cc0e0 100644 --- a/src/Symfony/Component/Locale/Stub/StubLocale.php +++ b/src/Symfony/Component/Locale/Stub/StubLocale.php @@ -468,6 +468,23 @@ class StubLocale throw new MethodNotImplementedException(__METHOD__); } + public static function getDataDirectory() + { + static $dataDirectory; + + if (null === $dataDirectory) { + $dataDirectory = 'current'; + + if (getenv('ICU_DATA_VERSION')) { + $dataDirectory = getenv('ICU_DATA_VERSION'); + } elseif (file_exists(__DIR__.'/../Resources/data/data-version.php')) { + $dataDirectory = include __DIR__.'/../Resources/data/data-version.php'; + } + } + + return __DIR__.'/../Resources/data/'.$dataDirectory; + } + /** * Returns the stub ICU data * @@ -488,7 +505,7 @@ class StubLocale } if (empty(self::${$cacheVariable})) { - self::${$cacheVariable} = include __DIR__.'/../Resources/data/'.$dataDirectory.'/stub/'.$stubDataDir.'/en.php'; + self::${$cacheVariable} = include $dataDirectory.'/stub/'.$stubDataDir.'/en.php'; } return self::${$cacheVariable}; diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php index fa902a133b..fb9835694b 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php @@ -121,10 +121,8 @@ class StubIntlDateFormatterTest extends LocaleTestCase $formatData = array( /* general */ array('y-M-d', 0, '1970-1-1'), - array("yyyy.MM.dd 'at' HH:mm:ss zzz", 0, '1970.01.01 at 00:00:00 GMT+00:00'), array("EEE, MMM d, ''yy", 0, "Thu, Jan 1, '70"), array('h:mm a', 0, '12:00 AM'), - array('K:mm a, z', 0, '0:00 AM, GMT+00:00'), array('yyyyy.MMMM.dd hh:mm aaa', 0, '01970.January.01 12:00 AM'), /* escaping */ @@ -285,26 +283,36 @@ class StubIntlDateFormatterTest extends LocaleTestCase array('s', 3601, '1'), array('s', 3630, '30'), array('s', 43200, '0'), // 12 hours - - /* timezone */ - array('z', 0, 'GMT+00:00'), - array('zz', 0, 'GMT+00:00'), - array('zzz', 0, 'GMT+00:00'), - array('zzzz', 0, 'GMT+00:00'), - array('zzzzz', 0, 'GMT+00:00'), ); + // Timezone + if ($this->isIntlExtensionLoaded() && $this->isGreaterOrEqualThanIcuVersion('4.8')) { + // general + $formatData[] = array("yyyy.MM.dd 'at' HH:mm:ss zzz", 0, '1970.01.01 at 00:00:00 GMT'); + $formatData[] = array('K:mm a, z', 0, '0:00 AM, GMT'); + + // timezone + $formatData[] = array('z', 0, 'GMT'); + $formatData[] = array('zz', 0, 'GMT'); + $formatData[] = array('zzz', 0, 'GMT'); + $formatData[] = array('zzzz', 0, 'GMT'); + $formatData[] = array('zzzzz', 0, 'GMT'); + } + // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) { $dateTime = new \DateTime('@0'); /* general, DateTime */ $formatData[] = array('y-M-d', $dateTime, '1970-1-1'); - $formatData[] = array("yyyy.MM.dd 'at' HH:mm:ss zzz", $dateTime, '1970.01.01 at 00:00:00 GMT+00:00'); $formatData[] = array("EEE, MMM d, ''yy", $dateTime, "Thu, Jan 1, '70"); $formatData[] = array('h:mm a', $dateTime, '12:00 AM'); - $formatData[] = array('K:mm a, z', $dateTime, '0:00 AM, GMT+00:00'); $formatData[] = array('yyyyy.MMMM.dd hh:mm aaa', $dateTime, '01970.January.01 12:00 AM'); + + if ($this->isIntlExtensionLoaded() && $this->isGreaterOrEqualThanIcuVersion('4.8')) { + $formatData[] = array("yyyy.MM.dd 'at' HH:mm:ss zzz", $dateTime, '1970.01.01 at 00:00:00 GMT'); + $formatData[] = array('K:mm a, z', $dateTime, '0:00 AM, GMT'); + } } return $formatData; @@ -428,7 +436,9 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->skipIfIntlExtensionIsNotLoaded(); $formatter = $this->createIntlFormatter('zzzz'); $formatter->setTimeZoneId('Pacific/Fiji'); - $this->assertEquals('Fiji Time', $formatter->format(0)); + + $expected = $this->isGreaterOrEqualThanIcuVersion('49') ? 'Fiji Standard Time' : 'Fiji Time'; + $this->assertEquals($expected, $formatter->format(0)); } public function testFormatWithGmtTimezoneStub() @@ -559,17 +569,22 @@ class StubIntlDateFormatterTest extends LocaleTestCase public function dateAndTimeTypeProvider() { - return array( + $data = array( array(0, StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'Thursday, January 1, 1970'), array(0, StubIntlDateFormatter::LONG, StubIntlDateFormatter::NONE, 'January 1, 1970'), array(0, StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::NONE, 'Jan 1, 1970'), array(0, StubIntlDateFormatter::SHORT, StubIntlDateFormatter::NONE, '1/1/70'), - - array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT+00:00'), - array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT+00:00'), - array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'), - array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'), ); + + if ($this->isIntlExtensionLoaded() && $this->isGreaterOrEqualThanIcuVersion('4.8')) { + $data[] = array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT'); + $data[] = array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT'); + } + + $data[] = array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'); + $data[] = array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'); + + return $data; } public function testGetCalendar() @@ -668,7 +683,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase public function parseProvider() { - return array( + $data = array( // years array('y-M-d', '1970-1-1', 0), array('yy-M-d', '70-1-1', 0), @@ -807,6 +822,15 @@ class StubIntlDateFormatterTest extends LocaleTestCase array("''y", "'1970", 0), array("H 'o'' clock'", "0 o' clock", 0), ); + + if ($this->isIntlExtensionLoaded() && $this->isGreaterOrEqualThanIcuVersion('4.8')) { + $data[] = array('y-M-d', '1970/1/1', 0); + $data[] = array('yy-M-d', '70/1/1', 0); + $data[] = array('y/M/d', '1970-1-1', 0); + $data[] = array('yy/M/d', '70-1-1', 0); + } + + return $data; } /** @@ -846,9 +870,6 @@ class StubIntlDateFormatterTest extends LocaleTestCase public function parseErrorProvider() { return array( - array('y-M-d', '1970/1/1'), - array('yy-M-d', '70/1/1'), - // 1 char month array('y-MMMMM-d', '1970-J-1'), array('y-MMMMM-d', '1970-S-1'), diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubLocaleTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubLocaleTest.php index 0531042732..568ee1ead3 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubLocaleTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubLocaleTest.php @@ -11,9 +11,11 @@ namespace Symfony\Component\Locale\Tests\Stub; +use Symfony\Component\Locale\Locale; use Symfony\Component\Locale\Stub\StubLocale; +use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase; -class StubLocaleTest extends \PHPUnit_Framework_TestCase +class StubLocaleTest extends LocaleTestCase { /** * @expectedException InvalidArgumentException @@ -65,8 +67,10 @@ class StubLocaleTest extends \PHPUnit_Framework_TestCase public function testGetCurrenciesData() { + $symbol = $this->isSameAsIcuVersion('4.8') ? 'BR$' : 'R$'; + $currencies = StubLocale::getCurrenciesData('en'); - $this->assertEquals('R$', $currencies['BRL']['symbol']); + $this->assertEquals($symbol, $currencies['BRL']['symbol']); $this->assertEquals('Brazilian Real', $currencies['BRL']['name']); $this->assertEquals(2, $currencies['BRL']['fractionDigits']); $this->assertEquals(0, $currencies['BRL']['roundingIncrement']); diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php index af3967f149..4286e9d744 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php @@ -161,7 +161,7 @@ class StubNumberFormatterTest extends LocaleTestCase public function testFormatCurrencyWithCurrencyStyleCostaRicanColonsRoundingStub($value, $currency, $symbol, $expected) { $formatter = $this->getStubFormatterWithCurrencyStyle(); - $this->assertEquals(sprintf($expected, '₡'), $formatter->formatCurrency($value, $currency)); + $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency)); } /** @@ -177,7 +177,7 @@ class StubNumberFormatterTest extends LocaleTestCase public function formatCurrencyWithCurrencyStyleCostaRicanColonsRoundingProvider() { - $crc = $this->isIntlExtensionLoaded() && $this->isSameAsIcuVersion('4.8') ? 'CRC' : '₡'; + $crc = $this->isIntlExtensionLoaded() && $this->isGreaterOrEqualThanIcuVersion('4.8') ? 'CRC' : '₡'; return array( array(100, 'CRC', $crc, '%s100'), @@ -192,7 +192,7 @@ class StubNumberFormatterTest extends LocaleTestCase public function testFormatCurrencyWithCurrencyStyleBrazilianRealRoundingStub($value, $currency, $symbol, $expected) { $formatter = $this->getStubFormatterWithCurrencyStyle(); - $this->assertEquals(sprintf($expected, 'R'), $formatter->formatCurrency($value, $currency)); + $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency)); } /** @@ -232,7 +232,7 @@ class StubNumberFormatterTest extends LocaleTestCase public function testFormatCurrencyWithCurrencyStyleSwissRoundingStub($value, $currency, $symbol, $expected) { $formatter = $this->getStubFormatterWithCurrencyStyle(); - $this->assertEquals(sprintf($expected, 'CHF'), $formatter->formatCurrency($value, $currency)); + $this->assertEquals(sprintf($expected, $symbol), $formatter->formatCurrency($value, $currency)); } /**