/vendor/symfony/translation/Symfony/Component/Translation/Tests/MessageCatalogueTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 212 lines · 149 code · 49 blank · 14 comment · 3 complexity · d6fc69d28977cb90c4d2d3e8a94b118c MD5 · raw file

  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. if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
  66. $this->markTestSkipped('The "Config" component is not available');
  67. }
  68. $r = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  69. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  70. $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  71. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  72. $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  73. $catalogue->addResource($r);
  74. $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo1' => 'foo1')));
  75. $catalogue1->addResource($r1);
  76. $catalogue->addCatalogue($catalogue1);
  77. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  78. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  79. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  80. }
  81. public function testAddFallbackCatalogue()
  82. {
  83. if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
  84. $this->markTestSkipped('The "Config" component is not available');
  85. }
  86. $r = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  87. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  88. $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  89. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  90. $catalogue = new MessageCatalogue('en_US', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
  91. $catalogue->addResource($r);
  92. $catalogue1 = new MessageCatalogue('en', array('domain1' => array('foo' => 'bar', 'foo1' => 'foo1')));
  93. $catalogue1->addResource($r1);
  94. $catalogue->addFallbackCatalogue($catalogue1);
  95. $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
  96. $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
  97. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  98. }
  99. /**
  100. * @expectedException LogicException
  101. */
  102. public function testAddFallbackCatalogueWithCircularReference()
  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 LogicException
  111. */
  112. public function testAddCatalogueWhenLocaleIsNotTheSameAsTheCurrentOne()
  113. {
  114. $catalogue = new MessageCatalogue('en');
  115. $catalogue->addCatalogue(new MessageCatalogue('fr', array()));
  116. }
  117. public function testGetAddResource()
  118. {
  119. if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
  120. $this->markTestSkipped('The "Config" component is not available');
  121. }
  122. $catalogue = new MessageCatalogue('en');
  123. $r = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  124. $r->expects($this->any())->method('__toString')->will($this->returnValue('r'));
  125. $catalogue->addResource($r);
  126. $catalogue->addResource($r);
  127. $r1 = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
  128. $r1->expects($this->any())->method('__toString')->will($this->returnValue('r1'));
  129. $catalogue->addResource($r1);
  130. $this->assertEquals(array($r, $r1), $catalogue->getResources());
  131. }
  132. public function testMetadataDelete()
  133. {
  134. $catalogue = new MessageCatalogue('en');
  135. $this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
  136. $catalogue->deleteMetadata('key', 'messages');
  137. $catalogue->deleteMetadata('', 'messages');
  138. $catalogue->deleteMetadata();
  139. }
  140. public function testMetadataSetGetDelete()
  141. {
  142. $catalogue = new MessageCatalogue('en');
  143. $catalogue->setMetadata('key', 'value');
  144. $this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
  145. $catalogue->setMetadata('key2', array());
  146. $this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
  147. $catalogue->deleteMetadata('key2', 'messages');
  148. $this->assertEquals(null, $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
  149. $catalogue->deleteMetadata('key2', 'domain');
  150. $this->assertEquals(null, $catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
  151. }
  152. public function testMetadataMerge()
  153. {
  154. $cat1 = new MessageCatalogue('en');
  155. $cat1->setMetadata('a', 'b');
  156. $this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
  157. $cat2 = new MessageCatalogue('en');
  158. $cat2->setMetadata('b', 'c', 'domain');
  159. $this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
  160. $cat1->addCatalogue($cat2);
  161. $this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
  162. }
  163. }