From facaa53b69d3499289a445f083a8339f09d2878c Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel
Date: Mon, 18 Oct 2010 07:06:13 +0200 Subject: [PATCH] Use setlocale. Catch errors when setting locale. Restore environment after test. 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. --- .../test/Horde/Translation/GettextTest.php | 30 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/framework/Translation/test/Horde/Translation/GettextTest.php b/framework/Translation/test/Horde/Translation/GettextTest.php index a63bbdd9f..bc3c327a5 100644 --- a/framework/Translation/test/Horde/Translation/GettextTest.php +++ b/framework/Translation/test/Horde/Translation/GettextTest.php @@ -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]); + } + } } -- 2.11.0