我想使用CommandTester
测试symfony命令,如http://symfony.com/doc/2.3/components/console/introduction.html#testing-commands所述。
我要测试的命令会删除列decision
设置为true的记录。 (以下代码中的更多细节)
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));
}
虽然测试失败,但有一个MyEntity
,其ID等于4,列decision
设置为true
,这意味着$commandTester->execute(array('command' => $command->getName()));
没有找到MyEntity
设置为decision
的任何true
(引用问题?)。
令人惊讶的是我更换时
$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()));
带
shell_exec('php ' . $this->container->get('kernel')->getRootDir() . '/console myproject:mycmd');
它确实有用。
有什么想法吗? THX
答案 0 :(得分:1)
你有没有尝试过:
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);
而不是:
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));