Symfony 3.4 - "no commands defined in the “doctrine:schema” namespace" when attempting to run console command in class
Asked Answered
H

3

2

I'm following along with this blog to create unit tests that use my data fixtures as a base. The relevant code:

namespace Tests\AppBundle\Repository;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DataFixtureTestCase extends WebTestCase
{
    protected $client, $container, $em;
    protected static $application;

    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        self::runCommand('doctrine:schema:drop --force');
        self::runCommand('doctrine:schema:create');
        self::runCommand('doctrine:fixtures:load --append --no-interaction --force');

        $this->client = static::createClient();
        $this->container = $this->client->getContainer();
        $this->em = $this->container->get('doctrine')->getManager();
    }

    protected static function runCommand($command)
    {
        $command = sprintf('%s --quiet', $command);

        try {
            return self::getApplication()->run(new StringInput($command));
        } catch(\Exception $e) {
            echo $e->getMessage();
        }
    }

    protected static function getApplication()
    {
        if (null === self::$application) {
            $client = static::createClient();

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

        return self::$application;
    }

    // other methods not relevant to the question

My unit test classes extend DataFixtureTestCase. When setUp() is invoked, I keep getting errors like:

There are no commands defined in the "doctrine:schema" namespace

Any ideas on what's causing this? My research hasn't revealed the cause of the error. Doctrine is correctly installed via Composer, and I can run the console commands manually in a terminal.

Hadley answered 21/2, 2018 at 1:23 Comment(1)
I think then the issue could be the working directory here. Do you see output of these commands? Can you print the current working directory in your PHP code and see if that is not what it should be when you run from terminal. Also you are missing parent::setUp();, I assume you shouldn't do thatVaughn
G
14

Your problem is that you are using the wrong Application class for this.

Have a look at your bin/console php file. github link

Change:

use Symfony\Component\Console\Application;

to

use Symfony\Bundle\FrameworkBundle\Console\Application;
Gadroon answered 23/2, 2018 at 16:10 Comment(0)
S
1

doctrine/doctrine-bundle and doctrine/doctrine-fixtures-bundle should be installed to run doctrine:schema and doctrine:fixtures commands

Supermarket answered 23/2, 2018 at 6:37 Comment(1)
I wouldn't be trying to run those console commands if I hadn't already installed those packages via Composer :PHadley
W
1

As suggested by Denis, try adding Doctrine bundles via componser:

composer require doctrine/doctrine-bundle
composer require doctrine/doctrine-fixtures-bundle --dev
Wacke answered 23/2, 2018 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.