PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

https://github.com/fabpot/symfony
PHP | 210 lines | 164 code | 38 blank | 8 comment | 1 complexity | f37f9397be98d9f76c6bdb024a7978fd 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\Bundle\FrameworkBundle\Tests\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
  13. use Symfony\Bundle\FrameworkBundle\Console\Application;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. use Symfony\Component\DependencyInjection\Container;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. class TranslationDebugCommandTest extends TestCase
  18. {
  19. private $fs;
  20. private $translationDir;
  21. public function testDebugMissingMessages()
  22. {
  23. $tester = $this->createCommandTester(['foo' => 'foo']);
  24. $res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);
  25. $this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
  26. $this->assertEquals(TranslationDebugCommand::EXIT_CODE_MISSING, $res);
  27. }
  28. public function testDebugUnusedMessages()
  29. {
  30. $tester = $this->createCommandTester([], ['foo' => 'foo']);
  31. $res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);
  32. $this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
  33. $this->assertEquals(TranslationDebugCommand::EXIT_CODE_UNUSED, $res);
  34. }
  35. public function testDebugFallbackMessages()
  36. {
  37. $tester = $this->createCommandTester(['foo' => 'foo'], ['foo' => 'foo']);
  38. $res = $tester->execute(['locale' => 'fr', 'bundle' => 'foo']);
  39. $this->assertMatchesRegularExpression('/fallback/', $tester->getDisplay());
  40. $this->assertEquals(TranslationDebugCommand::EXIT_CODE_FALLBACK, $res);
  41. }
  42. public function testNoDefinedMessages()
  43. {
  44. $tester = $this->createCommandTester();
  45. $res = $tester->execute(['locale' => 'fr', 'bundle' => 'test']);
  46. $this->assertMatchesRegularExpression('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
  47. $this->assertEquals(TranslationDebugCommand::EXIT_CODE_GENERAL_ERROR, $res);
  48. }
  49. public function testDebugDefaultDirectory()
  50. {
  51. $tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar']);
  52. $res = $tester->execute(['locale' => 'en']);
  53. $expectedExitStatus = TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED;
  54. $this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
  55. $this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
  56. $this->assertEquals($expectedExitStatus, $res);
  57. }
  58. public function testDebugDefaultRootDirectory()
  59. {
  60. $this->fs->remove($this->translationDir);
  61. $this->fs = new Filesystem();
  62. $this->translationDir = sys_get_temp_dir().'/'.uniqid('sf_translation', true);
  63. $this->fs->mkdir($this->translationDir.'/translations');
  64. $this->fs->mkdir($this->translationDir.'/templates');
  65. $expectedExitStatus = TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED;
  66. $tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar'], null, [$this->translationDir.'/trans'], [$this->translationDir.'/views']);
  67. $res = $tester->execute(['locale' => 'en']);
  68. $this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
  69. $this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
  70. $this->assertEquals($expectedExitStatus, $res);
  71. }
  72. public function testDebugCustomDirectory()
  73. {
  74. $this->fs->mkdir($this->translationDir.'/customDir/translations');
  75. $this->fs->mkdir($this->translationDir.'/customDir/templates');
  76. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  77. $kernel->expects($this->once())
  78. ->method('getBundle')
  79. ->with($this->equalTo($this->translationDir.'/customDir'))
  80. ->willThrowException(new \InvalidArgumentException());
  81. $expectedExitStatus = TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED;
  82. $tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar'], $kernel);
  83. $res = $tester->execute(['locale' => 'en', 'bundle' => $this->translationDir.'/customDir']);
  84. $this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
  85. $this->assertMatchesRegularExpression('/unused/', $tester->getDisplay());
  86. $this->assertEquals($expectedExitStatus, $res);
  87. }
  88. public function testDebugInvalidDirectory()
  89. {
  90. $this->expectException('InvalidArgumentException');
  91. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  92. $kernel->expects($this->once())
  93. ->method('getBundle')
  94. ->with($this->equalTo('dir'))
  95. ->willThrowException(new \InvalidArgumentException());
  96. $tester = $this->createCommandTester([], [], $kernel);
  97. $tester->execute(['locale' => 'en', 'bundle' => 'dir']);
  98. }
  99. protected function setUp(): void
  100. {
  101. $this->fs = new Filesystem();
  102. $this->translationDir = sys_get_temp_dir().'/'.uniqid('sf_translation', true);
  103. $this->fs->mkdir($this->translationDir.'/translations');
  104. $this->fs->mkdir($this->translationDir.'/templates');
  105. }
  106. protected function tearDown(): void
  107. {
  108. $this->fs->remove($this->translationDir);
  109. }
  110. private function createCommandTester($extractedMessages = [], $loadedMessages = [], $kernel = null, array $transPaths = [], array $viewsPaths = []): CommandTester
  111. {
  112. $translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $translator
  116. ->expects($this->any())
  117. ->method('getFallbackLocales')
  118. ->willReturn(['en']);
  119. $extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
  120. $extractor
  121. ->expects($this->any())
  122. ->method('extract')
  123. ->willReturnCallback(
  124. function ($path, $catalogue) use ($extractedMessages) {
  125. $catalogue->add($extractedMessages);
  126. }
  127. );
  128. $loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
  129. $loader
  130. ->expects($this->any())
  131. ->method('read')
  132. ->willReturnCallback(
  133. function ($path, $catalogue) use ($loadedMessages) {
  134. $catalogue->add($loadedMessages);
  135. }
  136. );
  137. if (null === $kernel) {
  138. $returnValues = [
  139. ['foo', $this->getBundle($this->translationDir)],
  140. ['test', $this->getBundle('test')],
  141. ];
  142. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  143. $kernel
  144. ->expects($this->any())
  145. ->method('getBundle')
  146. ->willReturnMap($returnValues);
  147. }
  148. $kernel
  149. ->expects($this->any())
  150. ->method('getBundles')
  151. ->willReturn([]);
  152. $container = new Container();
  153. $kernel
  154. ->expects($this->any())
  155. ->method('getContainer')
  156. ->willReturn($container);
  157. $command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $viewsPaths);
  158. $application = new Application($kernel);
  159. $application->add($command);
  160. return new CommandTester($application->find('debug:translation'));
  161. }
  162. private function getBundle($path)
  163. {
  164. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
  165. $bundle
  166. ->expects($this->any())
  167. ->method('getPath')
  168. ->willReturn($path)
  169. ;
  170. return $bundle;
  171. }
  172. }