SymfonyComponentTranslationTestsTranslatorTest

The Simple Calendar Symfony Component Translation Tests TranslatorTest class.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/Tests/TranslatorTest.php  
  1. class TranslatorTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @dataProvider getInvalidLocalesTests 
  4. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException 
  5. */ 
  6. public function testConstructorInvalidLocale($locale) 
  7. $translator = new Translator($locale, new MessageSelector()); 
  8.  
  9. /** 
  10. * @dataProvider getValidLocalesTests 
  11. */ 
  12. public function testConstructorValidLocale($locale) 
  13. $translator = new Translator($locale, new MessageSelector()); 
  14.  
  15. $this->assertEquals($locale, $translator->getLocale()); 
  16.  
  17. public function testConstructorWithoutLocale() 
  18. $translator = new Translator(null, new MessageSelector()); 
  19.  
  20. $this->assertNull($translator->getLocale()); 
  21.  
  22. public function testSetGetLocale() 
  23. $translator = new Translator('en'); 
  24.  
  25. $this->assertEquals('en', $translator->getLocale()); 
  26.  
  27. $translator->setLocale('fr'); 
  28. $this->assertEquals('fr', $translator->getLocale()); 
  29.  
  30. /** 
  31. * @dataProvider getInvalidLocalesTests 
  32. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException 
  33. */ 
  34. public function testSetInvalidLocale($locale) 
  35. $translator = new Translator('fr', new MessageSelector()); 
  36. $translator->setLocale($locale); 
  37.  
  38. /** 
  39. * @dataProvider getValidLocalesTests 
  40. */ 
  41. public function testSetValidLocale($locale) 
  42. $translator = new Translator($locale, new MessageSelector()); 
  43. $translator->setLocale($locale); 
  44.  
  45. $this->assertEquals($locale, $translator->getLocale()); 
  46.  
  47. public function testGetCatalogue() 
  48. $translator = new Translator('en'); 
  49.  
  50. $this->assertEquals(new MessageCatalogue('en'), $translator->getCatalogue()); 
  51.  
  52. $translator->setLocale('fr'); 
  53. $this->assertEquals(new MessageCatalogue('fr'), $translator->getCatalogue('fr')); 
  54.  
  55. public function testGetCatalogueReturnsConsolidatedCatalogue() 
  56. /** 
  57. * This will be useful once we refactor so that different domains will be loaded lazily (on-demand). 
  58. * In that case, getCatalogue() will probably have to load all missing domains in order to return 
  59. * one complete catalogue. 
  60. */ 
  61.  
  62. $locale = 'whatever'; 
  63. $translator = new Translator($locale); 
  64. $translator->addLoader('loader-a', new ArrayLoader()); 
  65. $translator->addLoader('loader-b', new ArrayLoader()); 
  66. $translator->addResource('loader-a', array('foo' => 'foofoo'), $locale, 'domain-a'); 
  67. $translator->addResource('loader-b', array('bar' => 'foobar'), $locale, 'domain-b'); 
  68.  
  69. /** 
  70. * Test that we get a single catalogue comprising messages 
  71. * from different loaders and different domains 
  72. */ 
  73. $catalogue = $translator->getCatalogue($locale); 
  74. $this->assertTrue($catalogue->defines('foo', 'domain-a')); 
  75. $this->assertTrue($catalogue->defines('bar', 'domain-b')); 
  76.  
  77. public function testSetFallbackLocales() 
  78. $translator = new Translator('en'); 
  79. $translator->addLoader('array', new ArrayLoader()); 
  80. $translator->addResource('array', array('foo' => 'foofoo'), 'en'); 
  81. $translator->addResource('array', array('bar' => 'foobar'), 'fr'); 
  82.  
  83. // force catalogue loading 
  84. $translator->trans('bar'); 
  85.  
  86. $translator->setFallbackLocales(array('fr')); 
  87. $this->assertEquals('foobar', $translator->trans('bar')); 
  88.  
  89. public function testSetFallbackLocalesMultiple() 
  90. $translator = new Translator('en'); 
  91. $translator->addLoader('array', new ArrayLoader()); 
  92. $translator->addResource('array', array('foo' => 'foo (en)'), 'en'); 
  93. $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr'); 
  94.  
  95. // force catalogue loading 
  96. $translator->trans('bar'); 
  97.  
  98. $translator->setFallbackLocales(array('fr_FR', 'fr')); 
  99. $this->assertEquals('bar (fr)', $translator->trans('bar')); 
  100.  
  101. /** 
  102. * @dataProvider getInvalidLocalesTests 
  103. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException 
  104. */ 
  105. public function testSetFallbackInvalidLocales($locale) 
  106. $translator = new Translator('fr', new MessageSelector()); 
  107. $translator->setFallbackLocales(array('fr', $locale)); 
  108.  
  109. /** 
  110. * @dataProvider getValidLocalesTests 
  111. */ 
  112. public function testSetFallbackValidLocales($locale) 
  113. $translator = new Translator($locale, new MessageSelector()); 
  114. $translator->setFallbackLocales(array('fr', $locale)); 
  115. // no assertion. this method just asserts that no exception is thrown 
  116.  
  117. public function testTransWithFallbackLocale() 
  118. $translator = new Translator('fr_FR'); 
  119. $translator->setFallbackLocales(array('en')); 
  120.  
  121. $translator->addLoader('array', new ArrayLoader()); 
  122. $translator->addResource('array', array('bar' => 'foobar'), 'en'); 
  123.  
  124. $this->assertEquals('foobar', $translator->trans('bar')); 
  125.  
  126. /** 
  127. * @dataProvider getInvalidLocalesTests 
  128. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException 
  129. */ 
  130. public function testAddResourceInvalidLocales($locale) 
  131. $translator = new Translator('fr', new MessageSelector()); 
  132. $translator->addResource('array', array('foo' => 'foofoo'), $locale); 
  133.  
  134. /** 
  135. * @dataProvider getValidLocalesTests 
  136. */ 
  137. public function testAddResourceValidLocales($locale) 
  138. $translator = new Translator('fr', new MessageSelector()); 
  139. $translator->addResource('array', array('foo' => 'foofoo'), $locale); 
  140. // no assertion. this method just asserts that no exception is thrown 
  141.  
  142. public function testAddResourceAfterTrans() 
  143. $translator = new Translator('fr'); 
  144. $translator->addLoader('array', new ArrayLoader()); 
  145.  
  146. $translator->setFallbackLocales(array('en')); 
  147.  
  148. $translator->addResource('array', array('foo' => 'foofoo'), 'en'); 
  149. $this->assertEquals('foofoo', $translator->trans('foo')); 
  150.  
  151. $translator->addResource('array', array('bar' => 'foobar'), 'en'); 
  152. $this->assertEquals('foobar', $translator->trans('bar')); 
  153.  
  154. /** 
  155. * @dataProvider getTransFileTests 
  156. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException 
  157. */ 
  158. public function testTransWithoutFallbackLocaleFile($format, $loader) 
  159. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader; 
  160. $translator = new Translator('en'); 
  161. $translator->addLoader($format, new $loaderClass()); 
  162. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en'); 
  163. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en'); 
  164.  
  165. // force catalogue loading 
  166. $translator->trans('foo'); 
  167.  
  168. /** 
  169. * @dataProvider getTransFileTests 
  170. */ 
  171. public function testTransWithFallbackLocaleFile($format, $loader) 
  172. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader; 
  173. $translator = new Translator('en_GB'); 
  174. $translator->addLoader($format, new $loaderClass()); 
  175. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en_GB'); 
  176. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en', 'resources'); 
  177.  
  178. $this->assertEquals('bar', $translator->trans('foo', array(), 'resources')); 
  179.  
  180. public function testTransWithFallbackLocaleBis() 
  181. $translator = new Translator('en_US'); 
  182. $translator->addLoader('array', new ArrayLoader()); 
  183. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US'); 
  184. $translator->addResource('array', array('bar' => 'foobar'), 'en'); 
  185. $this->assertEquals('foobar', $translator->trans('bar')); 
  186.  
  187. public function testTransWithFallbackLocaleTer() 
  188. $translator = new Translator('fr_FR'); 
  189. $translator->addLoader('array', new ArrayLoader()); 
  190. $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US'); 
  191. $translator->addResource('array', array('bar' => 'bar (en)'), 'en'); 
  192.  
  193. $translator->setFallbackLocales(array('en_US', 'en')); 
  194.  
  195. $this->assertEquals('foo (en_US)', $translator->trans('foo')); 
  196. $this->assertEquals('bar (en)', $translator->trans('bar')); 
  197.  
  198. public function testTransNonExistentWithFallback() 
  199. $translator = new Translator('fr'); 
  200. $translator->setFallbackLocales(array('en')); 
  201. $translator->addLoader('array', new ArrayLoader()); 
  202. $this->assertEquals('non-existent', $translator->trans('non-existent')); 
  203.  
  204. /** 
  205. * @expectedException \Symfony\Component\Translation\Exception\RuntimeException 
  206. */ 
  207. public function testWhenAResourceHasNoRegisteredLoader() 
  208. $translator = new Translator('en'); 
  209. $translator->addResource('array', array('foo' => 'foofoo'), 'en'); 
  210.  
  211. $translator->trans('foo'); 
  212.  
  213. public function testNestedFallbackCatalogueWhenUsingMultipleLocales() 
  214. $translator = new Translator('fr'); 
  215. $translator->setFallbackLocales(array('ru', 'en')); 
  216.  
  217. $translator->getCatalogue('fr'); 
  218.  
  219. $this->assertNotNull($translator->getCatalogue('ru')->getFallbackCatalogue()); 
  220.  
  221. public function testFallbackCatalogueResources() 
  222. $translator = new Translator('en_GB', new MessageSelector()); 
  223. $translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader()); 
  224. $translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB'); 
  225. $translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en'); 
  226.  
  227. // force catalogue loading 
  228. $this->assertEquals('bar', $translator->trans('foo', array())); 
  229.  
  230. $resources = $translator->getCatalogue('en')->getResources(); 
  231. $this->assertCount(1, $resources); 
  232. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'resources.yml', $resources); 
  233.  
  234. $resources = $translator->getCatalogue('en_GB')->getResources(); 
  235. $this->assertCount(2, $resources); 
  236. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'empty.yml', $resources); 
  237. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'resources.yml', $resources); 
  238.  
  239. /** 
  240. * @dataProvider getTransTests 
  241. */ 
  242. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain) 
  243. $translator = new Translator('en'); 
  244. $translator->addLoader('array', new ArrayLoader()); 
  245. $translator->addResource('array', array((string) $id => $translation), $locale, $domain); 
  246.  
  247. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale)); 
  248.  
  249. /** 
  250. * @dataProvider getInvalidLocalesTests 
  251. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException 
  252. */ 
  253. public function testTransInvalidLocale($locale) 
  254. $translator = new Translator('en', new MessageSelector()); 
  255. $translator->addLoader('array', new ArrayLoader()); 
  256. $translator->addResource('array', array('foo' => 'foofoo'), 'en'); 
  257.  
  258. $translator->trans('foo', array(), '', $locale); 
  259.  
  260. /** 
  261. * @dataProvider getValidLocalesTests 
  262. */ 
  263. public function testTransValidLocale($locale) 
  264. $translator = new Translator($locale, new MessageSelector()); 
  265. $translator->addLoader('array', new ArrayLoader()); 
  266. $translator->addResource('array', array('test' => 'OK'), $locale); 
  267.  
  268. $this->assertEquals('OK', $translator->trans('test')); 
  269. $this->assertEquals('OK', $translator->trans('test', array(), null, $locale)); 
  270.  
  271. /** 
  272. * @dataProvider getFlattenedTransTests 
  273. */ 
  274. public function testFlattenedTrans($expected, $messages, $id) 
  275. $translator = new Translator('en'); 
  276. $translator->addLoader('array', new ArrayLoader()); 
  277. $translator->addResource('array', $messages, 'fr', ''); 
  278.  
  279. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr')); 
  280.  
  281. /** 
  282. * @dataProvider getTransChoiceTests 
  283. */ 
  284. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain) 
  285. $translator = new Translator('en'); 
  286. $translator->addLoader('array', new ArrayLoader()); 
  287. $translator->addResource('array', array((string) $id => $translation), $locale, $domain); 
  288.  
  289. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale)); 
  290.  
  291. /** 
  292. * @dataProvider getInvalidLocalesTests 
  293. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException 
  294. */ 
  295. public function testTransChoiceInvalidLocale($locale) 
  296. $translator = new Translator('en', new MessageSelector()); 
  297. $translator->addLoader('array', new ArrayLoader()); 
  298. $translator->addResource('array', array('foo' => 'foofoo'), 'en'); 
  299.  
  300. $translator->transChoice('foo', 1, array(), '', $locale); 
  301.  
  302. /** 
  303. * @dataProvider getValidLocalesTests 
  304. */ 
  305. public function testTransChoiceValidLocale($locale) 
  306. $translator = new Translator('en', new MessageSelector()); 
  307. $translator->addLoader('array', new ArrayLoader()); 
  308. $translator->addResource('array', array('foo' => 'foofoo'), 'en'); 
  309.  
  310. $translator->transChoice('foo', 1, array(), '', $locale); 
  311. // no assertion. this method just asserts that no exception is thrown 
  312.  
  313. public function getTransFileTests() 
  314. return array( 
  315. array('csv', 'CsvFileLoader'),  
  316. array('ini', 'IniFileLoader'),  
  317. array('mo', 'MoFileLoader'),  
  318. array('po', 'PoFileLoader'),  
  319. array('php', 'PhpFileLoader'),  
  320. array('ts', 'QtFileLoader'),  
  321. array('xlf', 'XliffFileLoader'),  
  322. array('yml', 'YamlFileLoader'),  
  323. array('json', 'JsonFileLoader'),  
  324. ); 
  325.  
  326. public function getTransTests() 
  327. return array( 
  328. array('Symfony est super !', 'Symfony is great!', 'Symfony est super !', array(), 'fr', ''),  
  329. array('Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', array('%what%' => 'awesome'), 'fr', ''),  
  330. array('Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', array(), 'fr', ''),  
  331. ); 
  332.  
  333. public function getFlattenedTransTests() 
  334. $messages = array( 
  335. 'symfony' => array( 
  336. 'is' => array( 
  337. 'great' => 'Symfony est super!',  
  338. ),  
  339. ),  
  340. 'foo' => array( 
  341. 'bar' => array( 
  342. 'baz' => 'Foo Bar Baz',  
  343. ),  
  344. 'baz' => 'Foo Baz',  
  345. ),  
  346. ); 
  347.  
  348. return array( 
  349. array('Symfony est super!', $messages, 'symfony.is.great'),  
  350. array('Foo Bar Baz', $messages, 'foo.bar.baz'),  
  351. array('Foo Baz', $messages, 'foo.baz'),  
  352. ); 
  353.  
  354. public function getTransChoiceTests() 
  355. return array( 
  356. array('Il y a 0 pomme', '{0} There are no appless|{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(), 'fr', ''),  
  357. array('Il y a 1 pomme', '{0} There are no appless|{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(), 'fr', ''),  
  358. array('Il y a 10 pommes', '{0} There are no appless|{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(), 'fr', ''),  
  359.  
  360. 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(), 'fr', ''),  
  361. 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(), 'fr', ''),  
  362. 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(), 'fr', ''),  
  363.  
  364. 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(), 'fr', ''),  
  365. 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(), 'fr', ''),  
  366. 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(), 'fr', ''),  
  367.  
  368. array('Il n\'y a aucune pomme', '{0} There are no apples|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(), 'fr', ''),  
  369. array('Il y a 1 pomme', '{0} There are no apples|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(), 'fr', ''),  
  370. array('Il y a 10 pommes', '{0} There are no apples|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(), 'fr', ''),  
  371.  
  372. array('Il y a 0 pomme', new StringClass('{0} There are no appless|{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(), 'fr', ''),  
  373.  
  374. // Override %count% with a custom value 
  375. array('Il y a quelques pommes', 'one: There is one apple|more: There are %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 2, array('%count%' => 'quelques'), 'fr', ''),  
  376. ); 
  377.  
  378. public function getInvalidLocalesTests() 
  379. return array( 
  380. array('fr FR'),  
  381. array('franais'),  
  382. array('fr+en'),  
  383. array('utf#8'),  
  384. array('fr&en'),  
  385. array('fr~FR'),  
  386. array(' fr'),  
  387. array('fr '),  
  388. array('fr*'),  
  389. array('fr/FR'),  
  390. array('fr\\FR'),  
  391. ); 
  392.  
  393. public function getValidLocalesTests() 
  394. return array( 
  395. array(''),  
  396. array(null),  
  397. array('fr'),  
  398. array('francais'),  
  399. array('FR'),  
  400. array('frFR'),  
  401. array('fr-FR'),  
  402. array('fr_FR'),  
  403. array('fr.FR'),  
  404. array('fr-FR.UTF8'),  
  405. array('sr@latin'),  
  406. ); 
  407.  
  408. public function testTransChoiceFallback() 
  409. $translator = new Translator('ru'); 
  410. $translator->setFallbackLocales(array('en')); 
  411. $translator->addLoader('array', new ArrayLoader()); 
  412. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en'); 
  413.  
  414. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10))); 
  415.  
  416. public function testTransChoiceFallbackBis() 
  417. $translator = new Translator('ru'); 
  418. $translator->setFallbackLocales(array('en_US', 'en')); 
  419. $translator->addLoader('array', new ArrayLoader()); 
  420. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US'); 
  421.  
  422. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10))); 
  423.  
  424. public function testTransChoiceFallbackWithNoTranslation() 
  425. $translator = new Translator('ru'); 
  426. $translator->setFallbackLocales(array('en')); 
  427. $translator->addLoader('array', new ArrayLoader()); 
  428.  
  429. // consistent behavior with Translator::trans(), which returns the string 
  430. // unchanged if it can't be found 
  431. $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));