8a6723ea9cb1a8bb58a2ada1a76ac4795599e3ff
[yaffs-website] / vendor / symfony / translation / Tests / PluralizationRulesTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\PluralizationRules;
16
17 /**
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.
20  *
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.
23  *
24  * As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
25  *
26  * The goal to cover all languages is to far fetched so this test case is smaller.
27  *
28  * @author Clemens Tolboom clemens@build2be.nl
29  */
30 class PluralizationRulesTest extends TestCase
31 {
32     /**
33      * We test failed langcode here.
34      *
35      * TODO: The languages mentioned in the data provide need to get fixed somehow within PluralizationRules.
36      *
37      * @dataProvider failingLangcodes
38      */
39     public function testFailedLangcodes($nplural, $langCodes)
40     {
41         $matrix = $this->generateTestData($langCodes);
42         $this->validateMatrix($nplural, $matrix, false);
43     }
44
45     /**
46      * @dataProvider successLangcodes
47      */
48     public function testLangcodes($nplural, $langCodes)
49     {
50         $matrix = $this->generateTestData($langCodes);
51         $this->validateMatrix($nplural, $matrix);
52     }
53
54     /**
55      * This array should contain all currently known langcodes.
56      *
57      * As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
58      *
59      * @return array
60      */
61     public function successLangcodes()
62     {
63         return array(
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')),
69         );
70     }
71
72     /**
73      * This array should be at least empty within the near future.
74      *
75      * This both depends on a complete list trying to add above as understanding
76      * the plural rules of the current failing languages.
77      *
78      * @return array with nplural together with langcodes
79      */
80     public function failingLangcodes()
81     {
82         return array(
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')),
88         );
89     }
90
91     /**
92      * We validate only on the plural coverage. Thus the real rules is not tested.
93      *
94      * @param string $nplural       plural expected
95      * @param array  $matrix        containing langcodes and their plural index values
96      * @param bool   $expectSuccess
97      */
98     protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
99     {
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.");
104             } else {
105                 $this->assertNotEquals((int) $nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
106             }
107         }
108     }
109
110     protected function generateTestData($langCodes)
111     {
112         $matrix = array();
113         foreach ($langCodes as $langCode) {
114             for ($count = 0; $count < 200; ++$count) {
115                 $plural = PluralizationRules::get($count, $langCode);
116                 $matrix[$langCode][$count] = $plural;
117             }
118         }
119
120         return $matrix;
121     }
122 }