smarber
smarber

Reputation: 5074

Testing symfony commands not working

I'd like to test a symfony command using CommandTester as explained here http://symfony.com/doc/2.3/components/console/introduction.html#testing-commands.

The command I want to test removes records having the column decision set to true. (More detail in the code below)

public function testMyCommand()
{
    // .....
    // Updating the database
    $res = $this->client->getContainer()->get('doctrine.orm.entity_manager')->createQueryBuilder()
        ->update('MyBundle:MyEntity', 'me')
        ->set('me.decision', true)
        ->where('me.id = :id')
        ->setParameter('id', 4);

    // The command is meant to seek for all entities having *decision* set to true and delete them
    $application = new Application($this->client->getKernel());
    $application->add(new CronQuotidienCommand());

    $command = $application->find('myproject:mycmd');
    $commandTester = new CommandTester($command);
    $commandTester->execute(array('command' => $command->getName()));

    $this->assertEmpty($this->client->getContainer()->get('doctrine')->getRepository('MyBundle:MyEntity')->findBy(array('decision' => true));
}

Although the test fails, there is one MyEntity, whose id equals to 4, with the column decision set to true, which means that $commandTester->execute(array('command' => $command->getName())); didn't find any MyEntity with decision set to true (referesh matter?).

The surprise is when I replace

    $application = new Application($this->client->getKernel());
    $application->add(new CronQuotidienCommand());

    $command = $application->find('myproject:mycmd');
    $commandTester = new CommandTester($command);
    $commandTester->execute(array('command' => $command->getName()));

with

    shell_exec('php ' . $this->container->get('kernel')->getRootDir() . '/console myproject:mycmd');

it does work.

Any ideas? thx

Upvotes: 2

Views: 1526

Answers (1)

Gnucki
Gnucki

Reputation: 5133

Did you try:

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;

$arguments = array(
    // ...
);
$input = new ArrayInput($arguments);
$output = new ConsoleOutput();
$returnCode = $command->run($input, $output);

instead of:

$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));

Upvotes: 1

Related Questions