N°2651 rollback gitignore for lib tests dirs

Too dangerous ! We'll work properly on this but for 2.8
This commit is contained in:
Pierre Goiffon
2020-01-10 15:15:15 +01:00
parent 881fc2a1de
commit ad821e7d9c
2086 changed files with 151849 additions and 6 deletions

View File

@@ -0,0 +1,92 @@
<?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\CacheClearCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture\TestAppKernel;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Config\ConfigCacheFactory;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class CacheClearCommandTest extends TestCase
{
/** @var TestAppKernel */
private $kernel;
/** @var Filesystem */
private $fs;
private $rootDir;
protected function setUp()
{
$this->fs = new Filesystem();
$this->kernel = new TestAppKernel('test', true);
$this->rootDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('sf2_cache_', true);
$this->kernel->setRootDir($this->rootDir);
$this->fs->mkdir($this->rootDir);
}
protected function tearDown()
{
$this->fs->remove($this->rootDir);
}
public function testCacheIsFreshAfterCacheClearedWithWarmup()
{
$input = new ArrayInput(['cache:clear']);
$application = new Application($this->kernel);
$application->setCatchExceptions(false);
$application->doRun($input, new NullOutput());
// Ensure that all *.meta files are fresh
$finder = new Finder();
$metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta');
// check that cache is warmed up
$this->assertNotEmpty($metaFiles);
$configCacheFactory = new ConfigCacheFactory(true);
foreach ($metaFiles as $file) {
$configCacheFactory->cache(substr($file, 0, -5), function () use ($file) {
$this->fail(sprintf('Meta file "%s" is not fresh', (string) $file));
});
}
// check that app kernel file present in meta file of container's cache
$containerClass = $this->kernel->getContainer()->getParameter('kernel.container_class');
$containerRef = new \ReflectionClass($containerClass);
$containerFile = \dirname(\dirname($containerRef->getFileName())).'/'.$containerClass.'.php';
$containerMetaFile = $containerFile.'.meta';
$kernelRef = new \ReflectionObject($this->kernel);
$kernelFile = $kernelRef->getFileName();
/** @var ResourceInterface[] $meta */
$meta = unserialize(file_get_contents($containerMetaFile));
$found = false;
foreach ($meta as $resource) {
if ((string) $resource === $kernelFile) {
$found = true;
break;
}
}
$this->assertTrue($found, 'Kernel file should present as resource');
if (\defined('HHVM_VERSION')) {
return;
}
$containerRef = new \ReflectionClass(require $containerFile);
$containerFile = str_replace('tes_'.\DIRECTORY_SEPARATOR, 'test'.\DIRECTORY_SEPARATOR, $containerRef->getFileName());
$this->assertRegExp(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container');
}
}

View File

@@ -0,0 +1,43 @@
<?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\CacheClearCommand\Fixture;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
class TestAppKernel extends Kernel
{
public function registerBundles()
{
return [
new FrameworkBundle(),
];
}
public function setRootDir($rootDir)
{
$this->rootDir = $rootDir;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.\DIRECTORY_SEPARATOR.'config.yml');
}
protected function build(ContainerBuilder $container)
{
$container->register('logger', NullLogger::class);
}
}

View File

@@ -0,0 +1,2 @@
framework:
secret: test

View File

@@ -0,0 +1,110 @@
<?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;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Command\CachePoolPruneCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\HttpKernel\KernelInterface;
class CachePruneCommandTest extends TestCase
{
public function testCommandWithPools()
{
$tester = $this->getCommandTester($this->getKernel(), $this->getRewindableGenerator());
$tester->execute([]);
}
public function testCommandWithNoPools()
{
$tester = $this->getCommandTester($this->getKernel(), $this->getEmptyRewindableGenerator());
$tester->execute([]);
}
/**
* @return RewindableGenerator
*/
private function getRewindableGenerator()
{
return new RewindableGenerator(function () {
yield 'foo_pool' => $this->getPruneableInterfaceMock();
yield 'bar_pool' => $this->getPruneableInterfaceMock();
}, 2);
}
/**
* @return RewindableGenerator
*/
private function getEmptyRewindableGenerator()
{
return new RewindableGenerator(function () {
return new \ArrayIterator([]);
}, 0);
}
/**
* @return MockObject|KernelInterface
*/
private function getKernel()
{
$container = $this
->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')
->getMock();
$kernel = $this
->getMockBuilder(KernelInterface::class)
->getMock();
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);
$kernel
->expects($this->once())
->method('getBundles')
->willReturn([]);
return $kernel;
}
/**
* @return MockObject|PruneableInterface
*/
private function getPruneableInterfaceMock()
{
$pruneable = $this
->getMockBuilder(PruneableInterface::class)
->getMock();
$pruneable
->expects($this->atLeastOnce())
->method('prune');
return $pruneable;
}
/**
* @return CommandTester
*/
private function getCommandTester(KernelInterface $kernel, RewindableGenerator $generator)
{
$application = new Application($kernel);
$application->add(new CachePoolPruneCommand($generator));
return new CommandTester($application->find('cache:pool:prune'));
}
}

