PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Translation/Tests/TranslatorTest.php

https://bitbucket.org/hill2steve/mobileroom
PHP | 248 lines | 178 code | 45 blank | 25 comment | 0 complexity | e161f68fb29714701bf05994ea7d250b MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Tests;
  11. use Symfony\Component\Translation\Translator;
  12. use Symfony\Component\Translation\MessageSelector;
  13. use Symfony\Component\Translation\Loader\ArrayLoader;
  14. class TranslatorTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testSetGetLocale()
  17. {
  18. $translator = new Translator('en', new MessageSelector());
  19. $this->assertEquals('en', $translator->getLocale());
  20. $translator->setLocale('fr');
  21. $this->assertEquals('fr', $translator->getLocale());
  22. }
  23. public function testSetFallbackLocale()
  24. {
  25. $translator = new Translator('en', new MessageSelector());
  26. $translator->addLoader('array', new ArrayLoader());
  27. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  28. $translator->addResource('array', array('bar' => 'foobar'), 'fr');
  29. // force catalogue loading
  30. $translator->trans('bar');
  31. $translator->setFallbackLocale('fr');
  32. $this->assertEquals('foobar', $translator->trans('bar'));
  33. }
  34. public function testSetFallbackLocaleMultiple()
  35. {
  36. $translator = new Translator('en', new MessageSelector());
  37. $translator->addLoader('array', new ArrayLoader());
  38. $translator->addResource('array', array('foo' => 'foo (en)'), 'en');
  39. $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
  40. // force catalogue loading
  41. $translator->trans('bar');
  42. $translator->setFallbackLocale(array('fr_FR', 'fr'));
  43. $this->assertEquals('bar (fr)', $translator->trans('bar'));
  44. }
  45. public function testTransWithFallbackLocale()
  46. {
  47. $translator = new Translator('fr_FR', new MessageSelector());
  48. $translator->addLoader('array', new ArrayLoader());
  49. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  50. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  51. $translator->setFallbackLocale('en');
  52. $this->assertEquals('foobar', $translator->trans('bar'));
  53. }
  54. public function testTransWithFallbackLocaleBis()
  55. {
  56. $translator = new Translator('en_US', new MessageSelector());
  57. $translator->addLoader('array', new ArrayLoader());
  58. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  59. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  60. $this->assertEquals('foobar', $translator->trans('bar'));
  61. }
  62. public function testTransWithFallbackLocaleTer()
  63. {
  64. $translator = new Translator('fr_FR', new MessageSelector());
  65. $translator->addLoader('array', new ArrayLoader());
  66. $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US');
  67. $translator->addResource('array', array('bar' => 'bar (en)'), 'en');
  68. $translator->setFallbackLocale(array('en_US', 'en'));
  69. $this->assertEquals('foo (en_US)', $translator->trans('foo'));
  70. $this->assertEquals('bar (en)', $translator->trans('bar'));
  71. }
  72. public function testTransNonExistentWithFallback()
  73. {
  74. $translator = new Translator('fr', new MessageSelector());
  75. $translator->setFallbackLocale('en');
  76. $translator->addLoader('array', new ArrayLoader());
  77. $this->assertEquals('non-existent', $translator->trans('non-existent'));
  78. }
  79. /**
  80. * @expectedException RuntimeException
  81. */
  82. public function testWhenAResourceHasNoRegisteredLoader()
  83. {
  84. $translator = new Translator('en', new MessageSelector());
  85. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  86. $translator->trans('foo');
  87. }
  88. /**
  89. * @dataProvider getTransTests
  90. */
  91. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  92. {
  93. $translator = new Translator('en', new MessageSelector());
  94. $translator->addLoader('array', new ArrayLoader());
  95. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  96. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  97. }
  98. /**
  99. * @dataProvider getFlattenedTransTests
  100. */
  101. public function testFlattenedTrans($expected, $messages, $id)
  102. {
  103. $translator = new Translator('en', new MessageSelector());
  104. $translator->addLoader('array', new ArrayLoader());
  105. $translator->addResource('array', $messages, 'fr', '');
  106. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  107. }
  108. /**
  109. * @dataProvider getTransChoiceTests
  110. */
  111. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  112. {
  113. $translator = new Translator('en', new MessageSelector());
  114. $translator->addLoader('array', new ArrayLoader());
  115. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  116. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  117. }
  118. public function getTransTests()
  119. {
  120. return array(
  121. array('Symfony2 est super !', 'Symfony2 is great!', 'Symfony2 est super !', array(), 'fr', ''),
  122. array('Symfony2 est awesome !', 'Symfony2 is %what%!', 'Symfony2 est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  123. array('Symfony2 est super !', new String('Symfony2 is great!'), 'Symfony2 est super !', array(), 'fr', ''),
  124. );
  125. }
  126. public function getFlattenedTransTests()
  127. {
  128. $messages = array(
  129. 'symfony2' => array(
  130. 'is' => array(
  131. 'great' => 'Symfony2 est super!'
  132. )
  133. ),
  134. 'foo' => array(
  135. 'bar' => array(
  136. 'baz' => 'Foo Bar Baz'
  137. ),
  138. 'baz' => 'Foo Baz',
  139. ),
  140. );
  141. return array(
  142. array('Symfony2 est super!', $messages, 'symfony2.is.great'),
  143. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  144. array('Foo Baz', $messages, 'foo.baz'),
  145. );
  146. }
  147. public function getTransChoiceTests()
  148. {
  149. return array(
  150. 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', ''),
  151. 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', ''),
  152. 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', ''),
  153. 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', ''),
  154. 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', ''),
  155. 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', ''),
  156. 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', ''),
  157. 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', ''),
  158. 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', ''),
  159. 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', ''),
  160. 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', ''),
  161. 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', ''),
  162. 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', ''),
  163. );
  164. }
  165. public function testTransChoiceFallback()
  166. {
  167. $translator = new Translator('ru', new MessageSelector());
  168. $translator->setFallbackLocale('en');
  169. $translator->addLoader('array', new ArrayLoader());
  170. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
  171. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  172. }
  173. public function testTransChoiceFallbackBis()
  174. {
  175. $translator = new Translator('ru', new MessageSelector());
  176. $translator->setFallbackLocale(array('en_US', 'en'));
  177. $translator->addLoader('array', new ArrayLoader());
  178. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US');
  179. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  180. }
  181. /**
  182. * @expectedException \InvalidArgumentException
  183. */
  184. public function testTransChoiceFallbackWithNoTranslation()
  185. {
  186. $translator = new Translator('ru', new MessageSelector());
  187. $translator->setFallbackLocale('en');
  188. $translator->addLoader('array', new ArrayLoader());
  189. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  190. }
  191. }
  192. class String
  193. {
  194. protected $str;
  195. public function __construct($str)
  196. {
  197. $this->str = $str;
  198. }
  199. public function __toString()
  200. {
  201. return $this->str;
  202. }
  203. }