MessageSelectorTest.php 7.8 KB

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