From f16ff892bb12fab4e06f410ca7c8ffb02bef7ecb Mon Sep 17 00:00:00 2001 From: Eriksen Costa Date: Sun, 15 Apr 2012 19:08:47 -0300 Subject: [PATCH] [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()