[Locale] fixed StubIntlDateFormatter::format() to set the right error for PHP >= 5.3.4 and to behave like the intl when formatting successfully

This commit is contained in:
Eriksen Costa 2012-04-15 14:56:41 -03:00
parent 48af0ba722
commit 312a5a4201
2 changed files with 38 additions and 8 deletions

View File

@ -179,8 +179,17 @@ class StubIntlDateFormatter
}
// behave like the intl extension
$argumentError = null;
if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) {
StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, 'datefmt_format: takes either an array or an integer timestamp value ');
$argumentError = 'datefmt_format: takes either an array or an integer timestamp value ';
} elseif (!is_int($timestamp) && !$timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) {
$argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object';
}
if (null !== $argumentError) {
StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, $argumentError);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
return false;
}
@ -193,6 +202,11 @@ class StubIntlDateFormatter
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
$formatted = $transformer->format($this->createDateTime($timestamp));
// behave like the intl extension
StubIntl::setError(StubIntl::U_ZERO_ERROR);
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();
return $formatted;
}
@ -359,6 +373,7 @@ class StubIntlDateFormatter
$timestamp = $transformer->parse($dateTime, $value);
// behave like the intl extension. FullTransformer::parse() set the proper error
if (false === $timestamp) {
$this->errorCode = StubIntl::getErrorCode();
$this->errorMessage = StubIntl::getErrorMessage();

View File

@ -82,6 +82,21 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
}
/**
* @dataProvider formatErrorProvider
*/
public function testFormatErrorStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
{
$formatter = $this->createStubFormatter($pattern);
$this->assertSame($expected, $formatter->format($timestamp));
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
$this->assertSame($errorCode, StubIntl::getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
$this->assertSame($errorMessage, $formatter->getErrorMessage());
$this->assertSame($errorCode, $formatter->getErrorCode());
$this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
}
/**
* @dataProvider formatErrorProvider
*/
@ -90,10 +105,6 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->skipIfIntlExtensionIsNotLoaded();
$this->skipIfICUVersionIsTooOld();
if (version_compare(PHP_VERSION, '5.3.3') > 0) {
$this->markTestSkipped('The intl error messages were change in PHP 5.3.3.');
}
$formatter = $this->createIntlFormatter($pattern);
$this->assertSame($expected, $formatter->format($timestamp));
$this->assertSame($errorMessage, intl_get_error_message());
@ -297,11 +308,15 @@ class StubIntlDateFormatterTest extends LocaleTestCase
public function formatErrorProvider()
{
/* errors */
$message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR';
if (version_compare(\PHP_VERSION, '5.3.4', '>=')) {
$message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR';
}
return array(
array('y-M-d', '0', false, 1, 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'),
array('y-M-d', 'foobar', false, 1, 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'),
array('y-M-d', '0', false, 1, $message),
array('y-M-d', 'foobar', false, 1, $message),
);
}