View File

@@ -0,0 +1,123 @@
<?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;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouterDebugCommandTest extends TestCase
{
public function testDebugAllRoutes()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['name' => null], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertStringContainsString('Name Method Scheme Host Path', $tester->getDisplay());
}
public function testDebugSingleRoute()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['name' => 'foo'], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertStringContainsString('Route Name | foo', $tester->getDisplay());
}
public function testDebugInvalidRoute()
{
$this->expectException('InvalidArgumentException');
$this->createCommandTester()->execute(['name' => 'test']);
}
/**
* @group legacy
* @expectedDeprecation Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand::__construct() expects an instance of "Symfony\Component\Routing\RouterInterface" as first argument since Symfony 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.
*/
public function testLegacyDebugCommand()
{
$application = new Application($this->getKernel());
$application->add(new RouterDebugCommand());
$tester = new CommandTester($application->find('debug:router'));
$tester->execute([]);
$this->assertRegExp('/foo\s+ANY\s+ANY\s+ANY\s+\\/foo/', $tester->getDisplay());
}
/**
* @return CommandTester
*/
private function createCommandTester()
{
$application = new Application($this->getKernel());
$application->add(new RouterDebugCommand($this->getRouter()));
return new CommandTester($application->find('debug:router'));
}
private function getRouter()
{
$routeCollection = new RouteCollection();
$routeCollection->add('foo', new Route('foo'));
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
$router
->expects($this->any())
->method('getRouteCollection')
->willReturn($routeCollection);
return $router;
}
private function getKernel()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container
->expects($this->atLeastOnce())
->method('has')
->willReturnCallback(function ($id) {
if ('console.command_loader' === $id) {
return false;
}
return true;
})
;
$container
->expects($this->any())
->method('get')
->with('router')
->willReturn($this->getRouter())
;
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container)
;
$kernel
->expects($this->once())
->method('getBundles')
->willReturn([])
;
return $kernel;
}
}

View File

@@ -0,0 +1,123 @@
<?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;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouterMatchCommandTest extends TestCase
{
public function testWithMatchPath()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['path_info' => '/foo', 'foo'], ['decorated' => false]);
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertStringContainsString('Route Name | foo', $tester->getDisplay());
}
public function testWithNotMatchPath()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['path_info' => '/test', 'foo'], ['decorated' => false]);
$this->assertEquals(1, $ret, 'Returns 1 in case of failure');
$this->assertStringContainsString('None of the routes match the path "/test"', $tester->getDisplay());
}
/**
* @group legacy
* @expectedDeprecation Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand::__construct() expects an instance of "Symfony\Component\Routing\RouterInterface" as first argument since Symfony 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.
* @expectedDeprecation Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand::__construct() expects an instance of "Symfony\Component\Routing\RouterInterface" as first argument since Symfony 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.
*/
public function testLegacyMatchCommand()
{
$application = new Application($this->getKernel());
$application->add(new RouterMatchCommand());
$application->add(new RouterDebugCommand());
$tester = new CommandTester($application->find('router:match'));
$tester->execute(['path_info' => '/']);
$this->assertStringContainsString('None of the routes match the path "/"', $tester->getDisplay());
}
/**
* @return CommandTester
*/
private function createCommandTester()
{
$application = new Application($this->getKernel());
$application->add(new RouterMatchCommand($this->getRouter()));
$application->add(new RouterDebugCommand($this->getRouter()));
return new CommandTester($application->find('router:match'));
}
private function getRouter()
{
$routeCollection = new RouteCollection();
$routeCollection->add('foo', new Route('foo'));
$requestContext = new RequestContext();
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock();
$router
->expects($this->any())
->method('getRouteCollection')
->willReturn($routeCollection);
$router
->expects($this->any())
->method('getContext')
->willReturn($requestContext);
return $router;
}
private function getKernel()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container
->expects($this->atLeastOnce())
->method('has')
->willReturnCallback(function ($id) {
return 'console.command_loader' !== $id;
})
;
$container
->expects($this->any())
->method('get')
->with('router')
->willReturn($this->getRouter())
;
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container)
;
$kernel
->expects($this->once())
->method('getBundles')
->willReturn([])
;
return $kernel;
}
}

