LanguageBundleTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\Intl\Tests\ResourceBundle;
  11. use Symfony\Component\Intl\ResourceBundle\LanguageBundle;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. */
  15. class LanguageBundleTest extends \PHPUnit_Framework_TestCase
  16. {
  17. const RES_DIR = '/base/lang';
  18. /**
  19. * @var LanguageBundle
  20. */
  21. private $bundle;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $reader;
  26. protected function setUp()
  27. {
  28. $this->reader = $this->getMock('Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface');
  29. $this->bundle = new LanguageBundle(self::RES_DIR, $this->reader);
  30. }
  31. public function testGetLanguageName()
  32. {
  33. $languages = array(
  34. 'de' => 'German',
  35. 'en' => 'English',
  36. );
  37. $this->reader->expects($this->once())
  38. ->method('readEntry')
  39. ->with(self::RES_DIR, 'en', array('Languages'))
  40. ->will($this->returnValue($languages));
  41. $this->assertSame('German', $this->bundle->getLanguageName('de', null, 'en'));
  42. }
  43. public function testGetLanguageNameWithRegion()
  44. {
  45. $languages = array(
  46. 'de' => 'German',
  47. 'en' => 'English',
  48. 'en_GB' => 'British English',
  49. );
  50. $this->reader->expects($this->once())
  51. ->method('readEntry')
  52. ->with(self::RES_DIR, 'en', array('Languages'))
  53. ->will($this->returnValue($languages));
  54. $this->assertSame('British English', $this->bundle->getLanguageName('en', 'GB', 'en'));
  55. }
  56. public function testGetLanguageNameWithUntranslatedRegion()
  57. {
  58. $languages = array(
  59. 'de' => 'German',
  60. 'en' => 'English',
  61. );
  62. $this->reader->expects($this->once())
  63. ->method('readEntry')
  64. ->with(self::RES_DIR, 'en', array('Languages'))
  65. ->will($this->returnValue($languages));
  66. $this->assertSame('English', $this->bundle->getLanguageName('en', 'US', 'en'));
  67. }
  68. public function testGetLanguageNames()
  69. {
  70. $sortedLanguages = array(
  71. 'en' => 'English',
  72. 'de' => 'German',
  73. );
  74. $this->reader->expects($this->once())
  75. ->method('readEntry')
  76. ->with(self::RES_DIR, 'en', array('Languages'))
  77. ->will($this->returnValue($sortedLanguages));
  78. $this->assertSame($sortedLanguages, $this->bundle->getLanguageNames('en'));
  79. }
  80. public function testGetScriptName()
  81. {
  82. $data = array(
  83. 'Languages' => array(
  84. 'de' => 'German',
  85. 'en' => 'English',
  86. ),
  87. 'Scripts' => array(
  88. 'Latn' => 'latin',
  89. 'Cyrl' => 'cyrillique',
  90. ),
  91. );
  92. $this->reader->expects($this->once())
  93. ->method('read')
  94. ->with(self::RES_DIR, 'en')
  95. ->will($this->returnValue($data));
  96. $this->assertSame('latin', $this->bundle->getScriptName('Latn', null, 'en'));
  97. }
  98. public function testGetScriptNameIncludedInLanguage()
  99. {
  100. $data = array(
  101. 'Languages' => array(
  102. 'de' => 'German',
  103. 'en' => 'English',
  104. 'zh_Hans' => 'Simplified Chinese',
  105. ),
  106. 'Scripts' => array(
  107. 'Latn' => 'latin',
  108. 'Cyrl' => 'cyrillique',
  109. ),
  110. );
  111. $this->reader->expects($this->once())
  112. ->method('read')
  113. ->with(self::RES_DIR, 'en')
  114. ->will($this->returnValue($data));
  115. // Null because the script is included in the language anyway
  116. $this->assertNull($this->bundle->getScriptName('Hans', 'zh', 'en'));
  117. }
  118. public function testGetScriptNameIncludedInLanguageInBraces()
  119. {
  120. $data = array(
  121. 'Languages' => array(
  122. 'de' => 'German',
  123. 'en' => 'English',
  124. 'zh_Hans' => 'Chinese (simplified)',
  125. ),
  126. 'Scripts' => array(
  127. 'Latn' => 'latin',
  128. 'Cyrl' => 'cyrillique',
  129. ),
  130. );
  131. $this->reader->expects($this->once())
  132. ->method('read')
  133. ->with(self::RES_DIR, 'en')
  134. ->will($this->returnValue($data));
  135. $this->assertSame('simplified', $this->bundle->getScriptName('Hans', 'zh', 'en'));
  136. }
  137. public function testGetScriptNameNoScriptsBlock()
  138. {
  139. $data = array(
  140. 'Languages' => array(
  141. 'de' => 'German',
  142. 'en' => 'English',
  143. ),
  144. );
  145. $this->reader->expects($this->once())
  146. ->method('read')
  147. ->with(self::RES_DIR, 'en')
  148. ->will($this->returnValue($data));
  149. $this->assertNull($this->bundle->getScriptName('Latn', null, 'en'));
  150. }
  151. public function testGetScriptNames()
  152. {
  153. $sortedScripts = array(
  154. 'Cyrl' => 'cyrillique',
  155. 'Latn' => 'latin',
  156. );
  157. $this->reader->expects($this->once())
  158. ->method('readEntry')
  159. ->with(self::RES_DIR, 'en', array('Scripts'))
  160. ->will($this->returnValue($sortedScripts));
  161. $this->assertSame($sortedScripts, $this->bundle->getScriptNames('en'));
  162. }
  163. }