2015-09-03 08:05:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
|
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-07-22 10:58:19 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
2015-09-03 08:05:21 +01:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
use Symfony\Component\HttpKernel;
|
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class TranslationUpdateCommandTest extends TestCase
|
2015-09-03 08:05:21 +01:00
|
|
|
{
|
|
|
|
private $fs;
|
|
|
|
private $translationDir;
|
|
|
|
|
|
|
|
public function testDumpMessagesAndClean()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
|
2015-09-03 08:05:21 +01:00
|
|
|
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true));
|
|
|
|
$this->assertRegExp('/foo/', $tester->getDisplay());
|
2016-09-07 09:32:40 +01:00
|
|
|
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDumpTwoMessagesAndClean()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo', 'bar' => 'bar')));
|
2016-09-07 09:32:40 +01:00
|
|
|
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true));
|
|
|
|
$this->assertRegExp('/foo/', $tester->getDisplay());
|
|
|
|
$this->assertRegExp('/bar/', $tester->getDisplay());
|
2015-12-10 19:14:16 +00:00
|
|
|
$this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
2016-07-09 17:21:29 +01:00
|
|
|
public function testDumpMessagesForSpecificDomain()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar')));
|
2016-07-09 17:21:29 +01:00
|
|
|
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--domain' => 'mydomain'));
|
|
|
|
$this->assertRegExp('/bar/', $tester->getDisplay());
|
2016-09-13 11:19:55 +01:00
|
|
|
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
|
2016-07-09 17:21:29 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 19:14:16 +00:00
|
|
|
public function testWriteMessages()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
|
2015-12-10 19:14:16 +00:00
|
|
|
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true));
|
|
|
|
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
|
2015-09-03 08:05:21 +01:00
|
|
|
}
|
|
|
|
|
2016-07-09 17:21:29 +01:00
|
|
|
public function testWriteMessagesForSpecificDomain()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar')));
|
2016-07-09 17:21:29 +01:00
|
|
|
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true, '--domain' => 'mydomain'));
|
|
|
|
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
2015-09-03 08:05:21 +01:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->fs = new Filesystem();
|
|
|
|
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
|
|
|
|
$this->fs->mkdir($this->translationDir.'/Resources/translations');
|
|
|
|
$this->fs->mkdir($this->translationDir.'/Resources/views');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
$this->fs->remove($this->translationDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return CommandTester
|
|
|
|
*/
|
2017-07-22 10:58:19 +01:00
|
|
|
private function createCommandTester($extractedMessages = array(), $loadedMessages = array(), HttpKernel\KernelInterface $kernel = null)
|
2015-09-03 08:05:21 +01:00
|
|
|
{
|
|
|
|
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$translator
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getFallbackLocales')
|
|
|
|
->will($this->returnValue(array('en')));
|
|
|
|
|
2016-12-19 09:02:29 +00:00
|
|
|
$extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
|
2015-09-03 08:05:21 +01:00
|
|
|
$extractor
|
|
|
|
->expects($this->any())
|
|
|
|
->method('extract')
|
|
|
|
->will(
|
|
|
|
$this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
|
2016-07-09 17:21:29 +01:00
|
|
|
foreach ($extractedMessages as $domain => $messages) {
|
|
|
|
$catalogue->add($messages, $domain);
|
|
|
|
}
|
2015-09-03 08:05:21 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-07-25 19:07:39 +01:00
|
|
|
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
|
2015-09-03 08:05:21 +01:00
|
|
|
$loader
|
|
|
|
->expects($this->any())
|
2017-07-25 19:07:39 +01:00
|
|
|
->method('read')
|
2015-09-03 08:05:21 +01:00
|
|
|
->will(
|
|
|
|
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
|
2016-10-30 09:34:06 +00:00
|
|
|
$catalogue->add($loadedMessages);
|
2015-09-03 08:05:21 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-12-19 09:02:29 +00:00
|
|
|
$writer = $this->getMockBuilder('Symfony\Component\Translation\Writer\TranslationWriter')->getMock();
|
2015-09-03 08:05:21 +01:00
|
|
|
$writer
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getFormats')
|
|
|
|
->will(
|
|
|
|
$this->returnValue(array('xlf', 'yml'))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (null === $kernel) {
|
2016-12-19 09:02:29 +00:00
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
2015-09-03 08:05:21 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getBundle')
|
|
|
|
->will($this->returnValueMap(array(
|
|
|
|
array('foo', true, $this->getBundle($this->translationDir)),
|
|
|
|
array('test', true, $this->getBundle('test')),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getRootDir')
|
|
|
|
->will($this->returnValue($this->translationDir));
|
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getBundles')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getContainer')
|
|
|
|
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock()));
|
|
|
|
|
|
|
|
$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en');
|
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->add($command);
|
|
|
|
|
|
|
|
return new CommandTester($application->find('translation:update'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group legacy
|
|
|
|
* @expectedDeprecation Passing a command name as the first argument of "Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand::__construct" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.
|
|
|
|
*/
|
|
|
|
public function testLegacyUpdateCommand()
|
|
|
|
{
|
|
|
|
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
|
|
|
|
$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
|
|
|
|
$writer = $this->getMockBuilder('Symfony\Component\Translation\Writer\TranslationWriter')->getMock();
|
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getBundles')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
2016-12-19 09:02:29 +00:00
|
|
|
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
|
2015-09-03 08:05:21 +01:00
|
|
|
$container
|
|
|
|
->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap(array(
|
|
|
|
array('translation.extractor', 1, $extractor),
|
2017-07-25 19:07:39 +01:00
|
|
|
array('translation.reader', 1, $loader),
|
2015-09-03 08:05:21 +01:00
|
|
|
array('translation.writer', 1, $writer),
|
|
|
|
array('translator', 1, $translator),
|
|
|
|
array('kernel', 1, $kernel),
|
|
|
|
)));
|
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getContainer')
|
|
|
|
->will($this->returnValue($container));
|
|
|
|
|
|
|
|
$command = new TranslationUpdateCommand();
|
|
|
|
$command->setContainer($container);
|
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->add($command);
|
|
|
|
|
|
|
|
$tester = new CommandTester($application->find('translation:update'));
|
|
|
|
$tester->execute(array('locale' => 'en'));
|
|
|
|
|
|
|
|
$this->assertContains('You must choose one of --force or --dump-messages', $tester->getDisplay());
|
2015-09-03 08:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getBundle($path)
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
|
2015-09-03 08:05:21 +01:00
|
|
|
$bundle
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue($path))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $bundle;
|
|
|
|
}
|
|
|
|
}
|