Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / translation / Tests / MessageCatalogueTest.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\MessageCatalogue;
16
17 class MessageCatalogueTest extends TestCase
18 {
19     public function testGetLocale()
20     {
21         $catalogue = new MessageCatalogue('en');
22
23         $this->assertEquals('en', $catalogue->getLocale());
24     }
25
26     public function testGetDomains()
27     {
28         $catalogue = new MessageCatalogue('en', array('domain1' => array(), 'domain2' => array()));
29
30         $this->assertEquals(array('domain1', 'domain2'), $catalogue->getDomains());
31     }
32
33     public function testAll()
34     {
35         $catalogue = new MessageCatalogue('en', $messages = array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
36
37         $this->assertEquals(array('foo' => 'foo'), $catalogue->all('domain1'));
38         $this->assertEquals(array(), $catalogue->all('domain88'));
39         $this->assertEquals($messages, $catalogue->all());
40     }
41
42     public function testHas()
43     {
44         $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
45
46         $this->assertTrue($catalogue->has('foo', 'domain1'));
47         $this->assertFalse($catalogue->has('bar', 'domain1'));
48         $this->assertFalse($catalogue->has('foo', 'domain88'));
49     }
50
51     public function testGetSet()
52     {
53         $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
54         $catalogue->set('foo1', 'foo1', 'domain1');
55
56         $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
57         $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
58     }
59
60     public function testAdd()
61     {
62         $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
63         $catalogue->add(array('foo1' => 'foo1'), 'domain1');
64
65         $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
66         $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
67
68         $catalogue->add(array('foo' => 'bar'), 'domain1');
69         $this->assertEquals('bar', $catalogue->get('foo', 'domain1'));
70         $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
71
72         $catalogue->add(array('foo' => 'bar'), 'domain88');
73         $this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
74     }
75
76     public function testReplace()
77     {
78         $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
79         $catalogue->replace($messages = array('foo1' => 'foo1'), 'domain1');
80
81         $this->assertEquals($messages, $catalogue->all('domain1'));
82     }
83
84     public function testAddCatalogue()
85     {
86         $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
87         $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
88
89         $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
90         $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
91
92         $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
93         $catalogue->addResource($r);
94
95         $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo1' => 'foo1')));
96         $catalogue1->addResource($r1);
97
98         $catalogue->addCatalogue($catalogue1);
99
100         $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
101         $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
102
103         $this->assertEquals(array($r, $r1), $catalogue->getResources());
104     }
105
106     public function testAddFallbackCatalogue()
107     {
108         $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
109         $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
110
111         $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
112         $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
113
114         $r2 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
115         $r2->expects($this->any())->method('__toString')->will($this->returnValue('r2'));
116
117         $catalogue = new MessageCatalogue('fr_FR', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
118         $catalogue->addResource($r);
119
120         $catalogue1 = new MessageCatalogue('fr', array('domain1' => array('foo' => 'bar', 'foo1' => 'foo1')));
121         $catalogue1->addResource($r1);
122
123         $catalogue2 = new MessageCatalogue('en');
124         $catalogue2->addResource($r2);
125
126         $catalogue->addFallbackCatalogue($catalogue1);
127         $catalogue1->addFallbackCatalogue($catalogue2);
128
129         $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
130         $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
131
132         $this->assertEquals(array($r, $r1, $r2), $catalogue->getResources());
133     }
134
135     /**
136      * @expectedException \Symfony\Component\Translation\Exception\LogicException
137      */
138     public function testAddFallbackCatalogueWithParentCircularReference()
139     {
140         $main = new MessageCatalogue('en_US');
141         $fallback = new MessageCatalogue('fr_FR');
142
143         $fallback->addFallbackCatalogue($main);
144         $main->addFallbackCatalogue($fallback);
145     }
146
147     /**
148      * @expectedException \Symfony\Component\Translation\Exception\LogicException
149      */
150     public function testAddFallbackCatalogueWithFallbackCircularReference()
151     {
152         $fr = new MessageCatalogue('fr');
153         $en = new MessageCatalogue('en');
154         $es = new MessageCatalogue('es');
155
156         $fr->addFallbackCatalogue($en);
157         $es->addFallbackCatalogue($en);
158         $en->addFallbackCatalogue($fr);
159     }
160
161     /**
162      * @expectedException \Symfony\Component\Translation\Exception\LogicException
163      */
164     public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
165     {
166         $catalogue = new MessageCatalogue('en');
167         $catalogue->addCatalogue(new MessageCatalogue('fr', array()));
168     }
169
170     public function testGetAddResource()
171     {
172         $catalogue = new MessageCatalogue('en');
173         $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
174         $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
175         $catalogue->addResource($r);
176         $catalogue->addResource($r);
177         $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
178         $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
179         $catalogue->addResource($r1);
180
181         $this->assertEquals(array($r, $r1), $catalogue->getResources());
182     }
183
184     public function testMetadataDelete()
185     {
186         $catalogue = new MessageCatalogue('en');
187         $this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
188         $catalogue->deleteMetadata('key', 'messages');
189         $catalogue->deleteMetadata('', 'messages');
190         $catalogue->deleteMetadata();
191     }
192
193     public function testMetadataSetGetDelete()
194     {
195         $catalogue = new MessageCatalogue('en');
196         $catalogue->setMetadata('key', 'value');
197         $this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
198
199         $catalogue->setMetadata('key2', array());
200         $this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
201
202         $catalogue->deleteMetadata('key2', 'messages');
203         $this->assertNull($catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
204
205         $catalogue->deleteMetadata('key2', 'domain');
206         $this->assertNull($catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
207     }
208
209     public function testMetadataMerge()
210     {
211         $cat1 = new MessageCatalogue('en');
212         $cat1->setMetadata('a', 'b');
213         $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
214
215         $cat2 = new MessageCatalogue('en');
216         $cat2->setMetadata('b', 'c', 'domain');
217         $this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
218
219         $cat1->addCatalogue($cat2);
220         $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
221     }
222 }