MessageCatalogueTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  66. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  67. $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  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->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  81. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  82. $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  83. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  84. $catalogue = new MessageCatalogue('en_US', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  85. $catalogue->addResource($r);
  86. $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo' => 'bar', 'foo1' => 'foo1')));
  87. $catalogue1->addResource($r1);
  88. $catalogue->addFallbackCatalogue($catalogue1);
  89. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  90. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  91. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  92. }
  93. /**
  94. * @expectedException \LogicException
  95. */
  96. public function testAddFallbackCatalogueWithParentCircularReference()
  97. {
  98. $main = new MessageCatalogue('en_US');
  99. $fallback = new MessageCatalogue('fr_FR');
  100. $fallback->addFallbackCatalogue($main);
  101. $main->addFallbackCatalogue($fallback);
  102. }
  103. /**
  104. * @expectedException \LogicException
  105. */
  106. public function testAddFallbackCatalogueWithFallbackCircularReference()
  107. {
  108. $fr = new MessageCatalogue('fr');
  109. $en = new MessageCatalogue('en');
  110. $es = new MessageCatalogue('es');
  111. $fr->addFallbackCatalogue($en);
  112. $es->addFallbackCatalogue($en);
  113. $en->addFallbackCatalogue($fr);
  114. }
  115. /**
  116. * @expectedException \LogicException
  117. */
  118. public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
  119. {
  120. $catalogue = new MessageCatalogue('en');
  121. $catalogue->addCatalogue(new MessageCatalogue('fr', array()));
  122. }
  123. public function testGetAddResource()
  124. {
  125. $catalogue = new MessageCatalogue('en');
  126. $r = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  127. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  128. $catalogue->addResource($r);
  129. $catalogue->addResource($r);
  130. $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  131. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  132. $catalogue->addResource($r1);
  133. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  134. }
  135. public function testMetadataDelete()
  136. {
  137. $catalogue = new MessageCatalogue('en');
  138. $this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
  139. $catalogue->deleteMetadata('key', 'messages');
  140. $catalogue->deleteMetadata('', 'messages');
  141. $catalogue->deleteMetadata();
  142. }
  143. public function testMetadataSetGetDelete()
  144. {
  145. $catalogue = new MessageCatalogue('en');
  146. $catalogue->setMetadata('key', 'value');
  147. $this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
  148. $catalogue->setMetadata('key2', array());
  149. $this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
  150. $catalogue->deleteMetadata('key2', 'messages');
  151. $this->assertNull($catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
  152. $catalogue->deleteMetadata('key2', 'domain');
  153. $this->assertNull($catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
  154. }
  155. public function testMetadataMerge()
  156. {
  157. $cat1 = new MessageCatalogue('en');
  158. $cat1->setMetadata('a', 'b');
  159. $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
  160. $cat2 = new MessageCatalogue('en');
  161. $cat2->setMetadata('b', 'c', 'domain');
  162. $this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
  163. $cat1->addCatalogue($cat2);
  164. $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
  165. }
  166. }