4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\MessageSelector;
17 class MessageSelectorTest extends TestCase
20 * @dataProvider getChooseTests
22 public function testChoose($expected, $id, $number)
24 $selector = new MessageSelector();
26 $this->assertEquals($expected, $selector->choose($id, $number, 'en'));
29 public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
31 $selector = new MessageSelector();
33 $this->assertEquals('There are two apples', $selector->choose('There are two apples', 2, 'en'));
37 * @dataProvider getNonMatchingMessages
38 * @expectedException \InvalidArgumentException
40 public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
42 $selector = new MessageSelector();
44 $selector->choose($id, $number, 'en');
47 public function getNonMatchingMessages()
50 array('{0} There are no apples|{1} There is one apple', 2),
51 array('{1} There is one apple|]1,Inf] There are %count% apples', 0),
52 array('{1} There is one apple|]2,Inf] There are %count% apples', 2),
53 array('{0} There are no apples|There is one apple', 2),
57 public function getChooseTests()
60 array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
61 array('There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
62 array('There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0),
64 array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1),
66 array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
67 array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10),
68 array('There are %count% apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10),
70 array('There are %count% apples', 'There is one apple|There are %count% apples', 0),
71 array('There is one apple', 'There is one apple|There are %count% apples', 1),
72 array('There are %count% apples', 'There is one apple|There are %count% apples', 10),
74 array('There are %count% apples', 'one: There is one apple|more: There are %count% apples', 0),
75 array('There is one apple', 'one: There is one apple|more: There are %count% apples', 1),
76 array('There are %count% apples', 'one: There is one apple|more: There are %count% apples', 10),
78 array('There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0),
79 array('There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1),
80 array('There are %count% apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10),
82 array('', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0),
83 array('', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1),
85 // Indexed only tests which are Gettext PoFile* compatible strings.
86 array('There are %count% apples', 'There is one apple|There are %count% apples', 0),
87 array('There is one apple', 'There is one apple|There are %count% apples', 1),
88 array('There are %count% apples', 'There is one apple|There are %count% apples', 2),
90 // Tests for float numbers
91 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),
92 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),
93 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),
94 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),
95 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),
96 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),
98 // Test texts with new-lines
99 // with double-quotes and \n in id & double-quotes and actual newlines in text
100 array("This is a text with a\n new-line in it. Selector = 0.", '{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.', 0),
104 // with double-quotes and \n in id and single-quotes and actual newlines in text
105 array("This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a
106 new-line in it. Selector = 0.|{1}This is a text with a
107 new-line in it. Selector = 1.|[1,Inf]This is a text with a
108 new-line in it. Selector > 1.', 1),
109 array("This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a
110 new-line in it. Selector = 0.|{1}This is a text with a
111 new-line in it. Selector = 1.|[1,Inf]This is a text with a
112 new-line in it. Selector > 1.', 5),
113 // with double-quotes and id split accros lines
114 array('This is a text with a
115 new-line in it. Selector = 1.', '{0}This is a text with a
116 new-line in it. Selector = 0.|{1}This is a text with a
117 new-line in it. Selector = 1.|[1,Inf]This is a text with a
118 new-line in it. Selector > 1.', 1),
119 // with single-quotes and id split accros lines
120 array('This is a text with a
121 new-line in it. Selector > 1.', '{0}This is a text with a
122 new-line in it. Selector = 0.|{1}This is a text with a
123 new-line in it. Selector = 1.|[1,Inf]This is a text with a
124 new-line in it. Selector > 1.', 5),
125 // with single-quotes and \n in text
126 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),
127 // with double-quotes and id split accros lines
128 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),