菜谱项目

MessageCatalogueTest.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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', array('domain1' => array(), 'domain2' => array()));
  23. $this->assertEquals(array('domain1', 'domain2'), $catalogue->getDomains());
  24. }
  25. public function testAll()
  26. {
  27. $catalogue = new MessageCatalogue('en', $messages = array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  28. $this->assertEquals(array('foo' => 'foo'), $catalogue->all('domain1'));
  29. $this->assertEquals(array(), $catalogue->all('domain88'));
  30. $this->assertEquals($messages, $catalogue->all());
  31. }
  32. public function testHas()
  33. {
  34. $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  35. $this->assertTrue($catalogue->has('foo', 'domain1'));
  36. $this->assertFalse($catalogue->has('bar', 'domain1'));
  37. $this->assertFalse($catalogue->has('foo', 'domain88'));
  38. }
  39. public function testGetSet()
  40. {
  41. $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  42. $catalogue->set('foo1', 'foo1', 'domain1');
  43. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  44. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  45. }
  46. public function testAdd()
  47. {
  48. $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  49. $catalogue->add(array('foo1' => 'foo1'), 'domain1');
  50. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  51. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  52. $catalogue->add(array('foo' => 'bar'), 'domain1');
  53. $this->assertEquals('bar', $catalogue->get('foo', 'domain1'));
  54. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  55. $catalogue->add(array('foo' => 'bar'), 'domain88');
  56. $this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
  57. }
  58. public function testReplace()
  59. {
  60. $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  61. $catalogue->replace($messages = array('foo1' => 'foo1'), 'domain1');
  62. $this->assertEquals($messages, $catalogue->all('domain1'));
  63. }
  64. public function testAddCatalogue()
  65. {
  66. $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  67. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  68. $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  69. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  70. $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  71. $catalogue->addResource($r);
  72. $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo1' => 'foo1')));
  73. $catalogue1->addResource($r1);
  74. $catalogue->addCatalogue($catalogue1);
  75. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  76. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  77. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  78. }
  79. public function testAddFallbackCatalogue()
  80. {
  81. $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  82. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  83. $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  84. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  85. $r2 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  86. $r2->expects($this->any())->method('__toString')->will($this->returnValue('r2'));
  87. $catalogue = new MessageCatalogue('fr_FR', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  88. $catalogue->addResource($r);
  89. $catalogue1 = new MessageCatalogue('fr', array('domain1' => array('foo' => 'bar', 'foo1' => 'foo1')));
  90. $catalogue1->addResource($r1);
  91. $catalogue2 = new MessageCatalogue('en');
  92. $catalogue2->addResource($r2);
  93. $catalogue->addFallbackCatalogue($catalogue1);
  94. $catalogue1->addFallbackCatalogue($catalogue2);
  95. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  96. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  97. $this->assertEquals(array($r, $r1, $r2), $catalogue->getResources());
  98. }
  99. /**
  100. * @expectedException \Symfony\Component\Translation\Exception\LogicException
  101. */
  102. public function testAddFallbackCatalogueWithParentCircularReference()
  103. {
  104. $main = new MessageCatalogue('en_US');
  105. $fallback = new MessageCatalogue('fr_FR');
  106. $fallback->addFallbackCatalogue($main);
  107. $main->addFallbackCatalogue($fallback);
  108. }
  109. /**
  110. * @expectedException \Symfony\Component\Translation\Exception\LogicException
  111. */
  112. public function testAddFallbackCatalogueWithFallbackCircularReference()
  113. {
  114. $fr = new MessageCatalogue('fr');
  115. $en = new MessageCatalogue('en');
  116. $es = new MessageCatalogue('es');
  117. $fr->addFallbackCatalogue($en);
  118. $es->addFallbackCatalogue($en);
  119. $en->addFallbackCatalogue($fr);
  120. }
  121. /**
  122. * @expectedException \Symfony\Component\Translation\Exception\LogicException
  123. */
  124. public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
  125. {
  126. $catalogue = new MessageCatalogue('en');
  127. $catalogue->addCatalogue(new MessageCatalogue('fr', array()));
  128. }
  129. public function testGetAddResource()
  130. {
  131. $catalogue = new MessageCatalogue('en');
  132. $r = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  133. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  134. $catalogue->addResource($r);
  135. $catalogue->addResource($r);
  136. $r1 = $this->getMockBuilder('Symfony\Component\Config\Resource\ResourceInterface')->getMock();
  137. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  138. $catalogue->addResource($r1);
  139. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  140. }
  141. public function testMetadataDelete()
  142. {
  143. $catalogue = new MessageCatalogue('en');
  144. $this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
  145. $catalogue->deleteMetadata('key', 'messages');
  146. $catalogue->deleteMetadata('', 'messages');
  147. $catalogue->deleteMetadata();
  148. }
  149. public function testMetadataSetGetDelete()
  150. {
  151. $catalogue = new MessageCatalogue('en');
  152. $catalogue->setMetadata('key', 'value');
  153. $this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
  154. $catalogue->setMetadata('key2', array());
  155. $this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
  156. $catalogue->deleteMetadata('key2', 'messages');
  157. $this->assertNull($catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
  158. $catalogue->deleteMetadata('key2', 'domain');
  159. $this->assertNull($catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
  160. }
  161. public function testMetadataMerge()
  162. {
  163. $cat1 = new MessageCatalogue('en');
  164. $cat1->setMetadata('a', 'b');
  165. $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
  166. $cat2 = new MessageCatalogue('en');
  167. $cat2->setMetadata('b', 'c', 'domain');
  168. $this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
  169. $cat1->addCatalogue($cat2);
  170. $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
  171. }
  172. }