我想使用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
(referresh matter?
惊喜是当我更换时
$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');
它确实有效。
有什么想法吗?
你试过吗:
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()));