1717
1818class TranslatorTestextends \PHPUnit_Framework_TestCase
1919{
20+
21+ /**
22+ * @dataProvider getInvalidLocalesTests
23+ * @expectedException \InvalidArgumentException
24+ */
25+ public function testConstructorInvalidLocale ($ locale )
26+ {
27+ $ translator =new Translator ($ locale ,new MessageSelector ());
28+ }
29+
30+ /**
31+ * @dataProvider getValidLocalesTests
32+ */
33+ public function testConstructorValidLocale ($ locale )
34+ {
35+ $ translator =new Translator ($ locale ,new MessageSelector ());
36+
37+ $ this ->assertEquals ($ locale ,$ translator ->getLocale ());
38+ }
39+
40+ public function testConstructorWithoutLocale ()
41+ {
42+ $ translator =new Translator (null ,new MessageSelector ());
43+
44+ $ this ->assertNull ($ translator ->getLocale ());
45+ }
46+
2047public function testSetGetLocale ()
2148 {
2249$ translator =new Translator ('en ' ,new MessageSelector ());
@@ -27,6 +54,27 @@ public function testSetGetLocale()
2754$ this ->assertEquals ('fr ' ,$ translator ->getLocale ());
2855 }
2956
57+ /**
58+ * @dataProvider getInvalidLocalesTests
59+ * @expectedException \InvalidArgumentException
60+ */
61+ public function testSetInvalidLocale ($ locale )
62+ {
63+ $ translator =new Translator ('fr ' ,new MessageSelector ());
64+ $ translator ->setLocale ($ locale );
65+ }
66+
67+ /**
68+ * @dataProvider getValidLocalesTests
69+ */
70+ public function testSetValidLocale ($ locale )
71+ {
72+ $ translator =new Translator ($ locale ,new MessageSelector ());
73+ $ translator ->setLocale ($ locale );
74+
75+ $ this ->assertEquals ($ locale ,$ translator ->getLocale ());
76+ }
77+
3078public function testSetFallbackLocales ()
3179 {
3280$ translator =new Translator ('en ' ,new MessageSelector ());
@@ -55,6 +103,27 @@ public function testSetFallbackLocalesMultiple()
55103$ this ->assertEquals ('bar (fr) ' ,$ translator ->trans ('bar ' ));
56104 }
57105
106+
107+ /**
108+ * @dataProvider getInvalidLocalesTests
109+ * @expectedException \InvalidArgumentException
110+ */
111+ public function testSetFallbackInvalidLocales ($ locale )
112+ {
113+ $ translator =new Translator ('fr ' ,new MessageSelector ());
114+ $ translator ->setFallbackLocales (array ('fr ' ,$ locale ));
115+ }
116+
117+ /**
118+ * @dataProvider getValidLocalesTests
119+ */
120+ public function testSetFallbackValidLocales ($ locale )
121+ {
122+ $ translator =new Translator ($ locale ,new MessageSelector ());
123+ $ translator ->setFallbackLocales (array ('fr ' ,$ locale ));
124+ // no assertion. this method just asserts that no exception is thrown
125+ }
126+
58127public function testTransWithFallbackLocale ()
59128 {
60129$ translator =new Translator ('fr_FR ' ,new MessageSelector ());
@@ -67,6 +136,26 @@ public function testTransWithFallbackLocale()
67136$ this ->assertEquals ('foobar ' ,$ translator ->trans ('bar ' ));
68137 }
69138
139+ /**
140+ * @dataProvider getInvalidLocalesTests
141+ * @expectedException \InvalidArgumentException
142+ */
143+ public function testAddResourceInvalidLocales ($ locale )
144+ {
145+ $ translator =new Translator ('fr ' ,new MessageSelector ());
146+ $ translator ->addResource ('array ' ,array ('foo ' =>'foofoo ' ),$ locale );
147+ }
148+
149+ /**
150+ * @dataProvider getValidLocalesTests
151+ */
152+ public function testAddResourceValidLocales ($ locale )
153+ {
154+ $ translator =new Translator ('fr ' ,new MessageSelector ());
155+ $ translator ->addResource ('array ' ,array ('foo ' =>'foofoo ' ),$ locale );
156+ // no assertion. this method just asserts that no exception is thrown
157+ }
158+
70159public function testAddResourceAfterTrans ()
71160 {
72161$ translator =new Translator ('fr ' ,new MessageSelector ());
@@ -164,6 +253,32 @@ public function testTrans($expected, $id, $translation, $parameters, $locale, $d
164253$ this ->assertEquals ($ expected ,$ translator ->trans ($ id ,$ parameters ,$ domain ,$ locale ));
165254 }
166255
256+ /**
257+ * @dataProvider getInvalidLocalesTests
258+ * @expectedException \InvalidArgumentException
259+ */
260+ public function testTransInvalidLocale ($ locale )
261+ {
262+ $ translator =new Translator ('en ' ,new MessageSelector ());
263+ $ translator ->addLoader ('array ' ,new ArrayLoader ());
264+ $ translator ->addResource ('array ' ,array ('foo ' =>'foofoo ' ),'en ' );
265+
266+ $ translator ->trans ('foo ' ,array (),'' ,$ locale );
267+ }
268+
269+ /**
270+ * @dataProvider getValidLocalesTests
271+ */
272+ public function testTransValidLocale ($ locale )
273+ {
274+ $ translator =new Translator ('en ' ,new MessageSelector ());
275+ $ translator ->addLoader ('array ' ,new ArrayLoader ());
276+ $ translator ->addResource ('array ' ,array ('foo ' =>'foofoo ' ),'en ' );
277+
278+ $ translator ->trans ('foo ' ,array (),'' ,$ locale );
279+ // no assertion. this method just asserts that no exception is thrown
280+ }
281+
167282/**
168283 * @dataProvider getFlattenedTransTests
169284 */
@@ -188,6 +303,33 @@ public function testTransChoice($expected, $id, $translation, $number, $paramete
188303$ this ->assertEquals ($ expected ,$ translator ->transChoice ($ id ,$ number ,$ parameters ,$ domain ,$ locale ));
189304 }
190305
306+ /**
307+ * @dataProvider getInvalidLocalesTests
308+ * @expectedException \InvalidArgumentException
309+ */
310+ public function testTransChoiceInvalidLocale ($ locale )
311+ {
312+ $ translator =new Translator ('en ' ,new MessageSelector ());
313+ $ translator ->addLoader ('array ' ,new ArrayLoader ());
314+ $ translator ->addResource ('array ' ,array ('foo ' =>'foofoo ' ),'en ' );
315+
316+ $ translator ->transChoice ('foo ' ,1 ,array (),'' ,$ locale );
317+ }
318+
319+ /**
320+ * @dataProvider getValidLocalesTests
321+ */
322+ public function testTransChoiceValidLocale ($ locale )
323+ {
324+ $ translator =new Translator ('en ' ,new MessageSelector ());
325+ $ translator ->addLoader ('array ' ,new ArrayLoader ());
326+ $ translator ->addResource ('array ' ,array ('foo ' =>'foofoo ' ),'en ' );
327+
328+ $ translator ->transChoice ('foo ' ,1 ,array (),'' ,$ locale );
329+ // no assertion. this method just asserts that no exception is thrown
330+ }
331+
332+
191333public function getTransFileTests ()
192334 {
193335return array (
@@ -257,6 +399,39 @@ public function getTransChoiceTests()
257399 );
258400 }
259401
402+ public function getInvalidLocalesTests ()
403+ {
404+ return array (
405+ array ('fr FR ' ),
406+ array ('français ' ),
407+ array ('fr+en ' ),
408+ array ('utf#8 ' ),
409+ array ('fr&en ' ),
410+ array ('fr~FR ' ),
411+ array (' fr ' ),
412+ array ('fr ' ),
413+ array ('fr* ' ),
414+ array ('fr/FR ' ),
415+ array ('fr \\FR ' ),
416+ );
417+ }
418+
419+ public function getValidLocalesTests ()
420+ {
421+ return array (
422+ array ('' ),
423+ array (null ),
424+ array ('fr ' ),
425+ array ('francais ' ),
426+ array ('FR ' ),
427+ array ('frFR ' ),
428+ array ('fr-FR ' ),
429+ array ('fr_FR ' ),
430+ array ('fr.FR ' ),
431+ array ('fr-FR.UTF8 ' ),
432+ );
433+ }
434+
260435public function testTransChoiceFallback ()
261436 {
262437$ translator =new Translator ('ru ' ,new MessageSelector ());