TranslatorTest.php
234 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Contracts\Translation\Test; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\PHPUnit\Framework\TestCase; |
| 5 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 6 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorTrait; |
| 7 | class TranslatorTest extends TestCase |
| 8 | { |
| 9 | private $defaultLocale; |
| 10 | protected function setUp() : void |
| 11 | { |
| 12 | $this->defaultLocale = \Locale::getDefault(); |
| 13 | \Locale::setDefault('en'); |
| 14 | } |
| 15 | protected function tearDown() : void |
| 16 | { |
| 17 | \Locale::setDefault($this->defaultLocale); |
| 18 | } |
| 19 | public function getTranslator() |
| 20 | { |
| 21 | return new class implements TranslatorInterface |
| 22 | { |
| 23 | use TranslatorTrait; |
| 24 | }; |
| 25 | } |
| 26 | public function testTrans($expected, $id, $parameters) |
| 27 | { |
| 28 | $translator = $this->getTranslator(); |
| 29 | $this->assertEquals($expected, $translator->trans($id, $parameters)); |
| 30 | } |
| 31 | public function testTransChoiceWithExplicitLocale($expected, $id, $number) |
| 32 | { |
| 33 | $translator = $this->getTranslator(); |
| 34 | $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number])); |
| 35 | } |
| 36 | public function testTransChoiceWithDefaultLocale($expected, $id, $number) |
| 37 | { |
| 38 | $translator = $this->getTranslator(); |
| 39 | $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number])); |
| 40 | } |
| 41 | public function testTransChoiceWithEnUsPosix($expected, $id, $number) |
| 42 | { |
| 43 | $translator = $this->getTranslator(); |
| 44 | $translator->setLocale('en_US_POSIX'); |
| 45 | $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number])); |
| 46 | } |
| 47 | public function testGetSetLocale() |
| 48 | { |
| 49 | $translator = $this->getTranslator(); |
| 50 | $this->assertEquals('en', $translator->getLocale()); |
| 51 | } |
| 52 | public function testGetLocaleReturnsDefaultLocaleIfNotSet() |
| 53 | { |
| 54 | $translator = $this->getTranslator(); |
| 55 | \Locale::setDefault('pt_BR'); |
| 56 | $this->assertEquals('pt_BR', $translator->getLocale()); |
| 57 | \Locale::setDefault('en'); |
| 58 | $this->assertEquals('en', $translator->getLocale()); |
| 59 | } |
| 60 | public static function getTransTests() |
| 61 | { |
| 62 | return [['Symfony is great!', 'Symfony is great!', []], ['Symfony is awesome!', 'Symfony is %what%!', ['%what%' => 'awesome']]]; |
| 63 | } |
| 64 | public static function getTransChoiceTests() |
| 65 | { |
| 66 | return [ |
| 67 | ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0], |
| 68 | ['There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1], |
| 69 | ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10], |
| 70 | ['There are 0 apples', 'There is 1 apple|There are %count% apples', 0], |
| 71 | ['There is 1 apple', 'There is 1 apple|There are %count% apples', 1], |
| 72 | ['There are 10 apples', 'There is 1 apple|There are %count% apples', 10], |
| 73 | // custom validation messages may be coded with a fixed value |
| 74 | ['There are 2 apples', 'There are 2 apples', 2], |
| 75 | ]; |
| 76 | } |
| 77 | public function testInterval($expected, $number, $interval) |
| 78 | { |
| 79 | $translator = $this->getTranslator(); |
| 80 | $this->assertEquals($expected, $translator->trans($interval . ' foo|[1,Inf[ bar', ['%count%' => $number])); |
| 81 | } |
| 82 | public static function getInterval() |
| 83 | { |
| 84 | return [['foo', 3, '{1,2, 3 ,4}'], ['bar', 10, '{1,2, 3 ,4}'], ['bar', 3, '[1,2]'], ['foo', 1, '[1,2]'], ['foo', 2, '[1,2]'], ['bar', 1, ']1,2['], ['bar', 2, ']1,2['], ['foo', \log(0), '[-Inf,2['], ['foo', -\log(0), '[-2,+Inf]']]; |
| 85 | } |
| 86 | public function testChoose($expected, $id, $number, $locale = null) |
| 87 | { |
| 88 | $translator = $this->getTranslator(); |
| 89 | $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number], null, $locale)); |
| 90 | } |
| 91 | public function testReturnMessageIfExactlyOneStandardRuleIsGiven() |
| 92 | { |
| 93 | $translator = $this->getTranslator(); |
| 94 | $this->assertEquals('There are two apples', $translator->trans('There are two apples', ['%count%' => 2])); |
| 95 | } |
| 96 | public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number) |
| 97 | { |
| 98 | $this->expectException(\InvalidArgumentException::class); |
| 99 | $translator = $this->getTranslator(); |
| 100 | $translator->trans($id, ['%count%' => $number]); |
| 101 | } |
| 102 | public static function getNonMatchingMessages() |
| 103 | { |
| 104 | return [['{0} There are no apples|{1} There is one apple', 2], ['{1} There is one apple|]1,Inf] There are %count% apples', 0], ['{1} There is one apple|]2,Inf] There are %count% apples', 2], ['{0} There are no apples|There is one apple', 2]]; |
| 105 | } |
| 106 | public static function getChooseTests() |
| 107 | { |
| 108 | return [ |
| 109 | ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0], |
| 110 | ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0], |
| 111 | ['There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0], |
| 112 | ['There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1], |
| 113 | ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10], |
| 114 | ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10], |
| 115 | ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10], |
| 116 | ['There are 0 apples', 'There is one apple|There are %count% apples', 0], |
| 117 | ['There is one apple', 'There is one apple|There are %count% apples', 1], |
| 118 | ['There are 10 apples', 'There is one apple|There are %count% apples', 10], |
| 119 | ['There are 0 apples', 'one: There is one apple|more: There are %count% apples', 0], |
| 120 | ['There is one apple', 'one: There is one apple|more: There are %count% apples', 1], |
| 121 | ['There are 10 apples', 'one: There is one apple|more: There are %count% apples', 10], |
| 122 | ['There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0], |
| 123 | ['There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1], |
| 124 | ['There are 10 apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10], |
| 125 | ['', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0], |
| 126 | ['', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1], |
| 127 | // Indexed only tests which are Gettext PoFile* compatible strings. |
| 128 | ['There are 0 apples', 'There is one apple|There are %count% apples', 0], |
| 129 | ['There is one apple', 'There is one apple|There are %count% apples', 1], |
| 130 | ['There are 2 apples', 'There is one apple|There are %count% apples', 2], |
| 131 | // Tests for float numbers |
| 132 | ['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], |
| 133 | ['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], |
| 134 | ['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], |
| 135 | ['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], |
| 136 | ['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], |
| 137 | ['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], |
| 138 | // Test texts with new-lines |
| 139 | // with double-quotes and \n in id & double-quotes and actual newlines in text |
| 140 | ["This is a text with a\n new-line in it. Selector = 0.", '{0}This is a text with a |
| 141 | new-line in it. Selector = 0.|{1}This is a text with a |
| 142 | new-line in it. Selector = 1.|[1,Inf]This is a text with a |
| 143 | new-line in it. Selector > 1.', 0], |
| 144 | // with double-quotes and \n in id and single-quotes and actual newlines in text |
| 145 | ["This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a |
| 146 | new-line in it. Selector = 0.|{1}This is a text with a |
| 147 | new-line in it. Selector = 1.|[1,Inf]This is a text with a |
| 148 | new-line in it. Selector > 1.', 1], |
| 149 | ["This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a |
| 150 | new-line in it. Selector = 0.|{1}This is a text with a |
| 151 | new-line in it. Selector = 1.|[1,Inf]This is a text with a |
| 152 | new-line in it. Selector > 1.', 5], |
| 153 | // with double-quotes and id split across lines |
| 154 | ['This is a text with a |
| 155 | new-line in it. Selector = 1.', '{0}This is a text with a |
| 156 | new-line in it. Selector = 0.|{1}This is a text with a |
| 157 | new-line in it. Selector = 1.|[1,Inf]This is a text with a |
| 158 | new-line in it. Selector > 1.', 1], |
| 159 | // with single-quotes and id split across lines |
| 160 | ['This is a text with a |
| 161 | new-line in it. Selector > 1.', '{0}This is a text with a |
| 162 | new-line in it. Selector = 0.|{1}This is a text with a |
| 163 | new-line in it. Selector = 1.|[1,Inf]This is a text with a |
| 164 | new-line in it. Selector > 1.', 5], |
| 165 | // with single-quotes and \n in text |
| 166 | ['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], |
| 167 | // with double-quotes and id split across lines |
| 168 | ["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], |
| 169 | // escape pipe |
| 170 | ['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], |
| 171 | // Empty plural set (2 plural forms) from a .PO file |
| 172 | ['', '|', 1], |
| 173 | // Empty plural set (3 plural forms) from a .PO file |
| 174 | ['', '||', 1], |
| 175 | // Floating values |
| 176 | ['1.5 liters', '%count% liter|%count% liters', 1.5], |
| 177 | ['1.5 litre', '%count% litre|%count% litres', 1.5, 'fr'], |
| 178 | // Negative values |
| 179 | ['-1 degree', '%count% degree|%count% degrees', -1], |
| 180 | ['-1 degré', '%count% degré|%count% degrés', -1], |
| 181 | ['-1.5 degrees', '%count% degree|%count% degrees', -1.5], |
| 182 | ['-1.5 degré', '%count% degré|%count% degrés', -1.5, 'fr'], |
| 183 | ['-2 degrees', '%count% degree|%count% degrees', -2], |
| 184 | ['-2 degrés', '%count% degré|%count% degrés', -2], |
| 185 | ]; |
| 186 | } |
| 187 | public function testFailedLangcodes($nplural, $langCodes) |
| 188 | { |
| 189 | $matrix = $this->generateTestData($langCodes); |
| 190 | $this->validateMatrix($nplural, $matrix, \false); |
| 191 | } |
| 192 | public function testLangcodes($nplural, $langCodes) |
| 193 | { |
| 194 | $matrix = $this->generateTestData($langCodes); |
| 195 | $this->validateMatrix($nplural, $matrix); |
| 196 | } |
| 197 | public static function successLangcodes() |
| 198 | { |
| 199 | return [['1', ['ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky']], ['2', ['nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM', 'en_US_POSIX']], ['3', ['be', 'bs', 'cs', 'hr']], ['4', ['cy', 'mt', 'sl']], ['6', ['ar']]]; |
| 200 | } |
| 201 | public static function failingLangcodes() |
| 202 | { |
| 203 | return [['1', ['fa']], ['2', ['jbo']], ['3', ['cbs']], ['4', ['gd', 'kw']], ['5', ['ga']]]; |
| 204 | } |
| 205 | protected function validateMatrix($nplural, $matrix, $expectSuccess = \true) |
| 206 | { |
| 207 | foreach ($matrix as $langCode => $data) { |
| 208 | $indexes = \array_flip($data); |
| 209 | if ($expectSuccess) { |
| 210 | $this->assertCount($nplural, $indexes, "Langcode '{$langCode}' has '{$nplural}' plural forms."); |
| 211 | } else { |
| 212 | $this->assertNotEquals((int) $nplural, \count($indexes), "Langcode '{$langCode}' has '{$nplural}' plural forms."); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | protected function generateTestData($langCodes) |
| 217 | { |
| 218 | $translator = new class |
| 219 | { |
| 220 | use TranslatorTrait { |
| 221 | getPluralizationRule as public; |
| 222 | } |
| 223 | }; |
| 224 | $matrix = []; |
| 225 | foreach ($langCodes as $langCode) { |
| 226 | for ($count = 0; $count < 200; ++$count) { |
| 227 | $plural = $translator->getPluralizationRule($count, $langCode); |
| 228 | $matrix[$langCode][$count] = $plural; |
| 229 | } |
| 230 | } |
| 231 | return $matrix; |
| 232 | } |
| 233 | } |
| 234 |