No Description

MessageCatalogueTest.php 8.7KB

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