Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore locales before calling test assertions #45

Merged
merged 5 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
49 changes: 35 additions & 14 deletions tests/Zend/Locale/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
}

/**
Expand Down
53 changes: 42 additions & 11 deletions tests/Zend/Validate/FloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@
*/
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
*
* @var Zend_Validate_Float
*/
protected $_validator;

/**
glensc marked this conversation as resolved.
Show resolved Hide resolved
* @var string
*/
private $_locale;

/**
* Creates a new Zend_Validate_Float object for each test method
*
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -157,35 +172,51 @@ 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);
}

/**
* @ZF-7987
*/
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);
}

/**
* @ZF-8919
*/
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);
}

/**
Expand All @@ -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'));
Expand Down