View File

@@ -0,0 +1,256 @@
<?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;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel;
class TranslationDebugCommandTest extends TestCase
{
private $fs;
private $translationDir;
public function testDebugMissingMessages()
{
$tester = $this->createCommandTester(['foo' => 'foo']);
$tester->execute(['locale' => 'en', 'bundle' => 'foo']);
$this->assertRegExp('/missing/', $tester->getDisplay());
}
public function testDebugUnusedMessages()
{
$tester = $this->createCommandTester([], ['foo' => 'foo']);
$tester->execute(['locale' => 'en', 'bundle' => 'foo']);
$this->assertRegExp('/unused/', $tester->getDisplay());
}
public function testDebugFallbackMessages()
{
$tester = $this->createCommandTester([], ['foo' => 'foo']);
$tester->execute(['locale' => 'fr', 'bundle' => 'foo']);
$this->assertRegExp('/fallback/', $tester->getDisplay());
}
public function testNoDefinedMessages()
{
$tester = $this->createCommandTester();
$tester->execute(['locale' => 'fr', 'bundle' => 'test']);
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
}
public function testDebugDefaultDirectory()
{
$tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar']);
$tester->execute(['locale' => 'en']);
$this->assertRegExp('/missing/', $tester->getDisplay());
$this->assertRegExp('/unused/', $tester->getDisplay());
}
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(['foo' => 'foo'], ['bar' => 'bar']);
$tester->execute(['locale' => 'en']);
$this->assertRegExp('/missing/', $tester->getDisplay());
$this->assertRegExp('/unused/', $tester->getDisplay());
}
public function testDebugCustomDirectory()
{
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel->expects($this->once())
->method('getBundle')
->with($this->equalTo($this->translationDir))
->willThrowException(new \InvalidArgumentException());
$tester = $this->createCommandTester(['foo' => 'foo'], ['bar' => 'bar'], $kernel);
$tester->execute(['locale' => 'en', 'bundle' => $this->translationDir]);
$this->assertRegExp('/missing/', $tester->getDisplay());
$this->assertRegExp('/unused/', $tester->getDisplay());
}
public function testDebugInvalidDirectory()
{
$this->expectException('InvalidArgumentException');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel->expects($this->once())
->method('getBundle')
->with($this->equalTo('dir'))
->willThrowException(new \InvalidArgumentException());
$tester = $this->createCommandTester([], [], $kernel);
$tester->execute(['locale' => 'en', 'bundle' => 'dir']);
}
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');
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');
}
protected function tearDown()
{
$this->fs->remove($this->translationDir);
}
/**
* @return CommandTester
*/
private function createCommandTester($extractedMessages = [], $loadedMessages = [], $kernel = null)
{
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$translator
->expects($this->any())
->method('getFallbackLocales')
->willReturn(['en']);
$extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
$extractor
->expects($this->any())
->method('extract')
->willReturnCallback(
function ($path, $catalogue) use ($extractedMessages) {
$catalogue->add($extractedMessages);
}
);
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
$loader
->expects($this->any())
->method('read')
->willReturnCallback(
function ($path, $catalogue) use ($loadedMessages) {
$catalogue->add($loadedMessages);
}
);
if (null === $kernel) {
$returnValues = [
['foo', $this->getBundle($this->translationDir)],
['test', $this->getBundle('test')],
];
if (HttpKernel\Kernel::VERSION_ID < 40000) {
$returnValues = [
['foo', true, $this->getBundle($this->translationDir)],
['test', true, $this->getBundle('test')],
];
}
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel
->expects($this->any())
->method('getBundle')
->willReturnMap($returnValues);
}
$kernel
->expects($this->any())
->method('getRootDir')
->willReturn($this->translationDir);
$kernel
->expects($this->any())
->method('getBundles')
->willReturn([]);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock());
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates');
$application = new Application($kernel);
$application->add($command);
return new CommandTester($application->find('debug:translation'));
}
/**
* @group legacy
* @expectedDeprecation Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand::__construct() expects an instance of "Symfony\Component\Translation\TranslatorInterface" as first argument since Symfony 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.
*/
public function testLegacyDebugCommand()
{
$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();
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel
->expects($this->any())
->method('getBundles')
->willReturn([]);
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container
->expects($this->any())
->method('get')
->willReturnMap([
['translation.extractor', 1, $extractor],
['translation.reader', 1, $loader],
['translator', 1, $translator],
['kernel', 1, $kernel],
]);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);
$command = new TranslationDebugCommand();
$command->setContainer($container);
$application = new Application($kernel);
$application->add($command);
$tester = new CommandTester($application->find('debug:translation'));
$tester->execute(['locale' => 'en']);
$this->assertStringContainsString('No defined or extracted', $tester->getDisplay());
}
private function getBundle($path)
{
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
$bundle
->expects($this->any())
->method('getPath')
->willReturn($path)
;
return $bundle;
}
}

