0

I'm trying to run a fixture command from php code while running my phpunit tests. It ends up in the following error: Symfony\\Component\\Console\\Exception\\CommandNotFoundException] There are no commands defined in the \u0022doctrine:fixtures

I must say that I've been running the same stuff on my controller and it worked perfectly. Not only cannot it find the doctrine:fixtures command but also all the others.

It is as if all commands were unreachable while running tests.

This is my code:

namespace tests\functional\User\ManagerBundle\Controller;    

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\BufferedOutput;

class ClassControllerTest extends WebTestCase
{
   protected function setUp()
   {
        $client = static::createClient();

        $application = new Application($client->getKernel());
        $application->setAutoExit(false);

        $input = new ArrayInput(array(
            'command' => 'doctrine:fixtures:load',
            // (optional) define the value of command arguments
//            'fooArgument' => 'barValue',
            // (optional) pass options to the command
            '--no-interaction' => true,
            '--env' => 'TEST'
        ));

        // You can use NullOutput() if you don't need the output
        $output = new BufferedOutput();
        $application->run($input, $output);

        // return the output, don't use if you used NullOutput()
        $content = $output->fetch();

        var_dump(new JsonResponse($content));

   }
}
Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39
  • 2
    Possible duplicate of [How to run command from bundle in app controller in Symfony3?](https://stackoverflow.com/questions/43617903/how-to-run-command-from-bundle-in-app-controller-in-symfony3) – yceruto Nov 06 '17 at 16:31
  • THANK YOU!! That solved my problem. However I don't think it may be considered a duplicate. In this case I needed to use `Symfony\Bundle\FrameworkBundle\Console\Application` rather than `Symfony\Component\Console\Application` – Roberto Rizzi Nov 06 '17 at 16:35
  • 2
    Well, that's exactly what the related answer says, probably you've read too fast :) – yceruto Nov 06 '17 at 16:45

1 Answers1

3

Simple answer:

I had to use the following statement

use Symfony\Bundle\FrameworkBundle\Console\Application;

instead of

use Symfony\Component\Console\Application;
Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39