8000 [WIP][Locale] StubIntlDateFormatter::parse was throwing exception ins… · sadiqmmm/symfony@beb4fc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit beb4fc0

Browse files
committed
[WIP][Locale] StubIntlDateFormatter::parse was throwing exception instead of returning Boolean false like intl implementation
1 parent b61dff7 commit beb4fc0

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ public function parse(\DateTime $dateTime, $value)
154154
return $this->calculateUnixTimestamp($dateTime, $options);
155155
}
156156

157-
throw new \InvalidArgumentException(sprintf("Failed to match value '%s' with pattern '%s'", $value, $this->pattern));
157+
// behave like the intl extension
158+
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
159+
160+
return false;
158161
}
159162

160163
/**

tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,11 @@ public function testLocaltime()
517517
/**
518518
* @dataProvider parseProvider
519519
*/
520-
public function testParseIntl($pattern, $value, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
520+
public function testParseIntl($pattern, $value, $expected)
521521
{
522+
$errorCode = StubIntl::U_ZERO_ERROR;
523+
$errorMessage = 'U_ZERO_ERROR';
524+
522525
$this->skipIfIntlExtensionIsNotLoaded();
523526
$formatter = $this->createIntlFormatter($pattern);
524527
$this->assertSame($expected, $formatter->parse($value));
@@ -530,8 +533,11 @@ public function testParseIntl($pattern, $value, $expected, $errorCode = 0, $erro
530533
/**
531534
* @dataProvider parseProvider
532535
*/
533-
public function testParseStub($pattern, $value, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
536+
public function testParseStub($pattern, $value, $expected)
534537
{
538+
$errorCode = StubIntl::U_ZERO_ERROR;
539+
$errorMessage = 'U_ZERO_ERROR';
540+
535541
$formatter = $this->createStubFormatter($pattern);
536542
$this->assertSame($expected, $formatter->parse($value));
537543
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
@@ -551,19 +557,11 @@ public function parseProvider()
551557
array('y-MMM-d', '1970-Jan-1', 0),
552558
array('y-MMMM-d', '1970-January-1', 0),
553559

554-
// 1 char month
555-
array('y-MMMMM-d', '1970-J-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
556-
array('y-MMMMM-d', '1970-S-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
557-
558560
// standalone months
559561
array('y-L-d', '1970-1-1', 0),
560562
array('y-LLL-d', '1970-Jan-1', 0),
561563
array('y-LLLL-d', '1970-January-1', 0),
562564

563-
// standalone 1 char month
564-
array('y-LLLLL-d', '1970-J-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
565-
array('y-LLLLL-d', '1970-S-1', false, 9, 'Date parsing failed: U_PARSE_ERROR'),
566-
567565
// days
568566
array('y-M-d', '1970-1-1', 0),
569567
array('y-M-dd', '1970-1-01', 0),
@@ -690,6 +688,53 @@ public function p 10000 arseProvider()
690688
);
691689
}
692690

691+
/**
692+
* @dataProvider parseErrorProvider
693+
*/
694+
public function testParseErrorIntl($pattern, $value)
695+
{
696+
$errorCode = StubIntl::U_PARSE_ERROR;
697+
$errorMessage = 'Date parsing failed: U_PARSE_ERROR';
698+
699+
$this->skipIfIntlExtensionIsNotLoaded();
700+
$formatter = $this->createIntlFormatter($pattern);
701+
$this->assertSame(false, $formatter->parse($value));
702+
$this->assertSame($errorMessage, intl_get_error_message());
703+
$this->assertSame($errorCode, intl_get_error_code());
704+
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
705+
}
706+
707+
/**
708+
* @dataProvider parseErrorProvider
709+
*/
710+
public function testParseErrorStub($pattern, $value)
711+
{
712+
$errorCode = StubIntl::U_PARSE_ERROR;
713+
$errorMessage = 'Date parsing failed: U_PARSE_ERROR';
714+
715+
$formatter = $this->createStubFormatter($pattern);
716+
$this->assertSame(false, $formatter->parse($value));
717+
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
718+
$this->assertSame($errorCode, StubIntl::getErrorCode());
719+
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
720+
}
721+
722+
public function parseErrorProvider()
723+
{
724+
return array(
725+
array('y-M-d', '1970/1/1'),
726+
array('yy-M-d', '70/1/1'),
727+
728+
// 1 char month
729+
array('y-MMMMM-d', '1970-J-1'),
730+
array('y-MMMMM-d', '1970-S-1'),
731+
732+
// standalone 1 char month
733+
array('y-LLLLL-d', '1970-J-1'),
734+
array('y-LLLLL-d', '1970-S-1'),
735+
);
736+
}
737+
693738
/**
694739
* Just to document the differences between the stub and the intl implementations. The intl can parse
695740
* any of the tested formats alone. The stub does not implement them as it would be needed to add more

0 commit comments

Comments
 (0)
0