浏览代码

merged branch aboks/icu_version (PR #1776)

Commits
-------

86aeb04 [Form] Added skip for tests that require a higher version of the ICU library (needed by intl) than installed

Discussion
----------

[Form] Skip tests that are incompatible with the installed ICU (intl) library

Two tests for the `DateTimeToLocalizedStringTransformer` depend on a datetime-format that has only been added in version 3.8 of the ICU library (used by `intl`), see http://bugs.icu-project.org/trac/changeset/21815#file60. Some PHP-versions still ship with ICU 3.6 however.

This PR skips these tests when an incompatible version of the ICU library is detected. Unfortunately I had to copy-paste some code from `Symfony\Tests\Component\Locale\TestCase`, but I don't see any other way without creating a strange dependency between the tests for the Form and Locale components.

---------------------------------------------------------------------------

by Abhoryo at 2011/07/22 16:42:40 -0700

PHP 5.3.5 = ICU 3.6
PHP 5.3.6 = ICU 3.8
PHP 5.3.7rc4 = ICU 4.6

Since the RC3 of Symfony, config/check script recommends ICU 4.0+

---------------------------------------------------------------------------

by aboks at 2011/07/23 00:33:12 -0700

I noticed, but since it's not a hard requirement (and difficult for some people to install ICU 4.0+ at the moment) this PR would at least prevent false positives when people unable to upgrade run the test suite.
Fabien Potencier 14 年之前
父节点
当前提交
d236fc3662

+ 8 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

@@ -96,6 +96,10 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
 
     public function testTransformFullTime()
     {
+        if ($this->isLowerThanIcuVersion(3.8)) {
+            $this->markTestSkipped('Please upgrade ICU version to 3.8+');
+        }
+
         $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter::FULL);
 
         $this->assertEquals('03.02.2010 04:05:06 GMT+00:00', $transformer->transform($this->dateTime));
@@ -207,6 +211,10 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
 
     public function testReverseTransformFullTime()
     {
+        if ($this->isLowerThanIcuVersion(3.8)) {
+            $this->markTestSkipped('Please upgrade ICU version to 3.8+');
+        }
+
         $transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, \IntlDateFormatter::FULL);
 
         $this->assertDateTimeEquals($this->dateTime, $transformer->reverseTransform('03.02.2010 04:05:06 GMT+00:00', null));

+ 47 - 1
tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/LocalizedTestCase.php

@@ -13,12 +13,58 @@ namespace Symfony\Tests\Component\Form\Extension\Core\DataTransformer;
 
 class LocalizedTestCase extends \PHPUnit_Framework_TestCase
 {
+    protected static $icuVersion = null;
+
     protected function setUp()
     {
         parent::setUp();
 
-        if (!extension_loaded('intl')) {
+        if (!$this->isIntlExtensionLoaded()) {
             $this->markTestSkipped('The "intl" extension is not available');
         }
     }
+
+    protected function isIntlExtensionLoaded()
+    {
+        return extension_loaded('intl');
+    }
+
+    protected function isLowerThanIcuVersion($version)
+    {
+        $version = $this->normalizeIcuVersion($version);
+        $icuVersion = $this->normalizeIcuVersion($this->getIntlExtensionIcuVersion());
+
+        return $icuVersion < $version;
+    }
+
+    protected function normalizeIcuVersion($version)
+    {
+        return ((float) $version) * 100;
+    }
+
+    protected function getIntlExtensionIcuVersion()
+    {
+        if (isset(self::$icuVersion)) {
+            return self::$icuVersion;
+        }
+
+        if (!$this->isIntlExtensionLoaded()) {
+            throw new \RuntimeException('The intl extension is not available');
+        }
+
+        if (defined('INTL_ICU_VERSION')) {
+            return INTL_ICU_VERSION;
+        }
+
+        $reflector = new \ReflectionExtension('intl');
+
+        ob_start();
+        $reflector->info();
+        $output = ob_get_clean();
+
+        preg_match('/^ICU version => (.*)$/m', $output, $matches);
+        self::$icuVersion = $matches[1];
+
+        return self::$icuVersion;
+    }
 }