MessageSelectorTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Translation\MessageSelector;
  13. class MessageSelectorTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider getChooseTests
  17. */
  18. public function testChoose($expected, $id, $number)
  19. {
  20. $selector = new MessageSelector();
  21. $this->assertEquals($expected, $selector->choose($id, $number, 'en'));
  22. }
  23. public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
  24. {
  25. $selector = new MessageSelector();
  26. $this->assertEquals('There are two apples', $selector->choose('There are two apples', 2, 'en'));
  27. }
  28. /**
  29. * @dataProvider getNonMatchingMessages
  30. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  31. */
  32. public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
  33. {
  34. $selector = new MessageSelector();
  35. $selector->choose($id, $number, 'en');
  36. }
  37. public function getNonMatchingMessages()
  38. {
  39. return array(
  40. array('{0} There are no apples|{1} There is one apple', 2),
  41. array('{1} There is one apple|]1,Inf] There are %count% apples', 0),
  42. array('{1} There is one apple|]2,Inf] There are %count% apples', 2),
  43. array('{0} There are no apples|There is one apple', 2),
  44. );
  45. }
  46. public function getChooseTests()
  47. {
  48. return array(
  49. array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
  50. array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
  51. array('There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
  52. array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1),
  53. array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
  54. array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10),
  55. array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
  56. array('There are %count% apples', 'There is one apple|There are %count% apples', 0),
  57. array('There is one apple', 'There is one apple|There are %count% apples', 1),
  58. array('There are %count% apples', 'There is one apple|There are %count% apples', 10),
  59. array('There are %count% apples', 'one: There is one apple|more: There are %count% apples', 0),
  60. array('There is one apple', 'one: There is one apple|more: There are %count% apples', 1),
  61. array('There are %count% apples', 'one: There is one apple|more: There are %count% apples', 10),
  62. array('There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0),
  63. array('There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1),
  64. array('There are %count% apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10),
  65. array('', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0),
  66. array('', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1),
  67. // Indexed only tests which are Gettext PoFile* compatible strings.
  68. array('There are %count% apples', 'There is one apple|There are %count% apples', 0),
  69. array('There is one apple', 'There is one apple|There are %count% apples', 1),
  70. array('There are %count% apples', 'There is one apple|There are %count% apples', 2),
  71. // Tests for float numbers
  72. array('There is almost one apple', '{0} There are no apples|]0,1[ There is almost one apple|{1} There is one apple|[1,Inf] There is more than one apple', 0.7),
  73. array('There is one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1),
  74. array('There is more than one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1.7),
  75. array('There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
  76. array('There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0.0),
  77. array('There are no apples', '{0.0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0),
  78. // Test texts with new-lines
  79. // with double-quotes and \n in id & double-quotes and actual newlines in text
  80. array("This is a text with a\n new-line in it. Selector = 0.", '{0}This is a text with a
  81. new-line in it. Selector = 0.|{1}This is a text with a
  82. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  83. new-line in it. Selector > 1.', 0),
  84. // with double-quotes and \n in id and single-quotes and actual newlines in text
  85. array("This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a
  86. new-line in it. Selector = 0.|{1}This is a text with a
  87. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  88. new-line in it. Selector > 1.', 1),
  89. array("This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a
  90. new-line in it. Selector = 0.|{1}This is a text with a
  91. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  92. new-line in it. Selector > 1.', 5),
  93. // with double-quotes and id split accros lines
  94. array('This is a text with a
  95. new-line in it. Selector = 1.', '{0}This is a text with a
  96. new-line in it. Selector = 0.|{1}This is a text with a
  97. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  98. new-line in it. Selector > 1.', 1),
  99. // with single-quotes and id split accros lines
  100. array('This is a text with a
  101. new-line in it. Selector > 1.', '{0}This is a text with a
  102. new-line in it. Selector = 0.|{1}This is a text with a
  103. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  104. new-line in it. Selector > 1.', 5),
  105. // with single-quotes and \n in text
  106. array('This is a text with a\nnew-line in it. Selector = 0.', '{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.', 0),
  107. // with double-quotes and id split accros lines
  108. array("This is a text with a\nnew-line in it. Selector = 1.", "{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.", 1),
  109. // esacape pipe
  110. array('This is a text with | in it. Selector = 0.', '{0}This is a text with || in it. Selector = 0.|{1}This is a text with || in it. Selector = 1.', 0),
  111. );
  112. }
  113. }