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 Symfony\Component\Translation\MessageCatalogue
;
16 class MessageCatalogueTest
extends \PHPUnit_Framework_TestCase
18 public function testGetLocale()
20 $catalogue = new MessageCatalogue('en');
22 $this->assertEquals('en', $catalogue->getLocale());
25 public function testGetDomains()
27 $catalogue = new MessageCatalogue('en', array('domain1' => array(), 'domain2' => array()));
29 $this->assertEquals(array('domain1', 'domain2'), $catalogue->getDomains());
32 public function testAll()
34 $catalogue = new MessageCatalogue('en', $messages = array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
36 $this->assertEquals(array('foo' => 'foo'), $catalogue->all('domain1'));
37 $this->assertEquals(array(), $catalogue->all('domain88'));
38 $this->assertEquals($messages, $catalogue->all());
41 public function testHas()
43 $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
45 $this->assertTrue($catalogue->has('foo', 'domain1'));
46 $this->assertFalse($catalogue->has('bar', 'domain1'));
47 $this->assertFalse($catalogue->has('foo', 'domain88'));
50 public function testGetSet()
52 $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
53 $catalogue->set('foo1', 'foo1', 'domain1');
55 $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
56 $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
59 public function testAdd()
61 $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
62 $catalogue->add(array('foo1' => 'foo1'), 'domain1');
64 $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
65 $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
67 $catalogue->add(array('foo' => 'bar'), 'domain1');
68 $this->assertEquals('bar', $catalogue->get('foo', 'domain1'));
69 $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
71 $catalogue->add(array('foo' => 'bar'), 'domain88');
72 $this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
75 public function testReplace()
77 $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
78 $catalogue->replace($messages = array('foo1' => 'foo1'), 'domain1');
80 $this->assertEquals($messages, $catalogue->all('domain1'));
83 public function testAddCatalogue()
85 if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
86 $this->markTestSkipped('The "Config" component is not available');
89 $r = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
90 $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
92 $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
93 $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
95 $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
96 $catalogue->addResource($r);
98 $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo1' => 'foo1')));
99 $catalogue1->addResource($r1);
101 $catalogue->addCatalogue($catalogue1);
103 $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
104 $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
106 $this->assertEquals(array($r, $r1), $catalogue->getResources());
109 public function testAddFallbackCatalogue()
111 if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
112 $this->markTestSkipped('The "Config" component is not available');
115 $r = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
116 $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
118 $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
119 $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
121 $catalogue = new MessageCatalogue('en_US', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
122 $catalogue->addResource($r);
124 $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo' => 'bar', 'foo1' => 'foo1')));
125 $catalogue1->addResource($r1);
127 $catalogue->addFallbackCatalogue($catalogue1);
129 $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
130 $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
132 $this->assertEquals(array($r, $r1), $catalogue->getResources());
136 * @expectedException LogicException
138 public function testAddFallbackCatalogueWithCircularReference()
140 $main = new MessageCatalogue('en_US');
141 $fallback = new MessageCatalogue('fr_FR');
143 $fallback->addFallbackCatalogue($main);
144 $main->addFallbackCatalogue($fallback);
148 * @expectedException LogicException
150 public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
152 $catalogue = new MessageCatalogue('en');
153 $catalogue->addCatalogue(new MessageCatalogue('fr', array()));
156 public function testGetAddResource()
158 if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
159 $this->markTestSkipped('The "Config" component is not available');
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);
171 $this->assertEquals(array($r, $r1), $catalogue->getResources());
174 public function testMetadataDelete()
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();
183 public function testMetadataSetGetDelete()
185 $catalogue = new MessageCatalogue('en');
186 $catalogue->setMetadata('key', 'value');
187 $this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
189 $catalogue->setMetadata('key2', array());
190 $this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
192 $catalogue->deleteMetadata('key2', 'messages');
193 $this->assertEquals(null, $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
195 $catalogue->deleteMetadata('key2', 'domain');
196 $this->assertEquals(null, $catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
199 public function testMetadataMerge()
201 $cat1 = new MessageCatalogue('en');
202 $cat1->setMetadata('a', 'b');
203 $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
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.');
209 $cat1->addCatalogue($cat2);
210 $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');