From 312a5a4201a7b0a192c0b47cc39145b5e6dfe071 Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 14:56:41 -0300 Subject: [PATCH 1/7] [Locale] fixed StubIntlDateFormatter::format() to set the right error for PHP >= 5.3.4 and to behave like the intl when formatting successfully --- .../Locale/Stub/StubIntlDateFormatter.php | 17 ++++++++++- .../Locale/Stub/StubIntlDateFormatterTest.php | 29 ++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php index 5b68291260..5f0dfee698 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php @@ -179,8 +179,17 @@ class StubIntlDateFormatter } // behave like the intl extension + $argumentError = null; if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) { - StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, 'datefmt_format: takes either an array or an integer timestamp value '); + $argumentError = 'datefmt_format: takes either an array or an integer timestamp value '; + } elseif (!is_int($timestamp) && !$timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) { + $argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object'; + } + + if (null !== $argumentError) { + StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, $argumentError); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); return false; } @@ -193,6 +202,11 @@ class StubIntlDateFormatter $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); $formatted = $transformer->format($this->createDateTime($timestamp)); + // behave like the intl extension + StubIntl::setError(StubIntl::U_ZERO_ERROR); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); + return $formatted; } @@ -359,6 +373,7 @@ class StubIntlDateFormatter $timestamp = $transformer->parse($dateTime, $value); + // behave like the intl extension. FullTransformer::parse() set the proper error if (false === $timestamp) { $this->errorCode = StubIntl::getErrorCode(); $this->errorMessage = StubIntl::getErrorMessage(); diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php index f681e3260e..636b20fa83 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php @@ -82,6 +82,21 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); } + /** + * @dataProvider formatErrorProvider + */ + public function testFormatErrorStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + { + $formatter = $this->createStubFormatter($pattern); + $this->assertSame($expected, $formatter->format($timestamp)); + $this->assertSame($errorMessage, StubIntl::getErrorMessage()); + $this->assertSame($errorCode, StubIntl::getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + } + /** * @dataProvider formatErrorProvider */ @@ -90,10 +105,6 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfICUVersionIsTooOld(); - if (version_compare(PHP_VERSION, '5.3.3') > 0) { - $this->markTestSkipped('The intl error messages were change in PHP 5.3.3.'); - } - $formatter = $this->createIntlFormatter($pattern); $this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($errorMessage, intl_get_error_message()); @@ -297,11 +308,15 @@ class StubIntlDateFormatterTest extends LocaleTestCase public function formatErrorProvider() { - /* errors */ + $message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'; + + if (version_compare(\PHP_VERSION, '5.3.4', '>=')) { + $message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR'; + } return array( - array('y-M-d', '0', false, 1, 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'), - array('y-M-d', 'foobar', false, 1, 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'), + array('y-M-d', '0', false, 1, $message), + array('y-M-d', 'foobar', false, 1, $message), ); } From e4769d937766ef5d2b23e6c11e8368d3cea38bcb Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 14:57:23 -0300 Subject: [PATCH 2/7] [Locale] reordered test methods --- .../Locale/Stub/StubIntlDateFormatterTest.php | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php index 636b20fa83..6e9eec3345 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php @@ -82,36 +82,6 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); } - /** - * @dataProvider formatErrorProvider - */ - public function testFormatErrorStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') - { - $formatter = $this->createStubFormatter($pattern); - $this->assertSame($expected, $formatter->format($timestamp)); - $this->assertSame($errorMessage, StubIntl::getErrorMessage()); - $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); - $this->assertSame($errorMessage, $formatter->getErrorMessage()); - $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); - } - - /** - * @dataProvider formatErrorProvider - */ - public function testFormatErrorIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') - { - $this->skipIfIntlExtensionIsNotLoaded(); - $this->skipIfICUVersionIsTooOld(); - - $formatter = $this->createIntlFormatter($pattern); - $this->assertSame($expected, $formatter->format($timestamp)); - $this->assertSame($errorMessage, intl_get_error_message()); - $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); - } - public function formatProvider() { $formatData = array( @@ -306,6 +276,36 @@ class StubIntlDateFormatterTest extends LocaleTestCase return $formatData; } + /** + * @dataProvider formatErrorProvider + */ + public function testFormatErrorStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + { + $formatter = $this->createStubFormatter($pattern); + $this->assertSame($expected, $formatter->format($timestamp)); + $this->assertSame($errorMessage, StubIntl::getErrorMessage()); + $this->assertSame($errorCode, StubIntl::getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + } + + /** + * @dataProvider formatErrorProvider + */ + public function testFormatErrorIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + { + $this->skipIfIntlExtensionIsNotLoaded(); + $this->skipIfICUVersionIsTooOld(); + + $formatter = $this->createIntlFormatter($pattern); + $this->assertSame($expected, $formatter->format($timestamp)); + $this->assertSame($errorMessage, intl_get_error_message()); + $this->assertSame($errorCode, intl_get_error_code()); + $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + } + public function formatErrorProvider() { $message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'; From 0a606642b77573e493c808c12c2d2568166bb510 Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 15:32:40 -0300 Subject: [PATCH 3/7] [Locale] updated StubIntlDateFormatter::format() exception message when timestamp argument is an array for PHP >= 5.3.4 --- .../Locale/Stub/StubIntlDateFormatter.php | 12 ++++--- .../Locale/Stub/StubIntlDateFormatterTest.php | 33 +++++++++++++++++-- .../Tests/Component/Locale/TestCase.php | 5 +++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php index 5f0dfee698..ec339043a1 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php @@ -175,14 +175,18 @@ class StubIntlDateFormatter { // intl allows timestamps to be passed as arrays - we don't if (is_array($timestamp)) { - throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, 'Only integer unix timestamps are supported'); + $message = version_compare(\PHP_VERSION, '5.3.4', '>=') ? + 'Only integer unix timestamps and DateTime objects are supported' : + 'Only integer unix timestamps are supported'; + + throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message); } // behave like the intl extension $argumentError = null; - if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) { + if (version_compare(\PHP_VERSION, '5.3.4', '<') && !is_int($timestamp)) { $argumentError = 'datefmt_format: takes either an array or an integer timestamp value '; - } elseif (!is_int($timestamp) && !$timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) { + } elseif (version_compare(\PHP_VERSION, '5.3.4', '>=') && !is_int($timestamp) && !$timestamp instanceOf \DateTime) { $argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object'; } @@ -195,7 +199,7 @@ class StubIntlDateFormatter } // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances - if ($timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) { + if (version_compare(\PHP_VERSION, '5.3.4', '>=') && $timestamp instanceOf \DateTime) { $timestamp = $timestamp->getTimestamp(); } diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php index 6e9eec3345..4317a18830 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php @@ -53,6 +53,35 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertNull($formatter->getTimeZoneId()); } + public function testFormatWithUnsupportedTimestampArgument() + { + $formatter = $this->createStubFormatter(); + + $localtime = array( + 'tm_sec' => 59, + 'tm_min' => 3, + 'tm_hour' => 15, + 'tm_mday' => 15, + 'tm_mon' => 3, + 'tm_year' => 112, + 'tm_wday' => 0, + 'tm_yday' => 105, + 'tm_isdst' => 0 + ); + + try { + $formatter->format($localtime); + } catch (\Exception $e) { + $this->assertInstanceOf('Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException', $e); + + if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) { + $this->assertStringEndsWith('Only integer unix timestamps and DateTime objects are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage()); + } else { + $this->assertStringEndsWith('Only integer unix timestamps are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage()); + } + } + } + /** * @dataProvider formatProvider */ @@ -261,7 +290,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase ); // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances - if (version_compare(\PHP_VERSION, '5.3.4', '>=')) { + if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) { $dateTime = new \DateTime('@0'); /* general, DateTime */ @@ -310,7 +339,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase { $message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'; - if (version_compare(\PHP_VERSION, '5.3.4', '>=')) { + if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) { $message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR'; } diff --git a/tests/Symfony/Tests/Component/Locale/TestCase.php b/tests/Symfony/Tests/Component/Locale/TestCase.php index 0d6661fb49..8df87e447b 100644 --- a/tests/Symfony/Tests/Component/Locale/TestCase.php +++ b/tests/Symfony/Tests/Component/Locale/TestCase.php @@ -58,6 +58,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase } } + protected function isGreaterOrEqualThanPhpVersion($version) + { + return version_compare(\PHP_VERSION, $version, '>='); + } + protected function isGreaterOrEqualThanIcuVersion($version) { $version = $this->normalizeIcuVersion($version); From f16ff892bb12fab4e06f410ca7c8ffb02bef7ecb Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 19:08:47 -0300 Subject: [PATCH 4/7] [Locale] fixed StubNumberFormatter::parse() to behave like the NumberFormatter::parse() regarding to error flagging --- .../Locale/Stub/StubNumberFormatter.php | 10 +++++-- .../Locale/Stub/StubNumberFormatterTest.php | 27 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php index 73719d6293..a3a0412094 100644 --- a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php @@ -501,7 +501,7 @@ class StubNumberFormatter // Any string before the numeric value causes error in the parsing if (isset($matches[1]) && !empty($matches[1])) { - StubIntl::setError(StubIntl::U_PARSE_ERROR); + StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Number parsing failed'); $this->errorCode = StubIntl::getErrorCode(); $this->errorMessage = StubIntl::getErrorMessage(); @@ -510,8 +510,14 @@ class StubNumberFormatter // Remove everything that is not number or dot (.) $value = preg_replace('/[^0-9\.\-]/', '', $value); + $value = $this->convertValueDataType($value, $type); - return $this->convertValueDataType($value, $type); + // behave like the intl extension + StubIntl::setError(StubIntl::U_ZERO_ERROR); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); + + return $value; } /** diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php index 7535e28c23..000d736af7 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php @@ -669,10 +669,19 @@ class StubNumberFormatterTest extends LocaleTestCase $this->assertSame($expected, $parsedValue, $message); if ($expected === false) { - $this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode()); + $errorCode = StubIntl::U_PARSE_ERROR; + $errorMessage = 'Number parsing failed: U_PARSE_ERROR'; } else { - $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode()); + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; } + + $this->assertSame($errorMessage, StubIntl::getErrorMessage()); + $this->assertSame($errorCode, StubIntl::getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); } /** @@ -682,15 +691,25 @@ class StubNumberFormatterTest extends LocaleTestCase { $this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfICUVersionIsTooOld(); + $formatter = $this->getIntlFormatterWithDecimalStyle(); $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE); $this->assertSame($expected, $parsedValue, $message); if ($expected === false) { - $this->assertSame(U_PARSE_ERROR, $formatter->getErrorCode()); + $errorCode = StubIntl::U_PARSE_ERROR; + $errorMessage = 'Number parsing failed: U_PARSE_ERROR'; } else { - $this->assertEquals(U_ZERO_ERROR, $formatter->getErrorCode()); + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; } + + $this->assertSame($errorMessage, intl_get_error_message()); + $this->assertSame($errorCode, intl_get_error_code()); + $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode())); } public function parseProvider() From e4cbbf3e8c9d423d25b2693743bdf9e4b8dfa424 Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 19:28:47 -0300 Subject: [PATCH 5/7] [Locale] fixed StubNumberFormatter::format() to behave like the NumberFormatter::parse() regarding to error flagging --- .../Locale/Stub/StubNumberFormatter.php | 8 ++++++- .../Locale/Stub/StubNumberFormatterTest.php | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php index a3a0412094..c4949d062c 100644 --- a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php @@ -351,8 +351,14 @@ class StubNumberFormatter $fractionDigits = $this->getAttribute(self::FRACTION_DIGITS); $value = $this->round($value, $fractionDigits); + $value = $this->formatNumber($value, $fractionDigits); - return $this->formatNumber($value, $fractionDigits); + // behave like the intl extension + StubIntl::setError(StubIntl::U_ZERO_ERROR); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); + + return $value; } /** diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php index 000d736af7..1236598ab7 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php @@ -225,15 +225,36 @@ class StubNumberFormatterTest extends LocaleTestCase public function testFormatStub() { + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; + $formatter = $this->getStubFormatterWithDecimalStyle(); $this->assertSame('9.555', $formatter->format(9.555)); + + $this->assertSame($errorMessage, StubIntl::getErrorMessage()); + $this->assertSame($errorCode, StubIntl::getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); } public function testFormatIntl() { $this->skipIfIntlExtensionIsNotLoaded(); + + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; + $formatter = $this->getIntlFormatterWithDecimalStyle(); $this->assertSame('9.555', $formatter->format(9.555)); + + $this->assertSame($errorMessage, intl_get_error_message()); + $this->assertSame($errorCode, intl_get_error_code()); + $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode())); } /** From 09d30d3d1e980b0b9804bdb8f237d705b4c581a3 Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 20:01:42 -0300 Subject: [PATCH 6/7] [Locale] refactored some code --- .../Component/Locale/Stub/StubCollator.php | 14 +--- .../Locale/Stub/StubNumberFormatter.php | 66 ++++++++++--------- .../Locale/Stub/StubCollatorTest.php | 5 +- .../Locale/Stub/StubIntlDateFormatterTest.php | 27 +++++--- 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/Symfony/Component/Locale/Stub/StubCollator.php b/src/Symfony/Component/Locale/Stub/StubCollator.php index a6bd4ddb7d..73b1d76096 100644 --- a/src/Symfony/Component/Locale/Stub/StubCollator.php +++ b/src/Symfony/Component/Locale/Stub/StubCollator.php @@ -21,16 +21,6 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio */ class StubCollator { - /** - * Constants defined by the intl extension, not class constants in IntlDateFormatter - * TODO: remove if the Form component drop the call to the intl_is_failure() function - * - * @see StubIntlDateFormatter::getErrorCode() - * @see StubIntlDateFormatter::getErrorMessage() - */ - const U_ZERO_ERROR = 0; - const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR'; - /** Attribute constants */ const FRENCH_COLLATION = 0; const ALTERNATE_HANDLING = 1; @@ -158,7 +148,7 @@ class StubCollator */ public function getErrorCode() { - return self::U_ZERO_ERROR; + return StubIntl::U_ZERO_ERROR; } /** @@ -168,7 +158,7 @@ class StubCollator */ public function getErrorMessage() { - return self::U_ZERO_ERROR_MESSAGE; + return 'U_ZERO_ERROR'; } /** diff --git a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php index c4949d062c..08df95e173 100644 --- a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php @@ -24,20 +24,6 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio */ class StubNumberFormatter { - /** - * The error code from the last operation - * - * @var integer - */ - protected $errorCode = StubIntl::U_ZERO_ERROR; - - /** - * The error message from the last operation - * - * @var string - */ - protected $errorMessage = 'U_ZERO_ERROR'; - /** Format style constants */ const PATTERN_DECIMAL = 0; const DECIMAL = 1; @@ -125,6 +111,30 @@ class StubNumberFormatter const PAD_BEFORE_SUFFIX = 2; const PAD_AFTER_SUFFIX = 3; + /** + * The error code from the last operation + * + * @var integer + */ + protected $errorCode = StubIntl::U_ZERO_ERROR; + + /** + * The error message from the last operation + * + * @var string + */ + protected $errorMessage = 'U_ZERO_ERROR'; + + /** + * @var string + */ + private $locale; + + /** + * @var int + */ + private $style; + /** * Default values for the en locale * @@ -211,16 +221,6 @@ class StubNumberFormatter 'negative' => -9223372036854775808 ); - /** - * @var string - */ - private $locale = null; - - /** - * @var int - */ - private $style = null; - /** * Constructor * @@ -354,9 +354,7 @@ class StubNumberFormatter $value = $this->formatNumber($value, $fractionDigits); // behave like the intl extension - StubIntl::setError(StubIntl::U_ZERO_ERROR); - $this->errorCode = StubIntl::getErrorCode(); - $this->errorMessage = StubIntl::getErrorMessage(); + $this->resetError(); return $value; } @@ -519,9 +517,7 @@ class StubNumberFormatter $value = $this->convertValueDataType($value, $type); // behave like the intl extension - StubIntl::setError(StubIntl::U_ZERO_ERROR); - $this->errorCode = StubIntl::getErrorCode(); - $this->errorMessage = StubIntl::getErrorMessage(); + $this->resetError(); return $value; } @@ -624,6 +620,16 @@ class StubNumberFormatter throw new MethodNotImplementedException(__METHOD__); } + /** + * Set the error to the default U_ZERO_ERROR + */ + protected function resetError() + { + StubIntl::setError(StubIntl::U_ZERO_ERROR); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); + } + /** * Rounds a currency value, applying increment rounding if applicable * diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubCollatorTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubCollatorTest.php index f65da5d180..397b2732ca 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubCollatorTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubCollatorTest.php @@ -15,6 +15,7 @@ require_once __DIR__.'/../TestCase.php'; use Symfony\Component\Locale\Locale; use Symfony\Component\Locale\Stub\StubCollator; +use Symfony\Component\Locale\Stub\StubIntl; use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase; class StubCollatorTest extends LocaleTestCase @@ -91,13 +92,13 @@ class StubCollatorTest extends LocaleTestCase public function testGetErrorCode() { $collator = $this->createStubCollator(); - $this->assertEquals(StubCollator::U_ZERO_ERROR, $collator->getErrorCode()); + $this->assertEquals(StubIntl::U_ZERO_ERROR, $collator->getErrorCode()); } public function testGetErrorMessage() { $collator = $this->createStubCollator(); - $this->assertEquals(StubCollator::U_ZERO_ERROR_MESSAGE, $collator->getErrorMessage()); + $this->assertEquals('U_ZERO_ERROR', $collator->getErrorMessage()); } public function testGetLocale() diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php index 4317a18830..2beb388e93 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php @@ -85,8 +85,11 @@ class StubIntlDateFormatterTest extends LocaleTestCase /** * @dataProvider formatProvider */ - public function testFormatStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + public function testFormatStub($pattern, $timestamp, $expected) { + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; + $formatter = $this->createStubFormatter($pattern); $this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); @@ -100,10 +103,14 @@ class StubIntlDateFormatterTest extends LocaleTestCase /** * @dataProvider formatProvider */ - public function testFormatIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + public function testFormatIntl($pattern, $timestamp, $expected) { $this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfICUVersionIsTooOld(); + + $errorCode = StubIntl::U_ZERO_ERROR; + $errorMessage = 'U_ZERO_ERROR'; + $formatter = $this->createIntlFormatter($pattern); $this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($errorMessage, intl_get_error_message()); @@ -308,10 +315,12 @@ class StubIntlDateFormatterTest extends LocaleTestCase /** * @dataProvider formatErrorProvider */ - public function testFormatErrorStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + public function testFormatIllegalArgumentErrorStub($pattern, $timestamp, $errorMessage) { + $errorCode = StubIntl::U_ILLEGAL_ARGUMENT_ERROR; + $formatter = $this->createStubFormatter($pattern); - $this->assertSame($expected, $formatter->format($timestamp)); + $this->assertFalse($formatter->format($timestamp)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); @@ -323,13 +332,15 @@ class StubIntlDateFormatterTest extends LocaleTestCase /** * @dataProvider formatErrorProvider */ - public function testFormatErrorIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR') + public function testFormatIllegalArgumentErrorIntl($pattern, $timestamp, $errorMessage) { $this->skipIfIntlExtensionIsNotLoaded(); $this->skipIfICUVersionIsTooOld(); + $errorCode = StubIntl::U_ILLEGAL_ARGUMENT_ERROR; + $formatter = $this->createIntlFormatter($pattern); - $this->assertSame($expected, $formatter->format($timestamp)); + $this->assertFalse($formatter->format($timestamp)); $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); @@ -344,8 +355,8 @@ class StubIntlDateFormatterTest extends LocaleTestCase } return array( - array('y-M-d', '0', false, 1, $message), - array('y-M-d', 'foobar', false, 1, $message), + array('y-M-d', '0', $message), + array('y-M-d', 'foobar', $message), ); } From fab1b5ac8f258b01b7a3c339c9494815b7757002 Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 20:23:02 -0300 Subject: [PATCH 7/7] [Locale] changed inequality operator to strict checking and updated some assertions --- ...odArgumentValueNotImplementedException.php | 2 +- .../Stub/DateFormat/TimeZoneTransformer.php | 2 +- .../Locale/Stub/StubIntlDateFormatter.php | 4 ++-- .../Component/Locale/Stub/StubLocale.php | 2 +- .../Locale/Stub/StubIntlDateFormatterTest.php | 24 +++++++++---------- .../Locale/Stub/StubNumberFormatterTest.php | 16 ++++++------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php b/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php index 770f4181f6..2fdb6feaed 100644 --- a/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php +++ b/src/Symfony/Component/Locale/Exception/MethodArgumentValueNotImplementedException.php @@ -33,7 +33,7 @@ class MethodArgumentValueNotImplementedException extends NotImplementedException $methodName, $argName, var_export($argValue, true), - $additionalMessage != '' ? ' '.$additionalMessage.'. ' : '' + $additionalMessage !== '' ? ' '.$additionalMessage.'. ' : '' ); parent::__construct($message); diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php index d48e0a84b4..f817ba0d21 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php +++ b/src/Symfony/Component/Locale/Stub/DateFormat/TimeZoneTransformer.php @@ -88,7 +88,7 @@ class TimeZoneTransformer extends Transformer )); } - return 'Etc/GMT'.($hours != 0 ? $signal.$hours : ''); + return 'Etc/GMT'.($hours !== 0 ? $signal.$hours : ''); } throw new \InvalidArgumentException('The GMT time zone \'%s\' does not match with the supported formats GMT[+-]HH:MM or GMT[+-]HHMM.'); diff --git a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php index ec339043a1..ca21c9e82c 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php @@ -124,11 +124,11 @@ class StubIntlDateFormatter */ public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null) { - if ('en' != $locale) { + if ('en' !== $locale) { throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the \'en\' locale is supported'); } - if (self::GREGORIAN != $calendar) { + if (self::GREGORIAN !== $calendar) { throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported'); } diff --git a/src/Symfony/Component/Locale/Stub/StubLocale.php b/src/Symfony/Component/Locale/Stub/StubLocale.php index f4c3852e6d..831960f21b 100644 --- a/src/Symfony/Component/Locale/Stub/StubLocale.php +++ b/src/Symfony/Component/Locale/Stub/StubLocale.php @@ -480,7 +480,7 @@ class StubLocale */ static private function getStubData($locale, $cacheVariable, $stubDataDir) { - if ('en' != $locale) { + if ('en' !== $locale) { throw new \InvalidArgumentException(sprintf('Only the \'en\' locale is supported. %s', NotImplementedException::INTL_INSTALL_MESSAGE)); } diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php index 2beb388e93..79bcd5299c 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php @@ -94,10 +94,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + $this->assertFalse(StubIntl::isFailure($formatter->getErrorCode())); } /** @@ -115,7 +115,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($expected, $formatter->format($timestamp)); $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertFalse(intl_is_failure(intl_get_error_code())); } public function formatProvider() @@ -323,10 +323,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertFalse($formatter->format($timestamp)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertTrue(StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + $this->assertTrue(StubIntl::isFailure($formatter->getErrorCode())); } /** @@ -343,7 +343,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertFalse($formatter->format($timestamp)); $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertTrue(intl_is_failure(intl_get_error_code())); } public function formatErrorProvider() @@ -598,7 +598,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($expected, $formatter->parse($value)); $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertFalse(intl_is_failure(intl_get_error_code())); } /** @@ -613,10 +613,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertSame($expected, $formatter->parse($value)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + $this->assertFalse(StubIntl::isFailure($formatter->getErrorCode())); } public function parseProvider() @@ -775,7 +775,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertFalse($formatter->parse($value)); $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertTrue(intl_is_failure(intl_get_error_code())); } /** @@ -790,10 +790,10 @@ class StubIntlDateFormatterTest extends LocaleTestCase $this->assertFalse($formatter->parse($value)); $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertTrue(StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + $this->assertTrue(StubIntl::isFailure($formatter->getErrorCode())); } public function parseErrorProvider() diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php index 1236598ab7..e775763aee 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php +++ b/tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php @@ -233,10 +233,10 @@ class StubNumberFormatterTest extends LocaleTestCase $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertFalse(StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + $this->assertFalse(StubIntl::isFailure($formatter->getErrorCode())); } public function testFormatIntl() @@ -251,10 +251,10 @@ class StubNumberFormatterTest extends LocaleTestCase $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertFalse(intl_is_failure(intl_get_error_code())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode())); + $this->assertFalse(intl_is_failure($formatter->getErrorCode())); } /** @@ -699,10 +699,10 @@ class StubNumberFormatterTest extends LocaleTestCase $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorCode !== 0, StubIntl::isFailure(StubIntl::getErrorCode())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); + $this->assertSame($errorCode !== 0, StubIntl::isFailure($formatter->getErrorCode())); } /** @@ -727,10 +727,10 @@ class StubNumberFormatterTest extends LocaleTestCase $this->assertSame($errorMessage, intl_get_error_message()); $this->assertSame($errorCode, intl_get_error_code()); - $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code())); + $this->assertSame($errorCode > 0, intl_is_failure(intl_get_error_code())); $this->assertSame($errorMessage, $formatter->getErrorMessage()); $this->assertSame($errorCode, $formatter->getErrorCode()); - $this->assertSame($errorCode != 0, intl_is_failure($formatter->getErrorCode())); + $this->assertSame($errorCode > 0, intl_is_failure($formatter->getErrorCode())); } public function parseProvider()