MessageCatalogueTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\MessageCatalogue;
  13. class MessageCatalogueTest extends TestCase
  14. {
  15. public function testGetLocale()
  16. {
  17. $catalogue = new MessageCatalogue('en');
  18. $this->assertEquals('en', $catalogue->getLocale());
  19. }
  20. public function testGetDomains()
  21. {
  22. $catalogue = new MessageCatalogue('en', ['domain1' => [], 'domain2' => [], 'domain2+intl-icu' => [], 'domain3+intl-icu' => []]);
  23. $this->assertEquals(['domain1', 'domain2', 'domain3'], $catalogue->getDomains());
  24. }
  25. public function testAll()
  26. {
  27. $catalogue = new MessageCatalogue('en', $messages = ['domain1' => ['foo' => 'foo'], 'domain2' => ['bar' => 'bar']]);
  28. $this->assertEquals(['foo' => 'foo'], $catalogue->all('domain1'));
  29. $this->assertEquals([], $catalogue->all('domain88'));
  30. $this->assertEquals($messages, $catalogue->all());
  31. $messages = ['domain1+intl-icu' => ['foo' => 'bar']] + $messages + [
  32. 'domain2+intl-icu' => ['bar' => 'foo'],
  33. 'domain3+intl-icu' => ['biz' => 'biz'],
  34. ];
  35. $catalogue = new MessageCatalogue('en', $messages);
  36. $this->assertEquals(['foo' => 'bar'], $catalogue->all('domain1'));
  37. $this->assertEquals(['bar' => 'foo'], $catalogue->all('domain2'));
  38. $this->assertEquals(['biz' => 'biz'], $catalogue->all('domain3'));
  39. $messages = [
  40. 'domain1' => ['foo' => 'bar'],
  41. 'domain2' => ['bar' => 'foo'],
  42. 'domain3' => ['biz' => 'biz'],
  43. ];
  44. $this->assertEquals($messages, $catalogue->all());
  45. }
  46. public function testHas()
  47. {
  48. $catalogue = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo'], 'domain2+intl-icu' => ['bar' => 'bar']]);
  49. $this->assertTrue($catalogue->has('foo', 'domain1'));
  50. $this->assertTrue($catalogue->has('bar', 'domain2'));
  51. $this->assertFalse($catalogue->has('bar', 'domain1'));
  52. $this->assertFalse($catalogue->has('foo', 'domain88'));
  53. }
  54. public function testGetSet()
  55. {
  56. $catalogue = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo'], 'domain2' => ['bar' => 'bar'], 'domain2+intl-icu' => ['bar' => 'foo']]);
  57. $catalogue->set('foo1', 'foo1', 'domain1');
  58. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  59. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  60. $this->assertEquals('foo', $catalogue->get('bar', 'domain2'));
  61. }
  62. public function testAdd()
  63. {
  64. $catalogue = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo'], 'domain2' => ['bar' => 'bar']]);
  65. $catalogue->add(['foo1' => 'foo1'], 'domain1');
  66. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  67. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  68. $catalogue->add(['foo' => 'bar'], 'domain1');
  69. $this->assertEquals('bar', $catalogue->get('foo', 'domain1'));
  70. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  71. $catalogue->add(['foo' => 'bar'], 'domain88');
  72. $this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
  73. }
  74. public function testReplace()
  75. {
  76. $catalogue = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo'], 'domain1+intl-icu' => ['bar' => 'bar']]);
  77. $catalogue->replace($messages = ['foo1' => 'foo1'], 'domain1');
  78. $this->assertEquals($messages, $catalogue->all('domain1'));
  79. }
  80. public function testAddCatalogue()
  81. {
  82. $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  83. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  84. $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  85. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  86. $catalogue = new MessageCatalogue('en', ['domain1' => ['foo' => 'foo']]);
  87. $catalogue->addResource($r);
  88. $catalogue1 = new MessageCatalogue('en', ['domain1' => ['foo1' => 'foo1'], 'domain2+intl-icu' => ['bar' => 'bar']]);
  89. $catalogue1->addResource($r1);
  90. $catalogue->addCatalogue($catalogue1);
  91. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  92. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  93. $this->assertEquals('bar', $catalogue->get('bar', 'domain2'));
  94. $this->assertEquals('bar', $catalogue->get('bar', 'domain2+intl-icu'));
  95. $this->assertEquals([$r, $r1], $catalogue->getResources());
  96. }
  97. public function testAddFallbackCatalogue()
  98. {
  99. $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  100. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  101. $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  102. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  103. $r2 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  104. $r2->expects($this->any())->method('__toString')->will($this->returnValue('r2'));
  105. $catalogue = new MessageCatalogue('fr_FR', ['domain1' => ['foo' => 'foo'], 'domain2' => ['bar' => 'bar']]);
  106. $catalogue->addResource($r);
  107. $catalogue1 = new MessageCatalogue('fr', ['domain1' => ['foo' => 'bar', 'foo1' => 'foo1']]);
  108. $catalogue1->addResource($r1);
  109. $catalogue2 = new MessageCatalogue('en');
  110. $catalogue2->addResource($r2);
  111. $catalogue->addFallbackCatalogue($catalogue1);
  112. $catalogue1->addFallbackCatalogue($catalogue2);
  113. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  114. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  115. $this->assertEquals([$r, $r1, $r2], $catalogue->getResources());
  116. }
  117. /**
  118. * @expectedException \Symfony\Component\Translation\Exception\LogicException
  119. */
  120. public function testAddFallbackCatalogueWithParentCircularReference()
  121. {
  122. $main = new MessageCatalogue('en_US');
  123. $fallback = new MessageCatalogue('fr_FR');
  124. $fallback->addFallbackCatalogue($main);
  125. $main->addFallbackCatalogue($fallback);
  126. }
  127. /**
  128. * @expectedException \Symfony\Component\Translation\Exception\LogicException
  129. */
  130. public function testAddFallbackCatalogueWithFallbackCircularReference()
  131. {
  132. $fr = new MessageCatalogue('fr');
  133. $en = new MessageCatalogue('en');
  134. $es = new MessageCatalogue('es');
  135. $fr->addFallbackCatalogue($en);
  136. $es->addFallbackCatalogue($en);
  137. $en->addFallbackCatalogue($fr);
  138. }
  139. /**
  140. * @expectedException \Symfony\Component\Translation\Exception\LogicException
  141. */
  142. public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
  143. {
  144. $catalogue = new MessageCatalogue('en');
  145. $catalogue->addCatalogue(new MessageCatalogue('fr', []));
  146. }
  147. public function testGetAddResource()
  148. {
  149. $catalogue = new MessageCatalogue('en');
  150. $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  151. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  152. $catalogue->addResource($r);
  153. $catalogue->addResource($r);
  154. $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  155. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  156. $catalogue->addResource($r1);
  157. $this->assertEquals([$r, $r1], $catalogue->getResources());
  158. }
  159. public function testMetadataDelete()
  160. {
  161. $catalogue = new MessageCatalogue('en');
  162. $this->assertEquals([], $catalogue->getMetadata('', ''), 'Metadata is empty');
  163. $catalogue->deleteMetadata('key', 'messages');
  164. $catalogue->deleteMetadata('', 'messages');
  165. $catalogue->deleteMetadata();
  166. }
  167. public function testMetadataSetGetDelete()
  168. {
  169. $catalogue = new MessageCatalogue('en');
  170. $catalogue->setMetadata('key', 'value');
  171. $this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
  172. $catalogue->setMetadata('key2', []);
  173. $this->assertEquals([], $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
  174. $catalogue->deleteMetadata('key2', 'messages');
  175. $this->assertNull($catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
  176. $catalogue->deleteMetadata('key2', 'domain');
  177. $this->assertNull($catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
  178. }
  179. public function testMetadataMerge()
  180. {
  181. $cat1 = new MessageCatalogue('en');
  182. $cat1->setMetadata('a', 'b');
  183. $this->assertEquals(['messages' => ['a' => 'b']], $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
  184. $cat2 = new MessageCatalogue('en');
  185. $cat2->setMetadata('b', 'c', 'domain');
  186. $this->assertEquals(['domain' => ['b' => 'c']], $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
  187. $cat1->addCatalogue($cat2);
  188. $this->assertEquals(['messages' => ['a' => 'b'], 'domain' => ['b' => 'c']], $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
  189. }
  190. }