View File

@@ -0,0 +1,248 @@
<?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;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel;
class TranslationUpdateCommandTest extends TestCase
{
private $fs;
private $translationDir;
public function testDumpMessagesAndClean()
{
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true]);
$this->assertRegExp('/foo/', $tester->getDisplay());
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
}
public function testDumpMessagesAndCleanInRootDirectory()
{
$this->fs->remove($this->translationDir);
$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(['messages' => ['foo' => 'foo']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', '--dump-messages' => true, '--clean' => true]);
$this->assertRegExp('/foo/', $tester->getDisplay());
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
}
public function testDumpTwoMessagesAndClean()
{
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'bar' => 'bar']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true]);
$this->assertRegExp('/foo/', $tester->getDisplay());
$this->assertRegExp('/bar/', $tester->getDisplay());
$this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay());
}
public function testDumpMessagesForSpecificDomain()
{
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo'], 'mydomain' => ['bar' => 'bar']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--domain' => 'mydomain']);
$this->assertRegExp('/bar/', $tester->getDisplay());
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
}
public function testWriteMessages()
{
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true]);
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
}
public function testWriteMessagesInRootDirectory()
{
$this->fs->remove($this->translationDir);
$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(['messages' => ['foo' => 'foo']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', '--force' => true]);
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
}
public function testWriteMessagesForSpecificDomain()
{
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo'], 'mydomain' => ['bar' => 'bar']]);
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true, '--domain' => 'mydomain']);
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
}
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');
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');
}
protected function tearDown()
{
$this->fs->remove($this->translationDir);
}
/**
* @return CommandTester
*/
private function createCommandTester($extractedMessages = [], $loadedMessages = [], HttpKernel\KernelInterface $kernel = null)
{
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
->disableOriginalConstructor()
->getMock();
$translator
->expects($this->any())
->method('getFallbackLocales')
->willReturn(['en']);
$extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')->getMock();
$extractor
->expects($this->any())
->method('extract')
->willReturnCallback(
function ($path, $catalogue) use ($extractedMessages) {
foreach ($extractedMessages as $domain => $messages) {
$catalogue->add($messages, $domain);
}
}
);
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
$loader
->expects($this->any())
->method('read')
->willReturnCallback(
function ($path, $catalogue) use ($loadedMessages) {
$catalogue->add($loadedMessages);
}
);
$writer = $this->getMockBuilder('Symfony\Component\Translation\Writer\TranslationWriter')->getMock();
$writer
->expects($this->any())
->method('getFormats')
->willReturn(
['xlf', 'yml', 'yaml']
);
if (null === $kernel) {
$returnValues = [
['foo', $this->getBundle($this->translationDir)],
['test', $this->getBundle('test')],
];
if (HttpKernel\Kernel::VERSION_ID < 40000) {
$returnValues = [
['foo', true, $this->getBundle($this->translationDir)],
['test', true, $this->getBundle('test')],
];
}
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel
->expects($this->any())
->method('getBundle')
->willReturnMap($returnValues);
}
$kernel
->expects($this->any())
->method('getRootDir')
->willReturn($this->translationDir);
$kernel
->expects($this->any())
->method('getBundles')
->willReturn([]);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock());
$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates');
$application = new Application($kernel);
$application->add($command);
return new CommandTester($application->find('translation:update'));
}
/**
* @group legacy
* @expectedDeprecation Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand::__construct() expects an instance of "Symfony\Component\Translation\Writer\TranslationWriterInterface" as first argument since Symfony 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.
*/
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')
->willReturn([]);
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container
->expects($this->any())
->method('get')
->willReturnMap([
['translation.extractor', 1, $extractor],
['translation.reader', 1, $loader],
['translation.writer', 1, $writer],
['translator', 1, $translator],
['kernel', 1, $kernel],
]);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);
$command = new TranslationUpdateCommand();
$command->setContainer($container);
$application = new Application($kernel);
$application->add($command);
$tester = new CommandTester($application->find('translation:update'));
$tester->execute(['locale' => 'en']);
$this->assertStringContainsString('You must choose one of --force or --dump-messages', $tester->getDisplay());
}
private function getBundle($path)
{
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
$bundle
->expects($this->any())
->method('getPath')
->willReturn($path)
;
return $bundle;
}
}

