Browse Source

merged branch eriksencosta/ticket_2781 (PR #3350)

Commits
-------

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

Discussion
----------

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

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: ![travis.ci](https://secure.travis-ci.org/eriksencosta/symfony.png?branch=ticket_2781)
Fixes the following tickets: #2781
Todo: A test fail in 32 bit environment, executed tests only with PHP 5.3.2 and ext-intl ICU 4.2 based

Failed test:

    1) Symfony\Tests\Component\Locale\Stub\StubIntlDateFormatterTest::testFormatWithDefaultTimezoneIntl
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@
    -'1969-12-31 21:00:00'
    +'1969-12-31 16:00:00'
Fabien Potencier 13 years ago
parent
commit
b86e6db035

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

+ 2 - 0
src/Symfony/Component/Locale/Stub/StubNumberFormatter.php

@@ -386,6 +386,7 @@ class StubNumberFormatter
      * Returns formatter's last error code. Always returns the U_ZERO_ERROR class constant value
      *
      * @return int  The error code from last formatter call
+     *
      * @see    http://www.php.net/manual/en/numberformatter.geterrorcode.php
      */
     public function getErrorCode()
@@ -489,6 +490,7 @@ class StubNumberFormatter
      * @param  string       $value                          The value to parse
      * @param  string       $type                           Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default
      * @param  int          $position                       Offset to begin the parsing on return this value will hold the offset at which the parsing ended
+     *
      * @return Boolean|string                               The parsed value of false on error
      *
      * @see    http://www.php.net/manual/en/numberformatter.parse.php

+ 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