Bläddra i källkod

[Locale] fixed StubNumberFormatter::format() to behave like the NumberFormatter::parse() regarding to error flagging

Eriksen Costa 13 år sedan
förälder
incheckning
e4cbbf3e8c

+ 7 - 1
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;
     }
 
     /**

+ 21 - 0
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()));
     }
 
     /**