TranslationTranslatorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Illuminate\Tests\Translation;
  3. use Illuminate\Contracts\Translation\Loader;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Translation\MessageSelector;
  6. use Illuminate\Translation\Translator;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class TranslationTranslatorTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testHasMethodReturnsFalseWhenReturnedTranslationIsNull()
  16. {
  17. $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();
  18. $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'))->willReturn('foo');
  19. $this->assertFalse($t->has('foo', 'bar'));
  20. $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en', 'sp'])->getMock();
  21. $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'))->willReturn('bar');
  22. $this->assertTrue($t->has('foo', 'bar'));
  23. $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();
  24. $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'), false)->willReturn('bar');
  25. $this->assertTrue($t->hasForLocale('foo', 'bar'));
  26. $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();
  27. $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo([]), $this->equalTo('bar'), false)->willReturn('foo');
  28. $this->assertFalse($t->hasForLocale('foo', 'bar'));
  29. $t = new Translator($this->getLoader(), 'en');
  30. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  31. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['foo' => 'bar']);
  32. $this->assertTrue($t->hasForLocale('foo'));
  33. $t = new Translator($this->getLoader(), 'en');
  34. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  35. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn([]);
  36. $this->assertFalse($t->hasForLocale('foo'));
  37. }
  38. public function testGetMethodProperlyLoadsAndRetrievesItem()
  39. {
  40. $t = new Translator($this->getLoader(), 'en');
  41. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  42. $t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :foo', 'qux' => ['tree :foo', 'breeze :foo']]);
  43. $this->assertEquals(['tree bar', 'breeze bar'], $t->get('foo::bar.qux', ['foo' => 'bar'], 'en'));
  44. $this->assertSame('breeze bar', $t->get('foo::bar.baz', ['foo' => 'bar'], 'en'));
  45. $this->assertSame('foo', $t->get('foo::bar.foo'));
  46. }
  47. public function testGetMethodForNonExistingReturnsSameKey()
  48. {
  49. $t = new Translator($this->getLoader(), 'en');
  50. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  51. $t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :foo', 'qux' => ['tree :foo', 'breeze :foo']]);
  52. $t->getLoader()->shouldReceive('load')->once()->with('en', 'unknown', 'foo')->andReturn([]);
  53. $this->assertSame('foo::unknown', $t->get('foo::unknown', ['foo' => 'bar'], 'en'));
  54. $this->assertSame('foo::bar.unknown', $t->get('foo::bar.unknown', ['foo' => 'bar'], 'en'));
  55. $this->assertSame('foo::unknown.bar', $t->get('foo::unknown.bar'));
  56. }
  57. public function testTransMethodProperlyLoadsAndRetrievesItemWithHTMLInTheMessage()
  58. {
  59. $t = new Translator($this->getLoader(), 'en');
  60. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  61. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['bar' => 'breeze <p>test</p>']);
  62. $this->assertSame('breeze <p>test</p>', $t->get('foo.bar', [], 'en'));
  63. }
  64. public function testGetMethodProperlyLoadsAndRetrievesItemWithCapitalization()
  65. {
  66. $t = $this->getMockBuilder(Translator::class)->onlyMethods([])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();
  67. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  68. $t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :0 :Foo :BAR']);
  69. $this->assertSame('breeze john Bar FOO', $t->get('foo::bar.baz', ['john', 'foo' => 'bar', 'bar' => 'foo'], 'en'));
  70. $this->assertSame('foo', $t->get('foo::bar.foo'));
  71. }
  72. public function testGetMethodProperlyLoadsAndRetrievesItemWithLongestReplacementsFirst()
  73. {
  74. $t = new Translator($this->getLoader(), 'en');
  75. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  76. $t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :foo :foobar']);
  77. $this->assertSame('breeze bar taylor', $t->get('foo::bar.baz', ['foo' => 'bar', 'foobar' => 'taylor'], 'en'));
  78. $this->assertSame('breeze foo bar baz taylor', $t->get('foo::bar.baz', ['foo' => 'foo bar baz', 'foobar' => 'taylor'], 'en'));
  79. $this->assertSame('foo', $t->get('foo::bar.foo'));
  80. }
  81. public function testGetMethodProperlyLoadsAndRetrievesItemForFallback()
  82. {
  83. $t = new Translator($this->getLoader(), 'en');
  84. $t->setFallback('lv');
  85. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  86. $t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn([]);
  87. $t->getLoader()->shouldReceive('load')->once()->with('lv', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :foo']);
  88. $this->assertSame('breeze bar', $t->get('foo::bar.baz', ['foo' => 'bar'], 'en'));
  89. $this->assertSame('foo', $t->get('foo::bar.foo'));
  90. }
  91. public function testGetMethodProperlyLoadsAndRetrievesItemForGlobalNamespace()
  92. {
  93. $t = new Translator($this->getLoader(), 'en');
  94. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  95. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['bar' => 'breeze :foo']);
  96. $this->assertSame('breeze bar', $t->get('foo.bar', ['foo' => 'bar']));
  97. }
  98. public function testChoiceMethodProperlyLoadsAndRetrievesItem()
  99. {
  100. $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();
  101. $t->expects($this->once())->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line');
  102. $t->setSelector($selector = m::mock(MessageSelector::class));
  103. $selector->shouldReceive('choose')->once()->with('line', 10, 'en')->andReturn('choiced');
  104. $t->choice('foo', 10, ['replace']);
  105. }
  106. public function testChoiceMethodProperlyCountsCollectionsAndLoadsAndRetrievesItem()
  107. {
  108. $t = $this->getMockBuilder(Translator::class)->onlyMethods(['get'])->setConstructorArgs([$this->getLoader(), 'en'])->getMock();
  109. $t->expects($this->exactly(2))->method('get')->with($this->equalTo('foo'), $this->equalTo(['replace']), $this->equalTo('en'))->willReturn('line');
  110. $t->setSelector($selector = m::mock(MessageSelector::class));
  111. $selector->shouldReceive('choose')->twice()->with('line', 3, 'en')->andReturn('choiced');
  112. $values = ['foo', 'bar', 'baz'];
  113. $t->choice('foo', $values, ['replace']);
  114. $values = new Collection(['foo', 'bar', 'baz']);
  115. $t->choice('foo', $values, ['replace']);
  116. }
  117. public function testGetJson()
  118. {
  119. $t = new Translator($this->getLoader(), 'en');
  120. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['foo' => 'one']);
  121. $this->assertSame('one', $t->get('foo'));
  122. }
  123. public function testGetJsonReplaces()
  124. {
  125. $t = new Translator($this->getLoader(), 'en');
  126. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['foo :i:c :u' => 'bar :i:c :u']);
  127. $this->assertSame('bar onetwo three', $t->get('foo :i:c :u', ['i' => 'one', 'c' => 'two', 'u' => 'three']));
  128. }
  129. public function testGetJsonHasAtomicReplacements()
  130. {
  131. $t = new Translator($this->getLoader(), 'en');
  132. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['Hello :foo!' => 'Hello :foo!']);
  133. $this->assertSame('Hello baz:bar!', $t->get('Hello :foo!', ['foo' => 'baz:bar', 'bar' => 'abcdef']));
  134. }
  135. public function testGetJsonReplacesForAssociativeInput()
  136. {
  137. $t = new Translator($this->getLoader(), 'en');
  138. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['foo :i :c' => 'bar :i :c']);
  139. $this->assertSame('bar eye see', $t->get('foo :i :c', ['i' => 'eye', 'c' => 'see']));
  140. }
  141. public function testGetJsonPreservesOrder()
  142. {
  143. $t = new Translator($this->getLoader(), 'en');
  144. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['to :name I give :greeting' => ':greeting :name']);
  145. $this->assertSame('Greetings David', $t->get('to :name I give :greeting', ['name' => 'David', 'greeting' => 'Greetings']));
  146. }
  147. public function testGetJsonForNonExistingJsonKeyLooksForRegularKeys()
  148. {
  149. $t = new Translator($this->getLoader(), 'en');
  150. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  151. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['bar' => 'one']);
  152. $this->assertSame('one', $t->get('foo.bar'));
  153. }
  154. public function testGetJsonForNonExistingJsonKeyLooksForRegularKeysAndReplace()
  155. {
  156. $t = new Translator($this->getLoader(), 'en');
  157. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  158. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo', '*')->andReturn(['bar' => 'one :message']);
  159. $this->assertSame('one two', $t->get('foo.bar', ['message' => 'two']));
  160. }
  161. public function testGetJsonForNonExistingReturnsSameKey()
  162. {
  163. $t = new Translator($this->getLoader(), 'en');
  164. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  165. $t->getLoader()->shouldReceive('load')->once()->with('en', 'Foo that bar', '*')->andReturn([]);
  166. $this->assertSame('Foo that bar', $t->get('Foo that bar'));
  167. }
  168. public function testGetJsonForNonExistingReturnsSameKeyAndReplaces()
  169. {
  170. $t = new Translator($this->getLoader(), 'en');
  171. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  172. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo :message', '*')->andReturn([]);
  173. $this->assertSame('foo baz', $t->get('foo :message', ['message' => 'baz']));
  174. }
  175. public function testEmptyFallbacks()
  176. {
  177. $t = new Translator($this->getLoader(), 'en');
  178. $t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn([]);
  179. $t->getLoader()->shouldReceive('load')->once()->with('en', 'foo :message', '*')->andReturn([]);
  180. $this->assertSame('foo ', $t->get('foo :message', ['message' => null]));
  181. }
  182. protected function getLoader()
  183. {
  184. return m::mock(Loader::class);
  185. }
  186. }