MessageSelectorTest.php 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. );
  78. }
  79. }