MessageFormatterTest.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Formatter;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\Formatter\MessageFormatter;
  13. class MessageFormatterTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider getTransMessages
  17. */
  18. public function testFormat($expected, $message, $parameters = array())
  19. {
  20. $this->assertEquals($expected, $this->getMessageFormatter()->format($message, 'en', $parameters));
  21. }
  22. /**
  23. * @dataProvider getTransChoiceMessages
  24. */
  25. public function testFormatPlural($expected, $message, $number, $parameters)
  26. {
  27. $this->assertEquals($expected, $this->getMessageFormatter()->choiceFormat($message, $number, 'fr', $parameters));
  28. }
  29. public function getTransMessages()
  30. {
  31. return array(
  32. array(
  33. 'There is one apple',
  34. 'There is one apple',
  35. ),
  36. array(
  37. 'There are 5 apples',
  38. 'There are %count% apples',
  39. array('%count%' => 5),
  40. ),
  41. array(
  42. 'There are 5 apples',
  43. 'There are {{count}} apples',
  44. array('{{count}}' => 5),
  45. ),
  46. );
  47. }
  48. public function getTransChoiceMessages()
  49. {
  50. return array(
  51. array('Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0)),
  52. array('Il y a 1 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 1, array('%count%' => 1)),
  53. array('Il y a 10 pommes', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 10, array('%count%' => 10)),
  54. array('Il y a 0 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 0, array('%count%' => 0)),
  55. array('Il y a 1 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 1, array('%count%' => 1)),
  56. array('Il y a 10 pommes', 'Il y a %count% pomme|Il y a %count% pommes', 10, array('%count%' => 10)),
  57. array('Il y a 0 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0)),
  58. array('Il y a 1 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1)),
  59. array('Il y a 10 pommes', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10)),
  60. array('Il n\'y a aucune pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0)),
  61. array('Il y a 1 pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1)),
  62. array('Il y a 10 pommes', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10)),
  63. array('Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0)),
  64. );
  65. }
  66. private function getMessageFormatter()
  67. {
  68. return new MessageFormatter();
  69. }
  70. }