TranslationUpdateCommandTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\TranslationUpdateCommand;
  13. use Symfony\Component\Console\Application;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. use Symfony\Component\DependencyInjection;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. use Symfony\Component\HttpKernel;
  18. class TranslationUpdateCommandTest extends TestCase
  19. {
  20. private $fs;
  21. private $translationDir;
  22. public function testDumpMessagesAndClean()
  23. {
  24. $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
  25. $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true));
  26. $this->assertRegExp('/foo/', $tester->getDisplay());
  27. $this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
  28. }
  29. public function testDumpTwoMessagesAndClean()
  30. {
  31. $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo', 'bar' => 'bar')));
  32. $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true));
  33. $this->assertRegExp('/foo/', $tester->getDisplay());
  34. $this->assertRegExp('/bar/', $tester->getDisplay());
  35. $this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay());
  36. }
  37. public function testWriteMessages()
  38. {
  39. $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
  40. $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true));
  41. $this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
  42. }
  43. protected function setUp()
  44. {
  45. $this->fs = new Filesystem();
  46. $this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
  47. $this->fs->mkdir($this->translationDir.'/Resources/translations');
  48. $this->fs->mkdir($this->translationDir.'/Resources/views');
  49. }
  50. protected function tearDown()
  51. {
  52. $this->fs->remove($this->translationDir);
  53. }
  54. /**
  55. * @return CommandTester
  56. */
  57. private function createCommandTester(DependencyInjection\ContainerInterface $container)
  58. {
  59. $command = new TranslationUpdateCommand();
  60. $command->setContainer($container);
  61. $application = new Application();
  62. $application->add($command);
  63. return new CommandTester($application->find('translation:update'));
  64. }
  65. private function getContainer($extractedMessages = array(), $loadedMessages = array(), HttpKernel\KernelInterface $kernel = null)
  66. {
  67. $translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $translator
  71. ->expects($this->any())
  72. ->method('getFallbackLocales')
  73. ->will($this->returnValue(array('en')));
  74. $extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
  75. $extractor
  76. ->expects($this->any())
  77. ->method('extract')
  78. ->will(
  79. $this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
  80. $catalogue->add($extractedMessages);
  81. })
  82. );
  83. $loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
  84. $loader
  85. ->expects($this->any())
  86. ->method('loadMessages')
  87. ->will(
  88. $this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
  89. $catalogue->add($loadedMessages);
  90. })
  91. );
  92. $writer = $this->getMockBuilder('Symfony\Component\Translation\Writer\TranslationWriter')->getMock();
  93. $writer
  94. ->expects($this->any())
  95. ->method('getFormats')
  96. ->will(
  97. $this->returnValue(array('xlf', 'yml'))
  98. );
  99. if (null === $kernel) {
  100. $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
  101. $kernel
  102. ->expects($this->any())
  103. ->method('getBundle')
  104. ->will($this->returnValueMap(array(
  105. array('foo', true, $this->getBundle($this->translationDir)),
  106. array('test', true, $this->getBundle('test')),
  107. )));
  108. }
  109. $kernel
  110. ->expects($this->any())
  111. ->method('getRootDir')
  112. ->will($this->returnValue($this->translationDir));
  113. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  114. $container
  115. ->expects($this->any())
  116. ->method('get')
  117. ->will($this->returnValueMap(array(
  118. array('translation.extractor', 1, $extractor),
  119. array('translation.loader', 1, $loader),
  120. array('translation.writer', 1, $writer),
  121. array('translator', 1, $translator),
  122. array('kernel', 1, $kernel),
  123. )));
  124. return $container;
  125. }
  126. private function getBundle($path)
  127. {
  128. $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
  129. $bundle
  130. ->expects($this->any())
  131. ->method('getPath')
  132. ->will($this->returnValue($path))
  133. ;
  134. return $bundle;
  135. }
  136. }