[Locale] updated StubIntlDateFormatter::format() exception message when timestamp argument is an array for PHP >= 5.3.4

This commit is contained in:
Eriksen Costa 2012-04-15 15:32:40 -03:00
parent e4769d9377
commit 0a606642b7
3 changed files with 44 additions and 6 deletions

View File

@ -175,14 +175,18 @@ class StubIntlDateFormatter
{ {
// intl allows timestamps to be passed as arrays - we don't // intl allows timestamps to be passed as arrays - we don't
if (is_array($timestamp)) { if (is_array($timestamp)) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, 'Only integer unix timestamps are supported'); $message = version_compare(\PHP_VERSION, '5.3.4', '>=') ?
'Only integer unix timestamps and DateTime objects are supported' :
'Only integer unix timestamps are supported';
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message);
} }
// behave like the intl extension // behave like the intl extension
$argumentError = null; $argumentError = null;
if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) { if (version_compare(\PHP_VERSION, '5.3.4', '<') && !is_int($timestamp)) {
$argumentError = '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', '>=')) { } elseif (version_compare(\PHP_VERSION, '5.3.4', '>=') && !is_int($timestamp) && !$timestamp instanceOf \DateTime) {
$argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object'; $argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object';
} }
@ -195,7 +199,7 @@ class StubIntlDateFormatter
} }
// As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
if ($timestamp instanceOf \DateTime && version_compare(\PHP_VERSION, '5.3.4', '>=')) { if (version_compare(\PHP_VERSION, '5.3.4', '>=') && $timestamp instanceOf \DateTime) {
$timestamp = $timestamp->getTimestamp(); $timestamp = $timestamp->getTimestamp();
} }

View File

@ -53,6 +53,35 @@ class StubIntlDateFormatterTest extends LocaleTestCase
$this->assertNull($formatter->getTimeZoneId()); $this->assertNull($formatter->getTimeZoneId());
} }
public function testFormatWithUnsupportedTimestampArgument()
{
$formatter = $this->createStubFormatter();
$localtime = array(
'tm_sec' => 59,
'tm_min' => 3,
'tm_hour' => 15,
'tm_mday' => 15,
'tm_mon' => 3,
'tm_year' => 112,
'tm_wday' => 0,
'tm_yday' => 105,
'tm_isdst' => 0
);
try {
$formatter->format($localtime);
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException', $e);
if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
$this->assertStringEndsWith('Only integer unix timestamps and DateTime objects are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage());
} else {
$this->assertStringEndsWith('Only integer unix timestamps are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage());
}
}
}
/** /**
* @dataProvider formatProvider * @dataProvider formatProvider
*/ */
@ -261,7 +290,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
); );
// As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
if (version_compare(\PHP_VERSION, '5.3.4', '>=')) { if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
$dateTime = new \DateTime('@0'); $dateTime = new \DateTime('@0');
/* general, DateTime */ /* general, DateTime */
@ -310,7 +339,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
{ {
$message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR'; $message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR';
if (version_compare(\PHP_VERSION, '5.3.4', '>=')) { if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
$message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR'; $message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR';
} }

View File

@ -58,6 +58,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
} }
} }
protected function isGreaterOrEqualThanPhpVersion($version)
{
return version_compare(\PHP_VERSION, $version, '>=');
}
protected function isGreaterOrEqualThanIcuVersion($version) protected function isGreaterOrEqualThanIcuVersion($version)
{ {
$version = $this->normalizeIcuVersion($version); $version = $this->normalizeIcuVersion($version);