crmpicco crmpicco - 1 month ago 6
PHP Question

Pass console command option through to Symfony CommandTester in PHPUnit test

I have a Symfony console command configured as below:

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
)
;
}


...and I have a PHPUnit test for it which looks like this:

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'
]);
}


When I run it I get the following error:


InvalidArgumentException: The "filepath" argument does not exist.


My question is - how do I pass through an Option to the
CommandTester
? Passing arguments is fine, but I can't find docs on how to do it for Options.

Answer

You should pass the options with -- so try this:

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

instead of this:

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

Hope this help