Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 495
crmpicco Avatar asked Oct 20 '25 13:10

crmpicco


1 Answers

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

like image 57
Matteo Avatar answered Oct 23 '25 02:10

Matteo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!