diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php index c9bab9af55..5cb50e76a9 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php +++ b/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; } /** diff --git a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php index b74b68725e..d7accea3cc 100644 --- a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php +++ b/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 diff --git a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php b/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php index 5b5788a966..572986ed35 100644 --- a/tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php +++ b/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