diff options
Diffstat (limited to 'vendor/symfony/translation/Symfony/Component/Translation/Tests/TranslatorTest.php')
-rw-r--r-- | vendor/symfony/translation/Symfony/Component/Translation/Tests/TranslatorTest.php | 306 |
1 files changed, 306 insertions, 0 deletions
diff --git a/vendor/symfony/translation/Symfony/Component/Translation/Tests/TranslatorTest.php b/vendor/symfony/translation/Symfony/Component/Translation/Tests/TranslatorTest.php new file mode 100644 index 00000000..fb843d72 --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/Tests/TranslatorTest.php | |||
@@ -0,0 +1,306 @@ | |||
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 Symfony\Component\Translation\Translator; | ||
15 | use Symfony\Component\Translation\MessageSelector; | ||
16 | use Symfony\Component\Translation\Loader\ArrayLoader; | ||
17 | |||
18 | class TranslatorTest extends \PHPUnit_Framework_TestCase | ||
19 | { | ||
20 | public function testSetGetLocale() | ||
21 | { | ||
22 | $translator = new Translator('en', new MessageSelector()); | ||
23 | |||
24 | $this->assertEquals('en', $translator->getLocale()); | ||
25 | |||
26 | $translator->setLocale('fr'); | ||
27 | $this->assertEquals('fr', $translator->getLocale()); | ||
28 | } | ||
29 | |||
30 | public function testSetFallbackLocales() | ||
31 | { | ||
32 | $translator = new Translator('en', new MessageSelector()); | ||
33 | $translator->addLoader('array', new ArrayLoader()); | ||
34 | $translator->addResource('array', array('foo' => 'foofoo'), 'en'); | ||
35 | $translator->addResource('array', array('bar' => 'foobar'), 'fr'); | ||
36 | |||
37 | // force catalogue loading | ||
38 | $translator->trans('bar'); | ||
39 | |||
40 | $translator->setFallbackLocales(array('fr')); | ||
41 | $this->assertEquals('foobar', $translator->trans('bar')); | ||
42 | } | ||
43 | |||
44 | public function testSetFallbackLocalesMultiple() | ||
45 | { | ||
46 | $translator = new Translator('en', new MessageSelector()); | ||
47 | $translator->addLoader('array', new ArrayLoader()); | ||
48 | $translator->addResource('array', array('foo' => 'foo (en)'), 'en'); | ||
49 | $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr'); | ||
50 | |||
51 | // force catalogue loading | ||
52 | $translator->trans('bar'); | ||
53 | |||
54 | $translator->setFallbackLocales(array('fr_FR', 'fr')); | ||
55 | $this->assertEquals('bar (fr)', $translator->trans('bar')); | ||
56 | } | ||
57 | |||
58 | public function testTransWithFallbackLocale() | ||
59 | { | ||
60 | $translator = new Translator('fr_FR', new MessageSelector()); | ||
61 | $translator->addLoader('array', new ArrayLoader()); | ||
62 | $translator->addResource('array', array('foo' => 'foofoo'), 'en_US'); | ||
63 | $translator->addResource('array', array('bar' => 'foobar'), 'en'); | ||
64 | |||
65 | $translator->setFallbackLocales(array('en')); | ||
66 | |||
67 | $this->assertEquals('foobar', $translator->trans('bar')); | ||
68 | } | ||
69 | |||
70 | public function testAddResourceAfterTrans() | ||
71 | { | ||
72 | $translator = new Translator('fr', new MessageSelector()); | ||
73 | $translator->addLoader('array', new ArrayLoader()); | ||
74 | |||
75 | $translator->setFallbackLocale(array('en')); | ||
76 | |||
77 | $translator->addResource('array', array('foo' => 'foofoo'), 'en'); | ||
78 | $this->assertEquals('foofoo', $translator->trans('foo')); | ||
79 | |||
80 | $translator->addResource('array', array('bar' => 'foobar'), 'en'); | ||
81 | $this->assertEquals('foobar', $translator->trans('bar')); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * @dataProvider getTransFileTests | ||
86 | * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException | ||
87 | */ | ||
88 | public function testTransWithoutFallbackLocaleFile($format, $loader) | ||
89 | { | ||
90 | $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader; | ||
91 | $translator = new Translator('en', new MessageSelector()); | ||
92 | $translator->addLoader($format, new $loaderClass()); | ||
93 | $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en'); | ||
94 | $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en'); | ||
95 | |||
96 | // force catalogue loading | ||
97 | $translator->trans('foo'); | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * @dataProvider getTransFileTests | ||
102 | */ | ||
103 | public function testTransWithFallbackLocaleFile($format, $loader) | ||
104 | { | ||
105 | $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader; | ||
106 | $translator = new Translator('en_GB', new MessageSelector()); | ||
107 | $translator->addLoader($format, new $loaderClass()); | ||
108 | $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en_GB'); | ||
109 | $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en', 'resources'); | ||
110 | |||
111 | $this->assertEquals('bar', $translator->trans('foo', array(), 'resources')); | ||
112 | } | ||
113 | |||
114 | public function testTransWithFallbackLocaleBis() | ||
115 | { | ||
116 | $translator = new Translator('en_US', new MessageSelector()); | ||
117 | $translator->addLoader('array', new ArrayLoader()); | ||
118 | $translator->addResource('array', array('foo' => 'foofoo'), 'en_US'); | ||
119 | $translator->addResource('array', array('bar' => 'foobar'), 'en'); | ||
120 | $this->assertEquals('foobar', $translator->trans('bar')); | ||
121 | } | ||
122 | |||
123 | public function testTransWithFallbackLocaleTer() | ||
124 | { | ||
125 | $translator = new Translator('fr_FR', new MessageSelector()); | ||
126 | $translator->addLoader('array', new ArrayLoader()); | ||
127 | $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US'); | ||
128 | $translator->addResource('array', array('bar' => 'bar (en)'), 'en'); | ||
129 | |||
130 | $translator->setFallbackLocales(array('en_US', 'en')); | ||
131 | |||
132 | $this->assertEquals('foo (en_US)', $translator->trans('foo')); | ||
133 | $this->assertEquals('bar (en)', $translator->trans('bar')); | ||
134 | } | ||
135 | |||
136 | public function testTransNonExistentWithFallback() | ||
137 | { | ||
138 | $translator = new Translator('fr', new MessageSelector()); | ||
139 | $translator->setFallbackLocales(array('en')); | ||
140 | $translator->addLoader('array', new ArrayLoader()); | ||
141 | $this->assertEquals('non-existent', $translator->trans('non-existent')); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * @expectedException RuntimeException | ||
146 | */ | ||
147 | public function testWhenAResourceHasNoRegisteredLoader() | ||
148 | { | ||
149 | $translator = new Translator('en', new MessageSelector()); | ||
150 | $translator->addResource('array', array('foo' => 'foofoo'), 'en'); | ||
151 | |||
152 | $translator->trans('foo'); | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * @dataProvider getTransTests | ||
157 | */ | ||
158 | public function testTrans($expected, $id, $translation, $parameters, $locale, $domain) | ||
159 | { | ||
160 | $translator = new Translator('en', new MessageSelector()); | ||
161 | $translator->addLoader('array', new ArrayLoader()); | ||
162 | $translator->addResource('array', array((string) $id => $translation), $locale, $domain); | ||
163 | |||
164 | $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale)); | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * @dataProvider getFlattenedTransTests | ||
169 | */ | ||
170 | public function testFlattenedTrans($expected, $messages, $id) | ||
171 | { | ||
172 | $translator = new Translator('en', new MessageSelector()); | ||
173 | $translator->addLoader('array', new ArrayLoader()); | ||
174 | $translator->addResource('array', $messages, 'fr', ''); | ||
175 | |||
176 | $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr')); | ||
177 | } | ||
178 | |||
179 | /** | ||
180 | * @dataProvider getTransChoiceTests | ||
181 | */ | ||
182 | public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain) | ||
183 | { | ||
184 | $translator = new Translator('en', new MessageSelector()); | ||
185 | $translator->addLoader('array', new ArrayLoader()); | ||
186 | $translator->addResource('array', array((string) $id => $translation), $locale, $domain); | ||
187 | |||
188 | $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale)); | ||
189 | } | ||
190 | |||
191 | public function getTransFileTests() | ||
192 | { | ||
193 | return array( | ||
194 | array('csv', 'CsvFileLoader'), | ||
195 | array('ini', 'IniFileLoader'), | ||
196 | array('mo', 'MoFileLoader'), | ||
197 | array('po', 'PoFileLoader'), | ||
198 | array('php', 'PhpFileLoader'), | ||
199 | array('ts', 'QtFileLoader'), | ||
200 | array('xlf', 'XliffFileLoader'), | ||
201 | array('yml', 'YamlFileLoader'), | ||
202 | ); | ||
203 | } | ||
204 | |||
205 | public function getTransTests() | ||
206 | { | ||
207 | return array( | ||
208 | array('Symfony2 est super !', 'Symfony2 is great!', 'Symfony2 est super !', array(), 'fr', ''), | ||
209 | array('Symfony2 est awesome !', 'Symfony2 is %what%!', 'Symfony2 est %what% !', array('%what%' => 'awesome'), 'fr', ''), | ||
210 | array('Symfony2 est super !', new String('Symfony2 is great!'), 'Symfony2 est super !', array(), 'fr', ''), | ||
211 | ); | ||
212 | } | ||
213 | |||
214 | public function getFlattenedTransTests() | ||
215 | { | ||
216 | $messages = array( | ||
217 | 'symfony2' => array( | ||
218 | 'is' => array( | ||
219 | 'great' => 'Symfony2 est super!' | ||
220 | ) | ||
221 | ), | ||
222 | 'foo' => array( | ||
223 | 'bar' => array( | ||
224 | 'baz' => 'Foo Bar Baz' | ||
225 | ), | ||
226 | 'baz' => 'Foo Baz', | ||
227 | ), | ||
228 | ); | ||
229 | |||
230 | return array( | ||
231 | array('Symfony2 est super!', $messages, 'symfony2.is.great'), | ||
232 | array('Foo Bar Baz', $messages, 'foo.bar.baz'), | ||
233 | array('Foo Baz', $messages, 'foo.baz'), | ||
234 | ); | ||
235 | } | ||
236 | |||
237 | public function getTransChoiceTests() | ||
238 | { | ||
239 | return array( | ||
240 | array('Il y a 0 pomme', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''), | ||
241 | array('Il y a 1 pomme', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''), | ||
242 | array('Il y a 10 pommes', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''), | ||
243 | |||
244 | array('Il y a 0 pomme', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''), | ||
245 | array('Il y a 1 pomme', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''), | ||
246 | array('Il y a 10 pommes', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''), | ||
247 | |||
248 | array('Il y a 0 pomme', 'one: There is one apple|more: There is %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''), | ||
249 | array('Il y a 1 pomme', 'one: There is one apple|more: There is %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''), | ||
250 | array('Il y a 10 pommes', 'one: There is one apple|more: There is %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''), | ||
251 | |||
252 | array('Il n\'y a aucune pomme', '{0} There is no apple|one: There is one apple|more: There is %count% apples', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''), | ||
253 | array('Il y a 1 pomme', '{0} There is no apple|one: There is one apple|more: There is %count% apples', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''), | ||
254 | array('Il y a 10 pommes', '{0} There is no apple|one: There is one apple|more: There is %count% apples', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''), | ||
255 | |||
256 | array('Il y a 0 pomme', new String('{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples'), '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''), | ||
257 | ); | ||
258 | } | ||
259 | |||
260 | public function testTransChoiceFallback() | ||
261 | { | ||
262 | $translator = new Translator('ru', new MessageSelector()); | ||
263 | $translator->setFallbackLocales(array('en')); | ||
264 | $translator->addLoader('array', new ArrayLoader()); | ||
265 | $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en'); | ||
266 | |||
267 | $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10))); | ||
268 | } | ||
269 | |||
270 | public function testTransChoiceFallbackBis() | ||
271 | { | ||
272 | $translator = new Translator('ru', new MessageSelector()); | ||
273 | $translator->setFallbackLocales(array('en_US', 'en')); | ||
274 | $translator->addLoader('array', new ArrayLoader()); | ||
275 | $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US'); | ||
276 | |||
277 | $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10))); | ||
278 | } | ||
279 | |||
280 | /** | ||
281 | * @expectedException \InvalidArgumentException | ||
282 | */ | ||
283 | public function testTransChoiceFallbackWithNoTranslation() | ||
284 | { | ||
285 | $translator = new Translator('ru', new MessageSelector()); | ||
286 | $translator->setFallbackLocales(array('en')); | ||
287 | $translator->addLoader('array', new ArrayLoader()); | ||
288 | |||
289 | $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10))); | ||
290 | } | ||
291 | } | ||
292 | |||
293 | class String | ||
294 | { | ||
295 | protected $str; | ||
296 | |||
297 | public function __construct($str) | ||
298 | { | ||
299 | $this->str = $str; | ||
300 | } | ||
301 | |||
302 | public function __toString() | ||
303 | { | ||
304 | return $this->str; | ||
305 | } | ||
306 | } | ||