Testando Comandos do Container Aware no Symfony2

Se o seu comando for estendido SymfonyBundleFrameworkBundleCommandContainerAwareCommand, você também precisará usar SymfonyBundleFrameworkBundleConsoleApplicationpara testar o comando em vez da base SymfonyComponentConsoleApplication.

Sua classe de teste também deve estender SymfonyBundleFrameworkBundleTestWebTestCasee inicializar um kernel para obter um contexto utilizável em seu comando.

Exemplo:

use SymfonyBundleFrameworkBundleTestWebTestCase;
use SymfonyBundleFrameworkBundleConsoleApplication;
use SymfonyComponentConsoleTesterCommandTester;
use VLIZToolboxBundleCommandDownloadCommand;

class DownloadCommandTest extends WebTestCase
{
/**
* @inheritDoc

*/

protected function setUp()
{
static::$kernel = static::createKernel();
static::$kernel->boot();
}

public function testDownload()
{
$application
= new Application(static::$kernel);
$application
->add(new DownloadCommand());

$command
= $application->find('demo:greet');
$command
->setApplication($application);
$commandTester
= new CommandTester($command);
$commandTester
->execute(
array
(
'command' => $command->getName(),
'name' => 'Fabien',
'--iterations' => 5,
);

$this
->assertRegExp('/Fabien/', $commandTester->getDisplay());
}
}