Skip to content

Commit

Permalink
Restore locale if exception is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Mar 16, 2021
1 parent 4d0f809 commit 938c6c2
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions tests/Zend/Locale/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,20 +941,28 @@ public function testConvertPhpToIso()
public function testToFloatSetlocale()
{
$locale = setlocale(LC_ALL, 0);
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);

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);

$this->assertEquals("1" . self::NBSP . "234,50", $test1);
$this->assertEquals("1,234.50", $test2);
}

/**
Expand Down Expand Up @@ -1115,17 +1123,19 @@ public function testToNumberWithoutFormatWithPrecision()
*/
public function testCheckDateFormatDoesNotEmitNoticeWhenNoOptionsAreNotProvided()
{
$locale = setlocale(LC_ALL, 0); // read current locale
try {
$locale = setlocale(LC_ALL, 0); // read current locale
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());
setlocale(LC_ALL, $locale); // restore previous locale

$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
}

/**
Expand Down

0 comments on commit 938c6c2

Please sign in to comment.