Explorar el Código

[WIP][Locale] StubIntlDateFormatter::parse was throwing exception instead of returning Boolean false like intl implementation

Eriksen Costa hace 13 años
padre
commit
beb4fc0899

+ 4 - 1
src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php

@@ -154,7 +154,10 @@ class FullTransformer
             return $this->calculateUnixTimestamp($dateTime, $options);
         }
 
-        throw new \InvalidArgumentException(sprintf("Failed to match value '%s' with pattern '%s'", $value, $this->pattern));
+        // behave like the intl extension
+        StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
+
+        return false;
     }
 
     /**

+ 55 - 10
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -517,8 +517,11 @@ class StubIntlDateFormatterTest extends LocaleTestCase
     /**
      * @dataProvider parseProvider
      */
-    public function testParseIntl($pattern, $value, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
+    public function testParseIntl($pattern, $value, $expected)
     {
+        $errorCode = StubIntl::U_ZERO_ERROR;
+        $errorMessage = 'U_ZERO_ERROR';
+
         $this->skipIfIntlExtensionIsNotLoaded();
         $formatter = $this->createIntlFormatter($pattern);
         $this->assertSame($expected, $formatter->parse($value));
@@ -530,8 +533,11 @@ class StubIntlDateFormatterTest extends LocaleTestCase
     /**
      * @dataProvider parseProvider
      */
-    public function testParseStub($pattern, $value, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
+    public function testParseStub($pattern, $value, $expected)
     {
+        $errorCode = StubIntl::U_ZERO_ERROR;
+        $errorMessage = 'U_ZERO_ERROR';
+
         $formatter = $this->createStubFormatter($pattern);
         $this->assertSame($expected, $formatter->parse($value));
         $this->assertSame($errorMessage, StubIntl::getErrorMessage());
@@ -551,19 +557,11 @@ class StubIntlDateFormatterTest extends LocaleTestCase
             array('y-MMM-d', '1970-Jan-1', 0),
             array('y-MMMM-d', '1970-January-1', 0),
 
-            // 1 char month
-            array('y-MMMMM-d', '1970-J-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
-            array('y-MMMMM-d', '1970-S-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
-
             // standalone months
             array('y-L-d', '1970-1-1', 0),
             array('y-LLL-d', '1970-Jan-1', 0),
             array('y-LLLL-d', '1970-January-1', 0),
 
-            // standalone 1 char month
-            array('y-LLLLL-d', '1970-J-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
-            array('y-LLLLL-d', '1970-S-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
-
             // days
             array('y-M-d', '1970-1-1', 0),
             array('y-M-dd', '1970-1-01', 0),
@@ -690,6 +688,53 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         );
     }
 
+    /**
+     * @dataProvider parseErrorProvider
+     */
+    public function testParseErrorIntl($pattern, $value)
+    {
+        $errorCode = StubIntl::U_PARSE_ERROR;
+        $errorMessage = 'Date parsing failed: U_PARSE_ERROR';
+
+        $this->skipIfIntlExtensionIsNotLoaded();
+        $formatter = $this->createIntlFormatter($pattern);
+        $this->assertSame(false, $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()));
+    }
+
+    /**
+     * @dataProvider parseErrorProvider
+     */
+    public function testParseErrorStub($pattern, $value)
+    {
+        $errorCode = StubIntl::U_PARSE_ERROR;
+        $errorMessage = 'Date parsing failed: U_PARSE_ERROR';
+
+        $formatter = $this->createStubFormatter($pattern);
+        $this->assertSame(false, $formatter->parse($value));
+        $this->assertSame($errorMessage, StubIntl::getErrorMessage());
+        $this->assertSame($errorCode, StubIntl::getErrorCode());
+        $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
+    }
+
+    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'),
+
+            // standalone 1 char month
+            array('y-LLLLL-d', '1970-J-1'),
+            array('y-LLLLL-d', '1970-S-1'),
+        );
+    }
+
     /**
      * Just to document the differences between the stub and the intl implementations. The intl can parse
      * any of the tested formats alone. The stub does not implement them as it would be needed to add more