This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Intl/Tests/LocalesTest.php
Fabien Potencier fec95e01a2 bug #31354 [Intl][Validator] Handle alias locales/timezones (ro0NL)
This PR was squashed before being merged into the 4.3-dev branch (closes #31354).

Discussion
----------

[Intl][Validator] Handle alias locales/timezones

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes (including intl-data group)
| Fixed tickets | #31022
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

both timezones and locales have aliases (either thru deprecation/migration/etc.)

for locales we compile a mapping, for timezones we dont. yet we can benefit partial alias support thru DateTimeZone, which knows about most timezone IDs already.

both the timezone + locale validator already support aliases. Connsequently, we should support aliases in  `Timezones::exists()`  + `Locales::exists()` as well IMHO.

so far so good; the catch is; with this PR `Locales::getName()` supports aliases, whereas `Timezones::getName()` doesnt. I think it's reasonable for now, until we compile the timezone mapping so we can widen the timezone ID conversion here.

Commits
-------

0a9be0df6e [Intl][Validator] Handle alias locales/timezones
2019-05-06 11:58:38 +02:00

108 lines
2.9 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\Tests;
use Symfony\Component\Intl\Locales;
/**
* @group intl-data
*/
class LocalesTest extends ResourceBundleTestCase
{
public function testGetLocales()
{
$this->assertSame($this->getLocales(), Locales::getLocales());
}
public function testGetAliases()
{
$this->assertSame($this->getLocaleAliases(), Locales::getAliases());
}
/**
* @dataProvider provideLocales
*/
public function testGetNames($displayLocale)
{
$locales = array_keys(Locales::getNames($displayLocale));
sort($locales);
// We can't assert on exact list of locale, as there's too many variations.
// The best we can do is to make sure getNames() returns a subset of what getLocales() returns.
$this->assertNotEmpty($locales);
$this->assertEmpty(array_diff($locales, $this->getLocales()));
}
public function testGetNamesDefaultLocale()
{
\Locale::setDefault('de_AT');
$this->assertSame(Locales::getNames('de_AT'), Locales::getNames());
}
/**
* @dataProvider provideLocaleAliases
*/
public function testGetNamesSupportsAliases($alias, $ofLocale)
{
// Can't use assertSame(), because some aliases contain scripts with
// different collation (=order of output) than their aliased locale
// e.g. sr_Latn_ME => sr_ME
$this->assertEquals(Locales::getNames($ofLocale), Locales::getNames($alias));
}
/**
* @dataProvider provideLocales
*/
public function testGetName($displayLocale)
{
$names = Locales::getNames($displayLocale);
foreach ($names as $locale => $name) {
$this->assertSame($name, Locales::getName($locale, $displayLocale));
}
}
public function testGetNameDefaultLocale()
{
\Locale::setDefault('de_AT');
$names = Locales::getNames('de_AT');
foreach ($names as $locale => $name) {
$this->assertSame($name, Locales::getName($locale));
}
}
/**
* @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
*/
public function testGetNameWithInvalidLocale()
{
Locales::getName('foo');
}
public function testGetNameWithAliasLocale()
{
$this->assertSame(Locales::getName('tl_PH'), Locales::getName('fil_PH'));
}
public function testExists()
{
$this->assertTrue(Locales::exists('nl_NL'));
$this->assertTrue(Locales::exists('tl_PH'));
$this->assertTrue(Locales::exists('fil_PH')); // alias for "tl_PH"
$this->assertFalse(Locales::exists('zxx_ZZ'));
}
}