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 Symfony\Component\Translation\PluralizationRules
;
17 * Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
18 * and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
20 * See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
21 * The mozilla code is also interesting to check for.
23 * As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
25 * The goal to cover all languages is to far fetched so this test case is smaller.
27 * @author Clemens Tolboom clemens@build2be.nl
29 class PluralizationRulesTest
extends \PHPUnit_Framework_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($nplural, $langCodes);
42 $this->validateMatrix($nplural, $matrix, false);
46 * @dataProvider successLangcodes
48 public function testLangcodes($nplural, $langCodes)
50 $matrix = $this->generateTestData($nplural, $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')),
66 array('3' , array('be','bs','cs','hr')),
67 array('4' , array('cy','mt', 'sl')),
69 array('6' , array('ar')),
74 * This array should be at least empty within the near future.
76 * This both depends on a complete list trying to add above as understanding
77 * the plural rules of the current failing languages.
79 * @return array with nplural together with langcodes
81 public function failingLangcodes()
84 array('1' , array('fa')),
85 array('2' , array('jbo')),
86 array('3' , array('cbs')),
87 array('4' , array('gd','kw')),
88 array('5' , array('ga')),
94 * We validate only on the plural coverage. Thus the real rules is not tested.
96 * @param string $nplural plural expected
97 * @param array $matrix containing langcodes and their plural index values.
98 * @param boolean $expectSuccess
100 protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
102 foreach ($matrix as $langCode => $data) {
103 $indexes = array_flip($data);
104 if ($expectSuccess) {
105 $this->assertEquals($nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
107 $this->assertNotEquals((int) $nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
112 protected function generateTestData($plural, $langCodes)
115 foreach ($langCodes as $langCode) {
116 for ($count=0; $count<200; $count++
) {
117 $plural = PluralizationRules
::get($count, $langCode);
118 $matrix[$langCode][$count] = $plural;