IdentityTranslatorTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\IdentityTranslator;
  12. use Symfony\Component\Translation\MessageSelector;
  13. class IdentityTranslatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getTransTests
  17. */
  18. public function testTrans($expected, $id, $parameters)
  19. {
  20. $translator = new IdentityTranslator(new MessageSelector());
  21. $this->assertEquals($expected, $translator->trans($id, $parameters));
  22. }
  23. /**
  24. * @dataProvider getTransChoiceTests
  25. */
  26. public function testTransChoiceWithExplicitLocale($expected, $id, $number, $parameters)
  27. {
  28. $translator = new IdentityTranslator(new MessageSelector());
  29. $translator->setLocale('en');
  30. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
  31. }
  32. /**
  33. * @dataProvider getTransChoiceTests
  34. */
  35. public function testTransChoiceWithDefaultLocale($expected, $id, $number, $parameters)
  36. {
  37. \Locale::setDefault('en');
  38. $translator = new IdentityTranslator(new MessageSelector());
  39. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters));
  40. }
  41. public function testGetSetLocale()
  42. {
  43. $translator = new IdentityTranslator(new MessageSelector());
  44. $translator->setLocale('en');
  45. $this->assertEquals('en', $translator->getLocale());
  46. }
  47. public function testGetLocaleReturnsDefaultLocaleIfNotSet()
  48. {
  49. $translator = new IdentityTranslator(new MessageSelector());
  50. \Locale::setDefault('en');
  51. $this->assertEquals('en', $translator->getLocale());
  52. \Locale::setDefault('pt_BR');
  53. $this->assertEquals('pt_BR', $translator->getLocale());
  54. }
  55. public function getTransTests()
  56. {
  57. return array(
  58. array('Symfony2 is great!', 'Symfony2 is great!', array()),
  59. array('Symfony2 is awesome!', 'Symfony2 is %what%!', array('%what%' => 'awesome')),
  60. );
  61. }
  62. public function getTransChoiceTests()
  63. {
  64. return array(
  65. array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0, array('%count%' => 0)),
  66. array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1, array('%count%' => 1)),
  67. array('There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10, array('%count%' => 10)),
  68. array('There are 0 apples', 'There is 1 apple|There are %count% apples', 0, array('%count%' => 0)),
  69. array('There is 1 apple', 'There is 1 apple|There are %count% apples', 1, array('%count%' => 1)),
  70. array('There are 10 apples', 'There is 1 apple|There are %count% apples', 10, array('%count%' => 10)),
  71. // custom validation messages may be coded with a fixed value
  72. array('There are 2 apples', 'There are 2 apples', 2, array('%count%' => 2)),
  73. );
  74. }
  75. }