MongoDbMessageSourceTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace yiiunit\extensions\mongodb\i18n;
  3. use yii\i18n\I18N;
  4. use yii\mongodb\i18n\MongoDbMessageSource;
  5. use yiiunit\extensions\mongodb\TestCase;
  6. class MongoDbMessageSourceTest extends TestCase
  7. {
  8. /**
  9. * @var I18N
  10. */
  11. public $i18n;
  12. protected function setUp()
  13. {
  14. $this->mockApplication();
  15. parent::setUp();
  16. $this->setupTestRows();
  17. $this->setupI18N();
  18. }
  19. protected function tearDown()
  20. {
  21. $this->dropCollection('message');
  22. parent::tearDown();
  23. }
  24. /**
  25. * Setup test rows.
  26. */
  27. protected function setupTestRows()
  28. {
  29. $db = $this->getConnection();
  30. $collection = $db->getCollection('message');
  31. $collection->batchInsert([
  32. [
  33. 'language' => 'de',
  34. 'category' => 'test',
  35. 'messages' => [
  36. 'Hello world!' => 'Hallo Welt!'
  37. ],
  38. ],
  39. [
  40. 'language' => 'de-DE',
  41. 'category' => 'test',
  42. 'messages' => [
  43. [
  44. 'message' => 'The dog runs fast.',
  45. 'translation' => 'Der Hund rennt schnell.'
  46. ],
  47. [
  48. 'message' => 'His speed is about {n} km/h.',
  49. 'translation' => 'Seine Geschwindigkeit beträgt {n} km/h.'
  50. ],
  51. [
  52. 'message' => 'His name is {name} and his speed is about {n, number} km/h.',
  53. 'translation' => 'Er heißt {name} und ist {n, number} km/h schnell.'
  54. ],
  55. ],
  56. ],
  57. [
  58. 'language' => 'en-US',
  59. 'category' => 'test',
  60. 'messages' => [
  61. [
  62. 'message' => 'The dog runs fast.',
  63. 'translation' => 'The dog runs fast (en-US).'
  64. ]
  65. ],
  66. ],
  67. [
  68. 'language' => 'ru-RU',
  69. 'category' => 'test',
  70. 'messages' => [
  71. 'Hello world!' => 'Здравствуй Мир! (ru-RU)'
  72. ],
  73. ],
  74. [
  75. 'language' => 'ru',
  76. 'category' => 'test',
  77. 'messages' => [
  78. 'Hello world!' => 'Здравствуй Мир!',
  79. [
  80. 'message' => 'The dog runs fast.',
  81. 'translation' => 'Собака бегает быстро.',
  82. ],
  83. [
  84. 'message' => 'There {n, plural, =0{no cats} =1{one cat} other{are # cats}} on lying on the sofa!',
  85. 'translation' => 'На диване {n, plural, =0{нет кошек} =1{лежит одна кошка} one{лежит # кошка} few{лежит # кошки} many{лежит # кошек} other{лежит # кошки}}!'
  86. ],
  87. ],
  88. ],
  89. ]);
  90. }
  91. /**
  92. * Setup internal test [[I18N]] instance
  93. */
  94. protected function setupI18N()
  95. {
  96. $this->i18n = new I18N([
  97. 'translations' => [
  98. '*' => new MongoDbMessageSource([
  99. 'db' => $this->getConnection(),
  100. 'sourceLanguage' => 'en-US',
  101. ])
  102. ]
  103. ]);
  104. }
  105. // Tests :
  106. public function testTranslate()
  107. {
  108. $msg = 'The dog runs fast.';
  109. // source = target. Should be returned as is.
  110. $this->assertEquals('The dog runs fast.', $this->i18n->translate('test', $msg, [], 'en-US'));
  111. // exact match
  112. $this->assertEquals('Der Hund rennt schnell.', $this->i18n->translate('test', $msg, [], 'de-DE'));
  113. // fallback to just language code with absent exact match
  114. $this->assertEquals('Собака бегает быстро.', $this->i18n->translate('test', $msg, [], 'ru-RU'));
  115. // fallback to just langauge code with present exact match
  116. $this->assertEquals('Hallo Welt!', $this->i18n->translate('test', 'Hello world!', [], 'de-DE'));
  117. }
  118. /**
  119. * @depends testTranslate
  120. */
  121. public function testTranslatePriority()
  122. {
  123. // Ensure fallback entry does not override main one:
  124. $this->assertEquals('Здравствуй Мир! (ru-RU)', $this->i18n->translate('test', 'Hello world!', [], 'ru-RU'));
  125. }
  126. }