IdentityTranslatorTest.php 3.2 KB

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