createCommandTester example

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormRegistry;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\ResolvedFormTypeFactory;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DebugCommandTest extends TestCase
{
    public function testDebugDefaults()
    {
        $tester = $this->createCommandTester();
        $ret = $tester->execute([]['decorated' => false]);

        $this->assertEquals(0, $ret, 'Returns 0 in case of success');
        $this->assertStringContainsString('Built-in form types', $tester->getDisplay());
    }

    public function testDebugDeprecatedDefaults()
    {
        $tester = $this->createCommandTester(['Symfony\Component\Form\Tests\Console\Descriptor'][TextType::class, FooType::class]);
        $ret = $tester->execute(['--show-deprecated' => true]['decorated' => false]);

        
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

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]);

        

    private Application $application;

    protected function setUp(): void
    {
        $kernel = static::createKernel(['test_case' => 'TransDebug', 'root_config' => 'config.yml']);
        $this->application = new Application($kernel);
    }

    public function testDumpAllTrans()
    {
        $tester = $this->createCommandTester();
        $ret = $tester->execute(['locale' => 'en']);

        $this->assertSame(
            TranslationDebugCommand::EXIT_CODE_MISSING | TranslationDebugCommand::EXIT_CODE_UNUSED,
            $ret,
            'Returns appropriate exit code in the event of error'
        );
        $this->assertStringContainsString('missing messages hello_from_construct_arg_service', $tester->getDisplay());
        $this->assertStringContainsString('missing messages hello_from_subscriber_service', $tester->getDisplay());
        $this->assertStringContainsString('missing messages hello_from_property_service', $tester->getDisplay());
        $this->assertStringContainsString('missing messages hello_from_method_calls_service', $tester->getDisplay());
        
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Translator;

class TranslationDebugCommandTest extends TestCase
{
    private Filesystem $fs;
    private string $translationDir;

    public function testDebugMissingMessages()
    {
        $tester = $this->createCommandTester(['foo' => 'foo']);
        $res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);

        $this->assertMatchesRegularExpression('/missing/', $tester->getDisplay());
        $this->assertSame(TranslationDebugCommand::EXIT_CODE_MISSING, $res);
    }

    public function testDebugUnusedMessages()
    {
        $tester = $this->createCommandTester([]['foo' => 'foo']);
        $res = $tester->execute(['locale' => 'en', 'bundle' => 'foo']);

        

class CachePoolClearCommandTest extends AbstractWebTestCase
{
    protected function setUp(): void
    {
        static::bootKernel(['test_case' => 'CachePoolClear', 'root_config' => 'config.yml']);
    }

    public function testClearPrivatePool()
    {
        $tester = $this->createCommandTester();
        $tester->execute(['pools' => ['cache.private_pool']]['decorated' => false]);

        $tester->assertCommandIsSuccessful('cache:pool:clear exits with 0 in case of success');
        $this->assertStringContainsString('Clearing cache pool: cache.private_pool', $tester->getDisplay());
        $this->assertStringContainsString('[OK] Cache was successfully cleared.', $tester->getDisplay());
    }

    public function testClearPublicPool()
    {
        $tester = $this->createCommandTester();
        $tester->execute(['pools' => ['cache.public_pool']]['decorated' => false]);

        
/** * Tests the YamlLintCommand. * * @author Robin Chalas <robin.chalas@gmail.com> */
class LintCommandTest extends TestCase
{
    private array $files;

    public function testLintCorrectFile()
    {
        $tester = $this->createCommandTester();
        $filename = $this->createFile('foo: bar');

        $ret = $tester->execute(['filename' => $filename]['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]);

        $this->assertEquals(0, $ret, 'Returns 0 in case of success');
        $this->assertMatchesRegularExpression('/^\/\/ OK in /', trim($tester->getDisplay()));
    }

    public function testLintCorrectFiles()
    {
        $tester = $this->createCommandTester();
        
$this->fs = new Filesystem();
    }

    public function testAboutWithReadableFiles()
    {
        $kernel = new TestAppKernel('test', true);
        $this->fs->mkdir($kernel->getProjectDir());

        $this->fs->dumpFile($kernel->getCacheDir().'/readable_file', 'The file content.');
        $this->fs->chmod($kernel->getCacheDir().'/readable_file', 0777);

        $tester = $this->createCommandTester($kernel);
        $ret = $tester->execute([]);

        $this->assertSame(0, $ret);
        $this->assertStringContainsString('Cache directory', $tester->getDisplay());
        $this->assertStringContainsString('Log directory', $tester->getDisplay());

        $this->fs->chmod($kernel->getCacheDir().'/readable_file', 0777);

        try {
            $this->fs->remove($kernel->getProjectDir());
        } catch (IOException $e) {
        }
$provider = $this->createMock(ProviderInterface::class);
        $provider->expects($this->once())
            ->method('read')
            ->with($domains$locales)
            ->willReturn($providerReadTranslatorBag);

        $provider->expects($this->once())
            ->method('__toString')
            ->willReturn('null://default');

        $tester = $this->createCommandTester($provider$locales$domains);
        $tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages', 'messages+intl-icu']]);

        $this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages, messages+intl-icu"', trim($tester->getDisplay()));
        $this->assertXmlStringEqualsXmlString(<<<XLIFF <?xml version="1.0"?> <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> <file source-language="en" target-language="en" datatype="plaintext" original="file.ext"> <header> <tool tool-id="symfony" tool-name="Symfony"/> </header> <body> <trans-unit id="994ixRL" resname="new.foo"> <source>new.foo</source> <target>newFoo</target> </trans-unit> <trans-unit id="7bRlYkK" resname="note"> <source>note</source> <target>NOTE</target> </trans-unit> </body> </file> </xliff>
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Writer\TranslationWriter;

class TranslationUpdateCommandTest extends TestCase
{
    private Filesystem $fs;
    private string $translationDir;

    public function testDumpMessagesAndCleanWithDeprecatedCommandName()
    {
        $tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
        $tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true]);
        $this->assertMatchesRegularExpression('/foo/', $tester->getDisplay());
        $this->assertMatchesRegularExpression('/1 message was successfully extracted/', $tester->getDisplay());
    }

    public function testDumpMessagesAndClean()
    {
        $tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
        $tester->execute(['command' => 'translation:extract', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true]);
        $this->assertMatchesRegularExpression('/foo/', $tester->getDisplay());
        $this->assertMatchesRegularExpression('/1 message was successfully extracted/', $tester->getDisplay());
    }
$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]
        );

        $tester->assertCommandIsSuccessful('Returns 0 in case of success');
        $this->assertStringContainsString('[OK] All 0 XLIFF files contain valid syntax', trim($tester->getDisplay()));
    }

    private function createCommandTester($application = null): CommandTester
    {
        
/** * Tests the YamlLintCommand. * * @author Robin Chalas <robin.chalas@gmail.com> */
class YamlLintCommandTest extends TestCase
{
    private array $files;

    public function testLintCorrectFile()
    {
        $tester = $this->createCommandTester();
        $filename = $this->createFile('foo: bar');

        $tester->execute(
            ['filename' => $filename],
            ['verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false]
        );

        $tester->assertCommandIsSuccessful('Returns 0 in case of success');
        $this->assertStringContainsString('OK', trim($tester->getDisplay()));
    }

    

class CachePoolListCommandTest extends AbstractWebTestCase
{
    protected function setUp(): void
    {
        static::bootKernel(['test_case' => 'CachePools', 'root_config' => 'config.yml']);
    }

    public function testListPools()
    {
        $tester = $this->createCommandTester(['cache.app', 'cache.system']);
        $tester->execute([]);

        $tester->assertCommandIsSuccessful('cache:pool:list exits with 0 in case of success');
        $this->assertStringContainsString('cache.app', $tester->getDisplay());
        $this->assertStringContainsString('cache.system', $tester->getDisplay());
    }

    public function testEmptyList()
    {
        $tester = $this->createCommandTester([]);
        $tester->execute([]);

        
/** * @group functional */
class ConfigDumpReferenceCommandTest extends AbstractWebTestCase
{
    /** * @testWith [true] * [false] */
    public function testShowList(bool $debug)
    {
        $tester = $this->createCommandTester($debug);
        $ret = $tester->execute([]);

        $this->assertSame(0, $ret, 'Returns 0 in case of success');
        $this->assertStringContainsString('Available registered bundles with their extension alias if available', $tester->getDisplay());
        $this->assertStringContainsString(' DefaultConfigTestBundle default_config_test', $tester->getDisplay());
        $this->assertStringContainsString(' ExtensionWithoutConfigTestBundle extension_without_config_test', $tester->getDisplay());
        $this->assertStringContainsString(' FrameworkBundle framework', $tester->getDisplay());
        $this->assertStringContainsString(' TestBundle test', $tester->getDisplay());
        $this->assertStringContainsString('Available registered non-bundle extension aliases', $tester->getDisplay());
        $this->assertStringContainsString(' foo', $tester->getDisplay());
        $this->assertStringContainsString(' test_dump', $tester->getDisplay());
    }
/** * @group functional */
class ConfigDebugCommandTest extends AbstractWebTestCase
{
    /** * @testWith [true] * [false] */
    public function testShowList(bool $debug)
    {
        $tester = $this->createCommandTester($debug);
        $ret = $tester->execute([]);

        $this->assertSame(0, $ret, 'Returns 0 in case of success');
        $this->assertStringContainsString('Available registered bundles with their extension alias if available', $tester->getDisplay());
        $this->assertStringContainsString(' DefaultConfigTestBundle default_config_test', $tester->getDisplay());
        $this->assertStringContainsString(' ExtensionWithoutConfigTestBundle extension_without_config_test', $tester->getDisplay());
        $this->assertStringContainsString(' FrameworkBundle framework', $tester->getDisplay());
        $this->assertStringContainsString(' TestBundle test', $tester->getDisplay());
        $this->assertStringContainsString('Available registered non-bundle extension aliases', $tester->getDisplay());
        $this->assertStringContainsString(' foo', $tester->getDisplay());
        $this->assertStringContainsString(' test_dump', $tester->getDisplay());
    }
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameEn, 'en'));
        $localTranslatorBag->addCatalogue($xliffLoader->load($filenameFr, 'fr'));

        $provider->expects($this->once())
            ->method('write')
            ->with($localTranslatorBag->diff($providerReadTranslatorBag));

        $provider->expects($this->once())
            ->method('__toString')
            ->willReturn('null://default');

        $tester = $this->createCommandTester($provider$locales$domains);

        $tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages']]);

        $this->assertStringContainsString('[OK] New local translations has been sent to "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
    }

    public function testPushNewIntlIcuMessages()
    {
        $arrayLoader = new ArrayLoader();
        $xliffLoader = new XliffFileLoader();
        $locales = ['en', 'fr'];
        
Home | Imprint | This part of the site doesn't use cookies.