0

I am unable to set Argument while creating Unit test case in Symfony 3.4 console command application

My Console command

php bin\console identification-requests:process input.csv

My Console code

protected function execute(InputInterface $input, OutputInterface $output)
{
    // Retrieve the argument value using getArgument()
    $csv_name = $input->getArgument('file');

    // Check file name
    if ($csv_name == 'input.csv') {
        // Get input file from filesystem
        $csvData = array_map('str_getcsv', file($this->base_path.$csv_name));
        $formatData = Helpers::formatInputData($csvData);

        // Start session
        $session = new Session();
        $session->start();

        foreach ($csvData as $key => $data) {
            if (!empty($data[0])) {
                $validation = Validator::getInformationData($data, $formatData[$data[1]]);
                if (!empty($validation)) {
                    $output->writeln($validation);
                } else {
                    $output->writeln('valid');
                }
            }
        }
    } else {
        $output->writeln('Invalid file!');
    }
}

I tried the following test code

$kernel = static::createKernel();
$kernel->boot();

$application = new Application($kernel);
$application->add(new DocumentCommand());

$command = $application->find('identification-requests:process')
                        ->addArgument('file', InputArgument::REQUIRED, "input.csv");
$commandTester = new CommandTester($command);
$commandTester->execute(array(
    'command' => $command->getName()
));

$output = $commandTester->getOutput();
$this->assertContains('valid',$output);

When I run unit test it showing the following error message

There was 1 error:

1) Tests\AppBundle\Command\DocumentCommandTest::testExecute
Symfony\Component\Console\Exception\LogicException: An argument with name "file" already exists.
Community
  • 1
  • 1
Abdul Awal
  • 171
  • 2
  • 10

1 Answers1

2

I think you should put your input in the command tester and not in the command finder, in this case you are trying to create another parameter for that command, and that's why it's telling you that it is existing already. try this

$command = $application->find('identification-requests:process');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
    'command' => $command->getName(),
    'file' => 'input.csv'
));
Did
  • 493
  • 5
  • 19
  • Thanks @Selimi, it solved, but I got another new problem. I am using session to store data in my console command and when Unit test run it return this error `RuntimeException: Failed to start the session because headers have already been sent` can you please help me on that too? I am updating this question by adding console command section – Abdul Awal Aug 26 '19 at 20:20
  • That's a different problem, you should put it as a separate question, regardless this might help u with that https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Did Aug 26 '19 at 20:35