Abdul Awal
Abdul Awal

Reputation: 191

How to pass ContainerInterface from test class in symfony 3.4

I am testing command line application in Symfony Unit testing, in my command class I used container interface via constructor.

When I test it using unit test, it returns the following error Too few arguments to function AppBundle\Command\DocumentCommand::__construct(), 0 passed

My Command class

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class DocumentCommand extends Command
{
    private $container;
    // the name of the command (the part after "bin/console")
    protected static $defaultName = 'identification-requests:process';

    public function __construct(Container $container, bool $requirePassword = false)
    {
        $this->container = $container;
        $this->base_path = $this->container->get('kernel')->getRootDir();

        $this->requirePassword = $requirePassword;

        parent::__construct();
    }

Test class

use Symfony\Component\DependencyInjection\ContainerInterface;

class DocumentCommandTest extends KernelTestCase
{
    /** @var  Application $application */
    protected static $application;

    /** @var  Client $client */
    protected $client;

    /** @var  ContainerInterface $container */
    protected $container;

    /**
     * Test Execute
     *
     */
    public function testExecute()
    {
        $kernel = static::createKernel();
        $kernel->boot();

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

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

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

I tried to pass the container interface from test class but didn't work. Following is the error message

1) Tests\AppBundle\Command\DocumentCommandTest::testExecute
ArgumentCountError: Too few arguments to function AppBundle\Command\DocumentCommand::__construct(), 0 passed in \tests\AppBundle\Command\DocumentCommandTest.php on line 34 and at least 1 expected

Upvotes: 1

Views: 1090

Answers (1)

Manzolo
Manzolo

Reputation: 1959

Try passing container in testExecute:

$application->add(new DocumentCommand($kernel->getContainer()));

public function testExecute()
{
    $kernel = static::createKernel();
    $kernel->boot();

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

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

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

Upvotes: 1

Related Questions