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

crm*_*cco 2 php phpunit unit-testing symfony 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
    )
    ;
}
Run Code Online (Sandbox Code Playgroud)

...我有一个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'
    ]);
}
Run Code Online (Sandbox Code Playgroud)

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

InvalidArgumentException:"filepath"参数不存在.

我的问题是-我怎么通过一个选项CommandTester?传递参数很好,但是我找不到关于如何为Options做的文档.

Mat*_*teo 7

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

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

而不是这个:

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

希望这有帮助