9d68749428f4339e3e25bf76f90d0c9f4d71c050
[yaffs-website] / vendor / symfony / translation / Tests / MessageSelectorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\MessageSelector;
16
17 class MessageSelectorTest extends TestCase
18 {
19     /**
20      * @dataProvider getChooseTests
21      */
22     public function testChoose($expected, $id, $number)
23     {
24         $selector = new MessageSelector();
25
26         $this->assertEquals($expected, $selector->choose($id, $number, 'en'));
27     }
28
29     public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
30     {
31         $selector = new MessageSelector();
32
33         $this->assertEquals('There are two apples', $selector->choose('There are two apples', 2, 'en'));
34     }
35
36     /**
37      * @dataProvider getNonMatchingMessages
38      * @expectedException \InvalidArgumentException
39      */
40     public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
41     {
42         $selector = new MessageSelector();
43
44         $selector->choose($id, $number, 'en');
45     }
46
47     public function getNonMatchingMessages()
48     {
49         return array(
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),
54         );
55     }
56
57     public function getChooseTests()
58     {
59         return array(
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),
63
64             array('There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1),
65
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),
69
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),
73
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),
77
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),
81
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),
84
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),
89
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),
97
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),
129         );
130     }
131 }