diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 352738be6..d831aaf88 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -34,6 +34,8 @@ jobs: MYSQL_PASSWORD: "zftest" MYSQL_DATABASE: "zftest" MYSQL_HOST: "127.0.0.1" + # Default locales are: C C.UTF-8 POSIX en_US.utf8 + LOCALES: "fr_FR@euro fr_FR fr_BE.UTF-8 de en_US" services: memcache: @@ -81,6 +83,11 @@ jobs: - name: Setup environment for PHPUnit run: | cp tests/TestConfiguration.ci.php tests/TestConfiguration.php + echo "Existing locales" + locale -a + sudo apt-get update && sudo apt-get install tzdata locales -y && sudo locale-gen $LOCALES + echo "All languages..." + locale -a - name: "Run PHPUnit tests (Experimental: ${{ matrix.experimental }})" env: diff --git a/tests/Zend/Locale/FormatTest.php b/tests/Zend/Locale/FormatTest.php index 39ac8f790..ef9f6fdc3 100644 --- a/tests/Zend/Locale/FormatTest.php +++ b/tests/Zend/Locale/FormatTest.php @@ -35,6 +35,12 @@ */ class Zend_Locale_FormatTest extends PHPUnit_Framework_TestCase { + /** + * Constant for Non-breaking space UTF-8 encoded value. + * https://en.wikipedia.org/wiki/Non-breaking_space + */ + const NBSP = "\xC2\xA0"; + /** * teardown / cleanup */ @@ -934,19 +940,29 @@ public function testConvertPhpToIso() */ public function testToFloatSetlocale() { - setlocale(LC_ALL, 'fr_FR@euro'); // test setup - - //var_dump( setlocale(LC_NUMERIC, '0')); // this is the specific setting of interest - $locale_fr = new Zend_Locale('fr_FR'); - $locale_en = new Zend_Locale('en_US'); - $params_fr = array('precision' => 2, 'locale' => $locale_fr); - $params_en = array('precision' => 2, 'locale' => $locale_en); - $myFloat = 1234.5; - $test1 = Zend_Locale_Format::toFloat($myFloat, $params_fr); - $test2 = Zend_Locale_Format::toFloat($myFloat, $params_en); - $this->assertEquals("1 234,50", $test1); - $this->assertEquals("1,234.50", $test2); - // placing tearDown here (i.e. restoring locale) won't work, if test already failed/aborted above. + $locale = setlocale(LC_ALL, 0); + try { + setlocale(LC_ALL, 'fr_FR@euro'); + + $locale_fr = new Zend_Locale('fr_FR'); + $locale_en = new Zend_Locale('en_US'); + $params_fr = array('precision' => 2, 'locale' => $locale_fr); + $params_en = array('precision' => 2, 'locale' => $locale_en); + $myFloat = 1234.5; + $test1 = Zend_Locale_Format::toFloat($myFloat, $params_fr); + $test2 = Zend_Locale_Format::toFloat($myFloat, $params_en); + + $this->assertEquals("1" . self::NBSP . "234,50", $test1); + $this->assertEquals("1,234.50", $test2); + + } catch (Exception $e) { + setlocale(LC_ALL, $locale); + throw $e; + } catch (Throwable $e) { + setlocale(LC_ALL, $locale); + throw $e; + } + setlocale(LC_ALL, $locale); } /** @@ -1107,14 +1123,19 @@ public function testToNumberWithoutFormatWithPrecision() */ public function testCheckDateFormatDoesNotEmitNoticeWhenNoOptionsAreNotProvided() { + $locale = setlocale(LC_ALL, 0); // read current locale try { setlocale(LC_ALL, 'en_US'); // test setup Zend_Locale_Format::setOptions(array('date_format' => 'yyyy-MM-dd')); + $checkDateFormat = Zend_Locale_Format::checkDateFormat('2011-10-21', array()); - $this->assertTrue(Zend_Locale_Format::checkDateFormat('2011-10-21', array())); + $this->assertTrue($checkDateFormat); } catch ( PHPUnit_Framework_Error_Notice $ex ) { + setlocale(LC_ALL, $locale); // restore previous locale + $this->fail('Zend_Locale_Format::checkDateFormat emitted unexpected E_NOTICE'); } + setlocale(LC_ALL, $locale); // restore previous locale } /** diff --git a/tests/Zend/Validate/FloatTest.php b/tests/Zend/Validate/FloatTest.php index 26a4386c8..e09ce128b 100644 --- a/tests/Zend/Validate/FloatTest.php +++ b/tests/Zend/Validate/FloatTest.php @@ -35,6 +35,12 @@ */ class Zend_Validate_FloatTest extends PHPUnit_Framework_TestCase { + /** + * Constant for Non-breaking space UTF-8 encoded value. + * https://en.wikipedia.org/wiki/Non-breaking_space + */ + const NBSP = "\xC2\xA0"; + /** * Zend_Validate_Float object * @@ -42,6 +48,11 @@ class Zend_Validate_FloatTest extends PHPUnit_Framework_TestCase */ protected $_validator; + /** + * @var string + */ + private $_locale; + /** * Creates a new Zend_Validate_Float object for each test method * @@ -136,10 +147,14 @@ public function testUsingApplicationLocale() */ public function testNoZendLocaleButPhpLocale() { + $locale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'de'); $valid = new Zend_Validate_Float(); - $this->assertTrue($valid->isValid(123,456)); - $this->assertTrue($valid->isValid('123,456')); + $isValid1 = $valid->isValid(123.456); + $isValid2 = $valid->isValid('123,456'); + setlocale(LC_ALL, $locale); + $this->assertTrue($isValid1); + $this->assertTrue($isValid2); } /** @@ -157,9 +172,12 @@ public function testLocaleDeFloatType() */ public function testPhpLocaleDeFloatType() { + $locale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'de'); $valid = new Zend_Validate_Float(); - $this->assertTrue($valid->isValid(10.5)); + $isValid = $valid->isValid(10.5); + setlocale(LC_ALL, $locale); + $this->assertTrue($isValid); } /** @@ -167,9 +185,12 @@ public function testPhpLocaleDeFloatType() */ public function testPhpLocaleFrFloatType() { + $locale = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'fr'); $valid = new Zend_Validate_Float(); - $this->assertTrue($valid->isValid(10.5)); + $isValid = $valid->isValid(10.5); + setlocale(LC_ALL, $locale); + $this->assertTrue($isValid); } /** @@ -177,15 +198,25 @@ public function testPhpLocaleFrFloatType() */ public function testPhpLocaleDeStringType() { + $lcAll = setlocale(LC_ALL, 0); setlocale(LC_ALL, 'de_AT'); + $lcNumeric = setlocale(LC_NUMERIC, 0); setlocale(LC_NUMERIC, 'de_AT'); $valid = new Zend_Validate_Float('de_AT'); - $this->assertTrue($valid->isValid('1,3')); - $this->assertTrue($valid->isValid('1000,3')); - $this->assertTrue($valid->isValid('1.000,3')); - $this->assertFalse($valid->isValid('1.3')); - $this->assertFalse($valid->isValid('1000.3')); - $this->assertFalse($valid->isValid('1,000.3')); + $isValid0 = $valid->isValid('1,3'); + $isValid1 = $valid->isValid('1000,3'); + $isValid2 = $valid->isValid('1.000,3'); + $isValid3 = $valid->isValid('1.3'); + $isValid4 = $valid->isValid('1000.3'); + $isValid5 = $valid->isValid('1,000.3'); + setlocale(LC_ALL, $lcAll); + setlocale(LC_NUMERIC, $lcNumeric); + $this->assertTrue($isValid0); + $this->assertTrue($isValid1); + $this->assertTrue($isValid2); + $this->assertFalse($isValid3); + $this->assertFalse($isValid4); + $this->assertFalse($isValid5); } /** @@ -196,7 +227,7 @@ public function testPhpLocaleFrStringType() $valid = new Zend_Validate_Float('fr_FR'); $this->assertTrue($valid->isValid('1,3')); $this->assertTrue($valid->isValid('1000,3')); - $this->assertTrue($valid->isValid('1 000,3')); + $this->assertTrue($valid->isValid('1' . self::NBSP . '000,3')); $this->assertFalse($valid->isValid('1.3')); $this->assertFalse($valid->isValid('1000.3')); $this->assertFalse($valid->isValid('1,000.3'));