MessageFormatterTest.php 3.2 KB

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