Repository: symfony/symfony
File: src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php (PHP)

Code before:
        $aliasPrefix = 'console.command.public_alias.';
        $this->assertTrue($container->hasAlias($aliasPrefix.'my-command1'));
        $this->assertTrue($container->hasAlias($aliasPrefix.'my-command2'));
    }

    public function testProcessMultiCommandSameClass()
    {
        $container = new ContainerBuilder();

        $definition = new Definition(MethodBasedTestCommand::class);
        $definition->addTag('console.command');
        $definition->addTag('console.command', ['method' => 'cmd1']);
        $definition->addTag('console.command', ['method' => 'cmd2']);

        $container->setDefinition(MethodBasedTestCommand::class, $definition);

        new AddConsoleCommandPass()->process($container);
        $container->compile();

        /** @var ContainerCommandLoader $loader */
        $loader = $container->get('console.command_loader');

        $this->assertSame(['app:cmd0', 'app:cmd1', 'app:cmd2'], $loader->getNames());
        $this->assertSame(Command::SUCCESS, new CommandTester($loader->get('app:cmd0'))->execute([]));
        $this->assertSame(Command::SUCCESS, new CommandTester($loader->get('app:cmd1'))->execute([]));
        $this->assertSame(Command::SUCCESS, new CommandTester($loader->get('app:cmd2'))->execute([]));
    }

    public function testProcessOnChildDefinitionWithClass()
    {
        $container = new ContainerBuilder();
        $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
        $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';

        $parentId = 'my-parent-command';
        $childId = 'my-child-command';

        $parentDefinition = new Definition(/* no class */);
        $parentDefinition->setAbstract(true);

        $childDefinition = new ChildDefinition($parentId);
        $childDefinition->addTag('console.command')->setPublic(true);
        $childDefinition->setClass($className);

        $container->setDefinition($parentId, $parentDefinition);
        $container->setDefinition($childId, $childDefinition);

        $container->compile();
        $command = $container->get($childId);

        $this->assertInstanceOf($className, $command);

Review comment (suggestion):
testing that you can execute it with success is not enough to validate that you ran the *right* logic for them (which is not the case right now). You should also assert the output (as those fixture command conveniently output their name)

Code after:
        $aliasPrefix = 'console.command.public_alias.';
        $this->assertTrue($container->hasAlias($aliasPrefix.'my-command1'));
        $this->assertTrue($container->hasAlias($aliasPrefix.'my-command2'));
    }

    public function testProcessMultiCommandSameClass()
    {
        $container = new ContainerBuilder();

        $definition = new Definition(MethodBasedTestCommand::class);
        $definition->addTag('console.command');
        $definition->addTag('console.command', ['method' => 'cmd1']);
        $definition->addTag('console.command', ['method' => 'cmd2']);

        $container->setDefinition(MethodBasedTestCommand::class, $definition);

        new AddConsoleCommandPass()->process($container);
        $container->compile();

        /** @var ContainerCommandLoader $loader */
        $loader = $container->get('console.command_loader');

        $this->assertSame(['app:cmd0', 'app:cmd1', 'app:cmd2'], $loader->getNames());

        $commandTester = new CommandTester($loader->get('app:cmd0'));
        $this->assertSame(Command::SUCCESS, $commandTester->execute([]));
        $this->assertSame('cmd0', $commandTester->getDisplay());

        $commandTester = new CommandTester($loader->get('app:cmd1'));
        $this->assertSame(Command::SUCCESS, $commandTester->execute([]));
        $this->assertSame('cmd1', $commandTester->getDisplay());

        $commandTester = new CommandTester($loader->get('app:cmd2'));
        $this->assertSame(Command::SUCCESS, $commandTester->execute([]));
        $this->assertSame('cmd2', $commandTester->getDisplay());
    }

    public function testProcessOnChildDefinitionWithClass()
    {
        $container = new ContainerBuilder();
        $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
        $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';

        $parentId = 'my-parent-command';
        $childId = 'my-child-command';

        $parentDefinition = new Definition(/* no class */);
        $parentDefinition->setAbstract(true);

        $childDefinition = new ChildDefinition($parentId);
        $childDefinition->addTag('console.command')->setPublic(true);