2015-05-02 14:38:10 +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;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
|
2017-07-22 10:58:19 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
2015-05-02 14:38:10 +01:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2017-09-26 18:24:24 +01:00
|
|
|
use Symfony\Component\HttpKernel;
|
2015-05-02 14:38:10 +01:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class TranslationDebugCommandTest extends TestCase
|
2015-05-02 14:38:10 +01:00
|
|
|
{
|
|
|
|
private $fs;
|
|
|
|
private $translationDir;
|
|
|
|
|
|
|
|
public function testDebugMissingMessages()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('foo' => 'foo'));
|
2015-05-02 14:38:10 +01:00
|
|
|
$tester->execute(array('locale' => 'en', 'bundle' => 'foo'));
|
|
|
|
|
2015-05-11 18:19:17 +01:00
|
|
|
$this->assertRegExp('/missing/', $tester->getDisplay());
|
2015-05-02 14:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDebugUnusedMessages()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array(), array('foo' => 'foo'));
|
2015-05-02 14:38:10 +01:00
|
|
|
$tester->execute(array('locale' => 'en', 'bundle' => 'foo'));
|
|
|
|
|
2015-05-11 18:19:17 +01:00
|
|
|
$this->assertRegExp('/unused/', $tester->getDisplay());
|
2015-05-02 14:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDebugFallbackMessages()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array(), array('foo' => 'foo'));
|
2015-05-02 14:38:10 +01:00
|
|
|
$tester->execute(array('locale' => 'fr', 'bundle' => 'foo'));
|
|
|
|
|
2015-05-11 18:19:17 +01:00
|
|
|
$this->assertRegExp('/fallback/', $tester->getDisplay());
|
2015-05-02 14:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoDefinedMessages()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester();
|
2015-05-02 14:38:10 +01:00
|
|
|
$tester->execute(array('locale' => 'fr', 'bundle' => 'test'));
|
|
|
|
|
2015-05-11 18:19:17 +01:00
|
|
|
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDebugDefaultDirectory()
|
|
|
|
{
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'));
|
2015-05-11 18:19:17 +01:00
|
|
|
$tester->execute(array('locale' => 'en'));
|
|
|
|
|
|
|
|
$this->assertRegExp('/missing/', $tester->getDisplay());
|
|
|
|
$this->assertRegExp('/unused/', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
2017-11-21 06:48:50 +00:00
|
|
|
public function testDebugDefaultRootDirectory()
|
|
|
|
{
|
|
|
|
$this->fs->remove($this->translationDir);
|
|
|
|
$this->fs = new Filesystem();
|
|
|
|
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
|
|
|
|
$this->fs->mkdir($this->translationDir.'/translations');
|
|
|
|
$this->fs->mkdir($this->translationDir.'/templates');
|
|
|
|
|
|
|
|
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'));
|
|
|
|
$tester->execute(array('locale' => 'en'));
|
|
|
|
|
|
|
|
$this->assertRegExp('/missing/', $tester->getDisplay());
|
|
|
|
$this->assertRegExp('/unused/', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:19:17 +01:00
|
|
|
public function testDebugCustomDirectory()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
2015-05-11 18:19:17 +01:00
|
|
|
$kernel->expects($this->once())
|
|
|
|
->method('getBundle')
|
|
|
|
->with($this->equalTo($this->translationDir))
|
|
|
|
->willThrowException(new \InvalidArgumentException());
|
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'), $kernel);
|
2015-05-11 18:19:17 +01:00
|
|
|
$tester->execute(array('locale' => 'en', 'bundle' => $this->translationDir));
|
|
|
|
|
|
|
|
$this->assertRegExp('/missing/', $tester->getDisplay());
|
|
|
|
$this->assertRegExp('/unused/', $tester->getDisplay());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testDebugInvalidDirectory()
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
2015-05-11 18:19:17 +01:00
|
|
|
$kernel->expects($this->once())
|
|
|
|
->method('getBundle')
|
|
|
|
->with($this->equalTo('dir'))
|
|
|
|
->will($this->throwException(new \InvalidArgumentException()));
|
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$tester = $this->createCommandTester(array(), array(), $kernel);
|
2015-05-11 18:19:17 +01:00
|
|
|
$tester->execute(array('locale' => 'en', 'bundle' => 'dir'));
|
2015-05-02 14:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->fs = new Filesystem();
|
2015-07-07 20:01:23 +01:00
|
|
|
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
|
2015-05-02 14:38:10 +01:00
|
|
|
$this->fs->mkdir($this->translationDir.'/Resources/translations');
|
2015-05-11 18:19:17 +01:00
|
|
|
$this->fs->mkdir($this->translationDir.'/Resources/views');
|
2017-11-21 06:48:50 +00:00
|
|
|
$this->fs->mkdir($this->translationDir.'/translations');
|
|
|
|
$this->fs->mkdir($this->translationDir.'/templates');
|
2015-05-02 14:38:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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(), $kernel = null)
|
2015-05-02 14:38:10 +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-05-02 14:38:10 +01:00
|
|
|
$extractor
|
|
|
|
->expects($this->any())
|
|
|
|
->method('extract')
|
|
|
|
->will(
|
|
|
|
$this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
|
2016-10-30 09:34:06 +00:00
|
|
|
$catalogue->add($extractedMessages);
|
2015-05-02 14:38:10 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-07-25 19:07:39 +01:00
|
|
|
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
|
2015-05-02 14:38:10 +01:00
|
|
|
$loader
|
|
|
|
->expects($this->any())
|
2017-07-25 19:07:39 +01:00
|
|
|
->method('read')
|
2015-05-02 14:38:10 +01:00
|
|
|
->will(
|
|
|
|
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
|
2016-10-30 09:34:06 +00:00
|
|
|
$catalogue->add($loadedMessages);
|
2015-05-02 14:38:10 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2015-05-11 18:19:17 +01:00
|
|
|
if (null === $kernel) {
|
2017-09-26 18:24:24 +01:00
|
|
|
$returnValues = array(
|
|
|
|
array('foo', $this->getBundle($this->translationDir)),
|
|
|
|
array('test', $this->getBundle('test')),
|
|
|
|
);
|
|
|
|
if (HttpKernel\Kernel::VERSION_ID < 40000) {
|
|
|
|
$returnValues = array(
|
|
|
|
array('foo', true, $this->getBundle($this->translationDir)),
|
|
|
|
array('test', true, $this->getBundle('test')),
|
|
|
|
);
|
|
|
|
}
|
2016-12-19 09:02:29 +00:00
|
|
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
|
2015-05-11 18:19:17 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getBundle')
|
2017-09-26 18:24:24 +01:00
|
|
|
->will($this->returnValueMap($returnValues));
|
2015-05-11 18:19:17 +01:00
|
|
|
}
|
|
|
|
|
2015-05-02 14:38:10 +01:00
|
|
|
$kernel
|
|
|
|
->expects($this->any())
|
2015-05-11 18:19:17 +01:00
|
|
|
->method('getRootDir')
|
|
|
|
->will($this->returnValue($this->translationDir));
|
2015-05-02 14:38:10 +01:00
|
|
|
|
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()));
|
|
|
|
|
2017-11-21 06:48:50 +00:00
|
|
|
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates');
|
2017-07-22 10:58:19 +01:00
|
|
|
|
|
|
|
$application = new Application($kernel);
|
|
|
|
$application->add($command);
|
|
|
|
|
|
|
|
return new CommandTester($application->find('debug:translation'));
|
|
|
|
}
|
|
|
|
|
2015-05-02 14:38:10 +01:00
|
|
|
private function getBundle($path)
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
|
2015-05-02 14:38:10 +01:00
|
|
|
$bundle
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getPath')
|
|
|
|
->will($this->returnValue($path))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $bundle;
|
|
|
|
}
|
|
|
|
}
|