Pass console command option through to Symfony CommandTester in PHPUnit test
php, php-7, phpunit, symfony, unit-testing
Solution
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
Problem
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.