View File

@@ -0,0 +1,182 @@
<?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;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\YamlLintCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Tests the YamlLintCommand.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class YamlLintCommandTest extends TestCase
{
private $files;
public function testLintCorrectFile()
{
$tester = $this->createCommandTester();
$filename = $this->createFile('foo: bar');
$tester->execute(
['filename' => $filename],
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
$this->assertStringContainsString('OK', trim($tester->getDisplay()));
}
public function testLintIncorrectFile()
{
$incorrectContent = '
foo:
bar';
$tester = $this->createCommandTester();
$filename = $this->createFile($incorrectContent);
$tester->execute(['filename' => $filename], ['decorated' => false]);
$this->assertEquals(1, $tester->getStatusCode(), 'Returns 1 in case of error');
$this->assertStringContainsString('Unable to parse at line 3 (near "bar").', trim($tester->getDisplay()));
}
public function testLintFileNotReadable()
{
$this->expectException('RuntimeException');
$tester = $this->createCommandTester();
$filename = $this->createFile('');
unlink($filename);
$tester->execute(['filename' => $filename], ['decorated' => false]);
}
public function testGetHelp()
{
$command = new YamlLintCommand();
$expected = <<<EOF
Or find all files in a bundle:
<info>php %command.full_name% @AcmeDemoBundle</info>
EOF;
$this->assertStringContainsString($expected, $command->getHelp());
}
public function testLintFilesFromBundleDirectory()
{
$tester = $this->createCommandTester($this->getKernelAwareApplicationMock());
$tester->execute(
['filename' => '@AppBundle/Resources'],
['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
);
$this->assertEquals(0, $tester->getStatusCode(), 'Returns 0 in case of success');
$this->assertStringContainsString('[OK] All 0 YAML files contain valid syntax', trim($tester->getDisplay()));
}
/**
* @return string Path to the new file
*/
private function createFile($content)
{
$filename = tempnam(sys_get_temp_dir().'/yml-lint-test', 'sf-');
file_put_contents($filename, $content);
$this->files[] = $filename;
return $filename;
}
/**
* @return CommandTester
*/
private function createCommandTester($application = null)
{
if (!$application) {
$application = new BaseApplication();
$application->add(new YamlLintCommand());
}
$command = $application->find('lint:yaml');
if ($application) {
$command->setApplication($application);
}
return new CommandTester($command);
}
private function getKernelAwareApplicationMock()
{
$kernel = $this->getMockBuilder(KernelInterface::class)
->disableOriginalConstructor()
->getMock();
$kernel
->expects($this->once())
->method('locateResource')
->with('@AppBundle/Resources')
->willReturn(sys_get_temp_dir().'/yml-lint-test');
$application = $this->getMockBuilder(Application::class)
->disableOriginalConstructor()
->getMock();
$application
->expects($this->once())
->method('getKernel')
->willReturn($kernel);
$application
->expects($this->once())
->method('getHelperSet')
->willReturn(new HelperSet());
$application
->expects($this->any())
->method('getDefinition')
->willReturn(new InputDefinition());
$application
->expects($this->once())
->method('find')
->with('lint:yaml')
->willReturn(new YamlLintCommand());
return $application;
}
protected function setUp()
{
@mkdir(sys_get_temp_dir().'/yml-lint-test');
$this->files = [];
}
protected function tearDown()
{
foreach ($this->files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
rmdir(sys_get_temp_dir().'/yml-lint-test');
}
}