瀏覽代碼

[Locale] refactored some code

Eriksen Costa 13 年之前
父節點
當前提交
09d30d3d1e

+ 2 - 12
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';
     }
 
     /**

+ 36 - 30
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
      *

+ 3 - 2
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()

+ 19 - 8
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),
         );
     }