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

我是研究僧i 提交于 2019-12-11 06:47:25

问题


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.


回答1:


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;



回答2:


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




回答3:


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

composer require doctrine/doctrine-bundle
composer require doctrine/doctrine-fixtures-bundle --dev


来源:https://stackoverflow.com/questions/48896818/symfony-3-4-no-commands-defined-in-the-doctrineschema-namespace-when-atte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!