Debian Codegraph @

lib/3rdParty/composer/symfony/translation-contracts/Test/TranslatorTest.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\Contracts\Translation\Test;
13
14 use PHPUnit\Framework\Attributes\DataProvider;
15 use PHPUnit\Framework\Attributes\RequiresPhpExtension;
16 use PHPUnit\Framework\TestCase;
17 use Symfony\Component\Translation\TranslatableMessage;
18 use Symfony\Contracts\Translation\TranslatorInterface;
19 use Symfony\Contracts\Translation\TranslatorTrait;
20
21 /**
22 * Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
23 * and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms.
24 *
25 * See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
26 * The mozilla code is also interesting to check for.
27 *
28 * As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
29 *
30 * The goal to cover all languages is too far fetched so this test case is smaller.
31 *
32 * @author Clemens Tolboom [email protected]
33 */
34 class TranslatorTest extends TestCase
35 {
36 private string $defaultLocale;
37
38 protected function setUp(): void
39 {
40 $this->defaultLocale = \Locale::getDefault();
41 \Locale::setDefault('en');
42 }
43
44 protected function tearDown(): void
45 {
46 \Locale::setDefault($this->defaultLocale);
47 }
48
49 public function getTranslator(): TranslatorInterface
50 {
51 return new class implements TranslatorInterface {
52 use TranslatorTrait;
53 };
54 }
55
56 /**
57 * @dataProvider getTransTests
58 */
59 #[DataProvider('getTransTests')]
60 public function testTrans($expected, $id, $parameters)
61 {
62 $translator = $this->getTranslator();
63
64 $this->assertEquals($expected, $translator->trans($id, $parameters));
65 }
66
67 /**
68 * @dataProvider getTransChoiceTests
69 */
70 #[DataProvider('getTransChoiceTests')]
71 public function testTransChoiceWithExplicitLocale($expected, $id, $number)
72 {
73 $translator = $this->getTranslator();
74
75 $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number]));
76 }
77
78 /**
79 * @requires extension intl
80 *
81 * @dataProvider getTransChoiceTests
82 */
83 #[DataProvider('getTransChoiceTests')]
84 #[RequiresPhpExtension('intl')]
85 public function testTransChoiceWithDefaultLocale($expected, $id, $number)
86 {
87 $translator = $this->getTranslator();
88
89 $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number]));
90 }
91
92 /**
93 * @dataProvider getTransChoiceTests
94 */
95 #[DataProvider('getTransChoiceTests')]
96 public function testTransChoiceWithEnUsPosix($expected, $id, $number)
97 {
98 $translator = $this->getTranslator();
99 $translator->setLocale('en_US_POSIX');
100
101 $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number]));
102 }
103
104 public function testGetSetLocale()
105 {
106 $translator = $this->getTranslator();
107
108 $this->assertEquals('en', $translator->getLocale());
109 }
110
111 /**
112 * @requires extension intl
113 */
114 #[RequiresPhpExtension('intl')]
115 public function testGetLocaleReturnsDefaultLocaleIfNotSet()
116 {
117 $translator = $this->getTranslator();
118
119 \Locale::setDefault('pt_BR');
120 $this->assertEquals('pt_BR', $translator->getLocale());
121
122 \Locale::setDefault('en');
123 $this->assertEquals('en', $translator->getLocale());
124 }
125
126 public static function getTransTests()
127 {
128 yield ['Symfony is great!', 'Symfony is great!', []];
129 yield ['Symfony is awesome!', 'Symfony is %what%!', ['%what%' => 'awesome']];
130
131 if (class_exists(TranslatableMessage::class)) {
132 yield ['He said "Symfony is awesome!".', 'He said "%what%".', ['%what%' => new TranslatableMessage('Symfony is %what%!', ['%what%' => 'awesome'])]];
133 }
134 }
135
136 public static function getTransChoiceTests()
137 {
138 return [
139 ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
140 ['There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1],
141 ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10],
142 ['There are 0 apples', 'There is 1 apple|There are %count% apples', 0],
143 ['There is 1 apple', 'There is 1 apple|There are %count% apples', 1],
144 ['There are 10 apples', 'There is 1 apple|There are %count% apples', 10],
145 // custom validation messages may be coded with a fixed value
146 ['There are 2 apples', 'There are 2 apples', 2],
147 ];
148 }
149
150 /**
151 * @dataProvider getInterval
152 */
153 #[DataProvider('getInterval')]
154 public function testInterval($expected, $number, $interval)
155 {
156 $translator = $this->getTranslator();
157
158 $this->assertEquals($expected, $translator->trans($interval.' foo|[1,Inf[ bar', ['%count%' => $number]));
159 }
160
161 public static function getInterval()
162 {
163 return [
164 ['foo', 3, '{1,2, 3 ,4}'],
165 ['bar', 10, '{1,2, 3 ,4}'],
166 ['bar', 3, '[1,2]'],
167 ['foo', 1, '[1,2]'],
168 ['foo', 2, '[1,2]'],
169 ['bar', 1, ']1,2['],
170 ['bar', 2, ']1,2['],
171 ['foo', log(0), '[-Inf,2['],
172 ['foo', -log(0), '[-2,+Inf]'],
173 ];
174 }
175
176 /**
177 * @dataProvider getChooseTests
178 */
179 #[DataProvider('getChooseTests')]
180 public function testChoose($expected, $id, $number, $locale = null)
181 {
182 $translator = $this->getTranslator();
183
184 $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number], null, $locale));
185 }
186
187 public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
188 {
189 $translator = $this->getTranslator();
190
191 $this->assertEquals('There are two apples', $translator->trans('There are two apples', ['%count%' => 2]));
192 }
193
194 /**
195 * @dataProvider getNonMatchingMessages
196 */
197 #[DataProvider('getNonMatchingMessages')]
198 public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
199 {
200 $translator = $this->getTranslator();
201
202 $this->expectException(\InvalidArgumentException::class);
203
204 $translator->trans($id, ['%count%' => $number]);
205 }
206
207 public static function getNonMatchingMessages()
208 {
209 return [
210 ['{0} There are no apples|{1} There is one apple', 2],
211 ['{1} There is one apple|]1,Inf] There are %count% apples', 0],
212 ['{1} There is one apple|]2,Inf] There are %count% apples', 2],
213 ['{0} There are no apples|There is one apple', 2],
214 ];
215 }
216
217 public static function getChooseTests()
218 {
219 return [
220 ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
221 ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
222 ['There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
223
224 ['There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1],
225
226 ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10],
227 ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10],
228 ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10],
229
230 ['There are 0 apples', 'There is one apple|There are %count% apples', 0],
231 ['There is one apple', 'There is one apple|There are %count% apples', 1],
232 ['There are 10 apples', 'There is one apple|There are %count% apples', 10],
233
234 ['There are 0 apples', 'one: There is one apple|more: There are %count% apples', 0],
235 ['There is one apple', 'one: There is one apple|more: There are %count% apples', 1],
236 ['There are 10 apples', 'one: There is one apple|more: There are %count% apples', 10],
237
238 ['There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0],
239 ['There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1],
240 ['There are 10 apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10],
241
242 ['', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0],
243 ['', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1],
244
245 // Indexed only tests which are Gettext PoFile* compatible strings.
246 ['There are 0 apples', 'There is one apple|There are %count% apples', 0],
247 ['There is one apple', 'There is one apple|There are %count% apples', 1],
248 ['There are 2 apples', 'There is one apple|There are %count% apples', 2],
249
250 // Tests for float numbers
251 ['There is almost one apple', '{0} There are no apples|]0,1[ There is almost one apple|{1} There is one apple|[1,Inf] There is more than one apple', 0.7],
252 ['There is one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1],
253 ['There is more than one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1.7],
254 ['There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0],
255 ['There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0.0],
256 ['There are no apples', '{0.0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0],
257
258 // Test texts with new-lines
259 // with double-quotes and \n in id & double-quotes and actual newlines in text
260 ["This is a text with a\n new-line in it. Selector = 0.", '{0}This is a text with a
261 new-line in it. Selector = 0.|{1}This is a text with a
262 new-line in it. Selector = 1.|[1,Inf]This is a text with a
263 new-line in it. Selector > 1.', 0],
264 // with double-quotes and \n in id and single-quotes and actual newlines in text
265 ["This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a
266 new-line in it. Selector = 0.|{1}This is a text with a
267 new-line in it. Selector = 1.|[1,Inf]This is a text with a
268 new-line in it. Selector > 1.', 1],
269 ["This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a
270 new-line in it. Selector = 0.|{1}This is a text with a
271 new-line in it. Selector = 1.|[1,Inf]This is a text with a
272 new-line in it. Selector > 1.', 5],
273 // with double-quotes and id split across lines
274 ['This is a text with a
275 new-line in it. Selector = 1.', '{0}This is a text with a
276 new-line in it. Selector = 0.|{1}This is a text with a
277 new-line in it. Selector = 1.|[1,Inf]This is a text with a
278 new-line in it. Selector > 1.', 1],
279 // with single-quotes and id split across lines
280 ['This is a text with a
281 new-line in it. Selector > 1.', '{0}This is a text with a
282 new-line in it. Selector = 0.|{1}This is a text with a
283 new-line in it. Selector = 1.|[1,Inf]This is a text with a
284 new-line in it. Selector > 1.', 5],
285 // with single-quotes and \n in text
286 ['This is a text with a\nnew-line in it. Selector = 0.', '{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.', 0],
287 // with double-quotes and id split across lines
288 ["This is a text with a\nnew-line in it. Selector = 1.", "{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.", 1],
289 // escape pipe
290 ['This is a text with | in it. Selector = 0.', '{0}This is a text with || in it. Selector = 0.|{1}This is a text with || in it. Selector = 1.', 0],
291 // Empty plural set (2 plural forms) from a .PO file
292 ['', '|', 1],
293 // Empty plural set (3 plural forms) from a .PO file
294 ['', '||', 1],
295
296 // Floating values
297 ['1.5 liters', '%count% liter|%count% liters', 1.5],
298 ['1.5 litre', '%count% litre|%count% litres', 1.5, 'fr'],
299
300 // Negative values
301 ['-1 degree', '%count% degree|%count% degrees', -1],
302 ['-1 degré', '%count% degré|%count% degrés', -1],
303 ['-1.5 degrees', '%count% degree|%count% degrees', -1.5],
304 ['-1.5 degré', '%count% degré|%count% degrés', -1.5, 'fr'],
305 ['-2 degrees', '%count% degree|%count% degrees', -2],
306 ['-2 degrés', '%count% degré|%count% degrés', -2],
307 ];
308 }
309
310 /**
311 * @dataProvider failingLangcodes
312 */
313 #[DataProvider('failingLangcodes')]
314 public function testFailedLangcodes($nplural, $langCodes)
315 {
316 $matrix = $this->generateTestData($langCodes);
317 $this->validateMatrix($nplural, $matrix, false);
318 }
319
320 /**
321 * @dataProvider successLangcodes
322 */
323 #[DataProvider('successLangcodes')]
324 public function testLangcodes($nplural, $langCodes)
325 {
326 $matrix = $this->generateTestData($langCodes);
327 $this->validateMatrix($nplural, $matrix);
328 }
329
330 /**
331 * This array should contain all currently known langcodes.
332 *
333 * As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
334 */
335 public static function successLangcodes(): array
336 {
337 return [
338 ['1', ['ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky']],
339 ['2', ['nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM', 'en_US_POSIX']],
340 ['3', ['be', 'bs', 'cs', 'hr']],
341 ['4', ['cy', 'mt', 'sl']],
342 ['6', ['ar']],
343 ];
344 }
345
346 /**
347 * This array should be at least empty within the near future.
348 *
349 * This both depends on a complete list trying to add above as understanding
350 * the plural rules of the current failing languages.
351 *
352 * @return array With nplural together with langcodes
353 */
354 public static function failingLangcodes(): array
355 {
356 return [
357 ['1', ['fa']],
358 ['2', ['jbo']],
359 ['3', ['cbs']],
360 ['4', ['gd', 'kw']],
361 ['5', ['ga']],
362 ];
363 }
364
365 /**
366 * We validate only on the plural coverage. Thus the real rules is not tested.
367 *
368 * @param string $nplural Plural expected
369 * @param array $matrix Containing langcodes and their plural index values
370 */
371 protected function validateMatrix(string $nplural, array $matrix, bool $expectSuccess = true)
372 {
373 foreach ($matrix as $langCode => $data) {
374 $indexes = array_flip($data);
375 if ($expectSuccess) {
376 $this->assertCount($nplural, $indexes, "Langcode '$langCode' has '$nplural' plural forms.");
377 } else {
378 $this->assertNotCount($nplural, $indexes, "Langcode '$langCode' has '$nplural' plural forms.");
379 }
380 }
381 }
382
383 protected function generateTestData($langCodes)
384 {
385 $translator = new class {
386 use TranslatorTrait {
387 getPluralizationRule as public;
388 }
389 };
390
391 $matrix = [];
392 foreach ($langCodes as $langCode) {
393 for ($count = 0; $count < 200; ++$count) {
394 $plural = $translator->getPluralizationRule($count, $langCode);
395 $matrix[$langCode][$count] = $plural;
396 }
397 }
398
399 return $matrix;
400 }
401 }