TranslationDebugCommandTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\Bundle\FrameworkBundle\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. class TranslationDebugCommandTest extends TestCase
  17. {
  18. private $fs;
  19. private $translationDir;
  20. public function testDebugMissingMessages()
  21. {
  22. $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
  23. $tester->execute(array('locale' => 'en', 'bundle' => 'foo'));
  24. $this->assertRegExp('/missing/', $tester->getDisplay());
  25. }
  26. public function testDebugUnusedMessages()
  27. {
  28. $tester = $this->createCommandTester($this->getContainer(array(), array('foo' => 'foo')));
  29. $tester->execute(array('locale' => 'en', 'bundle' => 'foo'));
  30. $this->assertRegExp('/unused/', $tester->getDisplay());
  31. }
  32. public function testDebugFallbackMessages()
  33. {
  34. $tester = $this->createCommandTester($this->getContainer(array(), array('foo' => 'foo')));
  35. $tester->execute(array('locale' => 'fr', 'bundle' => 'foo'));
  36. $this->assertRegExp('/fallback/', $tester->getDisplay());
  37. }
  38. public function testNoDefinedMessages()
  39. {
  40. $tester = $this->createCommandTester($this->getContainer());
  41. $tester->execute(array('locale' => 'fr', 'bundle' => 'test'));
  42. $this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
  43. }
  44. public function testDebugDefaultDirectory()
  45. {
  46. $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo'), array('bar' => 'bar')));
  47. $tester->execute(array('locale' => 'en'));
  48. $this->assertRegExp('/missing/', $tester->getDisplay());
  49. $this->assertRegExp('/unused/', $tester->getDisplay());
  50. }
  51. public function testDebugCustomDirectory()
  52. {
  53. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  54. $kernel->expects($this->once())
  55. ->method('getBundle')
  56. ->with($this->equalTo($this->translationDir))
  57. ->willThrowException(new \InvalidArgumentException());
  58. $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo'), array('bar' => 'bar'), $kernel));
  59. $tester->execute(array('locale' => 'en', 'bundle' => $this->translationDir));
  60. $this->assertRegExp('/missing/', $tester->getDisplay());
  61. $this->assertRegExp('/unused/', $tester->getDisplay());
  62. }
  63. /**
  64. * @expectedException \InvalidArgumentException
  65. */
  66. public function testDebugInvalidDirectory()
  67. {
  68. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  69. $kernel->expects($this->once())
  70. ->method('getBundle')
  71. ->with($this->equalTo('dir'))
  72. ->will($this->throwException(new \InvalidArgumentException()));
  73. $tester = $this->createCommandTester($this->getContainer(array(), array(), $kernel));
  74. $tester->execute(array('locale' => 'en', 'bundle' => 'dir'));
  75. }
  76. protected function setUp()
  77. {
  78. $this->fs = new Filesystem();
  79. $this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
  80. $this->fs->mkdir($this->translationDir.'/Resources/translations');
  81. $this->fs->mkdir($this->translationDir.'/Resources/views');
  82. }
  83. protected function tearDown()
  84. {
  85. $this->fs->remove($this->translationDir);
  86. }
  87. /**
  88. * @return CommandTester
  89. */
  90. private function createCommandTester($container)
  91. {
  92. $command = new TranslationDebugCommand();
  93. $command->setContainer($container);
  94. $application = new Application();
  95. $application->add($command);
  96. return new CommandTester($application->find('debug:translation'));
  97. }
  98. private function getContainer($extractedMessages = array(), $loadedMessages = array(), $kernel = null)
  99. {
  100. $translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
  101. ->disableOriginalConstructor()
  102. ->getMock();
  103. $translator
  104. ->expects($this->any())
  105. ->method('getFallbackLocales')
  106. ->will($this->returnValue(array('en')));
  107. $extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
  108. $extractor
  109. ->expects($this->any())
  110. ->method('extract')
  111. ->will(
  112. $this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
  113. $catalogue->add($extractedMessages);
  114. })
  115. );
  116. $loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
  117. $loader
  118. ->expects($this->any())
  119. ->method('loadMessages')
  120. ->will(
  121. $this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
  122. $catalogue->add($loadedMessages);
  123. })
  124. );
  125. if (null === $kernel) {
  126. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  127. $kernel
  128. ->expects($this->any())
  129. ->method('getBundle')
  130. ->will($this->returnValueMap(array(
  131. array('foo', true, $this->getBundle($this->translationDir)),
  132. array('test', true, $this->getBundle('test')),
  133. )));
  134. }
  135. $kernel
  136. ->expects($this->any())
  137. ->method('getRootDir')
  138. ->will($this->returnValue($this->translationDir));
  139. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  140. $container
  141. ->expects($this->any())
  142. ->method('get')
  143. ->will($this->returnValueMap(array(
  144. array('translation.extractor', 1, $extractor),
  145. array('translation.loader', 1, $loader),
  146. array('translator', 1, $translator),
  147. array('kernel', 1, $kernel),
  148. )));
  149. return $container;
  150. }
  151. private function getBundle($path)
  152. {
  153. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
  154. $bundle
  155. ->expects($this->any())
  156. ->method('getPath')
  157. ->will($this->returnValue($path))
  158. ;
  159. return $bundle;
  160. }
  161. }