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

Pass console command option through to Symfony CommandTester in PHPUnit test

我有一个 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: The "filepath" argument does not exist.

我的问题是 - 如何通过 Option 传递给 CommandTester?传递参数没问题,但我找不到关于如何为选项执行此操作的文档。

你应该通过 -- 传递选项,所以试试这个:

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

而不是这个:

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

希望对您有所帮助