将控制台命令选项传递给PHPUnit测试中的Symfony CommandTester

时间:2016-10-20 07:58:11

标签: php unit-testing symfony phpunit php-7

我有一个Symfony控制台命令配置如下:

protected function configure()
{
    $this
       ->setName('crmpiccobundle:list:sync')
       ->setDescription('Sync users to the list')
       ->addOption(
       'filepath',
       null,
       InputOption::VALUE_OPTIONAL,
       'The path to the file',
       null
    )
    ;
}

...我有一个PHPUnit测试,看起来像这样:

public function testExecuteWhenFileIsEmpty()
{
    self::bootKernel();

    $application = new Application(self::$kernel);

    $application->add(new Sync());

    $command = $application->find('crmpiccobundle:list:sync');

    $commandTester = new CommandTester($command);
    $commandTester->execute([
        'command' => $command->getName(),
        'filepath' => '/tmp/crmpicco.co.uk.csv'
    ]);
}

当我运行它时,我收到以下错误:

  

InvalidArgumentException:“filepath”参数不存在。

我的问题是 - 如何将选项传递给CommandTester?传递参数很好,但我找不到关于如何为选项做的文档。

1 个答案:

答案 0 :(得分:7)

您应该使用--传递选项,请尝试以下操作:

$commandTester->execute([
    'command' => $command->getName(),
    '--filepath' => '/tmp/crmpicco.co.uk.csv'
]);

而不是:

$commandTester->execute([
    'command' => $command->getName(),
    'filepath' => '/tmp/crmpicco.co.uk.csv'
]);

希望这个帮助