4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\PluralizationRules;
18 * Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
19 * and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms.
21 * See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
22 * The mozilla code is also interesting to check for.
24 * As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
26 * The goal to cover all languages is to far fetched so this test case is smaller.
28 * @author Clemens Tolboom clemens@build2be.nl
30 class PluralizationRulesTest extends TestCase
33 * We test failed langcode here.
35 * TODO: The languages mentioned in the data provide need to get fixed somehow within PluralizationRules.
37 * @dataProvider failingLangcodes
39 public function testFailedLangcodes($nplural, $langCodes)
41 $matrix = $this->generateTestData($langCodes);
42 $this->validateMatrix($nplural, $matrix, false);
46 * @dataProvider successLangcodes
48 public function testLangcodes($nplural, $langCodes)
50 $matrix = $this->generateTestData($langCodes);
51 $this->validateMatrix($nplural, $matrix);
55 * This array should contain all currently known langcodes.
57 * As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
61 public function successLangcodes()
64 array('1', array('ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky')),
65 array('2', array('nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM')),
66 array('3', array('be', 'bs', 'cs', 'hr')),
67 array('4', array('cy', 'mt', 'sl')),
68 array('6', array('ar')),
73 * This array should be at least empty within the near future.
75 * This both depends on a complete list trying to add above as understanding
76 * the plural rules of the current failing languages.
78 * @return array with nplural together with langcodes
80 public function failingLangcodes()
83 array('1', array('fa')),
84 array('2', array('jbo')),
85 array('3', array('cbs')),
86 array('4', array('gd', 'kw')),
87 array('5', array('ga')),
92 * We validate only on the plural coverage. Thus the real rules is not tested.
94 * @param string $nplural plural expected
95 * @param array $matrix containing langcodes and their plural index values
96 * @param bool $expectSuccess
98 protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
100 foreach ($matrix as $langCode => $data) {
101 $indexes = array_flip($data);
102 if ($expectSuccess) {
103 $this->assertEquals($nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
105 $this->assertNotEquals((int) $nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
110 protected function generateTestData($langCodes)
113 foreach ($langCodes as $langCode) {
114 for ($count = 0; $count < 200; ++$count) {
115 $plural = PluralizationRules::get($count, $langCode);
116 $matrix[$langCode][$count] = $plural;