فهرست منبع

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

Eriksen Costa 13 سال پیش
والد
کامیت
0a606642b7

+ 8 - 4
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -175,14 +175,18 @@ class StubIntlDateFormatter
     {
         // intl allows timestamps to be passed as arrays - we don't
         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
         $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 ';
-        } 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';
         }
 
@@ -195,7 +199,7 @@ class StubIntlDateFormatter
         }
 
         // 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();
         }
 

+ 31 - 2
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -53,6 +53,35 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         $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
      */
@@ -261,7 +290,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         );
 
         // 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');
 
             /* 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';
 
-        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';
         }
 

+ 5 - 0
tests/Symfony/Tests/Component/Locale/TestCase.php

@@ -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)
     {
         $version = $this->normalizeIcuVersion($version);