Use setlocale. Catch errors when setting locale. Restore environment after test.
authorGunnar Wrobel <p@rdus.de>
Mon, 18 Oct 2010 05:06:13 +0000 (07:06 +0200)
committerGunnar Wrobel <p@rdus.de>
Mon, 18 Oct 2010 12:49:20 +0000 (14:49 +0200)
For me the test didn't run without also calling setlocale(). Added
this based on the PHPUnit setLocale() call which resets to the
old locale after testing.

In case setting the locale fails the tests will be skipped.

I'm not certain how environment variables will be handled by PHPUnit
but to be on the safe side I rather reset them after the test.

framework/Translation/test/Horde/Translation/GettextTest.php

index a63bbdd..bc3c327 100644 (file)
@@ -11,16 +11,25 @@ class Horde_Translation_GettextTest extends PHPUnit_Framework_TestCase
 {
     private $_dict;
     private $_otherDict;
+    private $_env;
 
     public function setUp()
     {
-        putenv('LC_ALL=de_DE.UTF-8');
-        putenv('LANG=de_DE.UTF-8');
-        putenv('LANGUAGE=de_DE.UTF-8');
+        try {
+            $this->setLocale(LC_ALL, 'de_DE.UTF-8');
+        } catch (PHPUnit_Framework_Exception $e) {
+            $this->markTestSkipped('Setting the locale failed. de_DE.UTF-8 might not be supported.');
+        }
+        $this->_setEnv('de_DE.UTF-8');
         $this->_dict = new Horde_Translation_Gettext('Horde_Translation', dirname(__FILE__) . '/locale');
         $this->_otherDict = new Horde_Translation_Gettext('Horde_Other', dirname(__FILE__) . '/locale');
     }
 
+    public function tearDown()
+    {
+        $this->_restoreEnv();
+    }
+
     public function testGettext()
     {
         $this->assertEquals('Heute', $this->_dict->t('Today'));
@@ -34,4 +43,19 @@ class Horde_Translation_GettextTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('1 Woche', sprintf($this->_dict->ngettext('%d week', '%d weeks', 1), 1));
         $this->assertEquals('2 Wochen', sprintf($this->_dict->ngettext('%d week', '%d weeks', 2), 2));
     }
+
+    private function _setEnv($value)
+    {
+        foreach (array('LC_ALL', 'LANG', 'LANGUAGE') as $env) {
+            $this->_env[$env] = getenv($env);
+            putenv($env . '=' . $value);
+        }
+    }
+
+    private function _restoreEnv()
+    {
+        foreach (array('LC_ALL', 'LANG', 'LANGUAGE') as $env) {
+            putenv($env . '=' . $this->_env[$env]);
+        }
+    }
 }