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