| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 146 |
['There are 2 apples', 'There are 2 apples', 2], |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 259 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 292 |
['', '|', 1], |
| 293 |
|
| 294 |
['', '||', 1], |
| 295 |
|
| 296 |
|
| 297 |
['1.5 liters', '%count% liter|%count% liters', 1.5], |
| 298 |
['1.5 litre', '%count% litre|%count% litres', 1.5, 'fr'], |
| 299 |
|
| 300 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 332 |
|
| 333 |
|
| 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 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 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 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 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 |
} |