MessageSelectorTest.php 7.4 KB

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