Symfony 3.4 - "no commands defined in the “doctrine:schema” namespace" 在 class 中尝试 运行 控制台命令时

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

我将与 this blog 一起创建使用我的数据装置作为基础的单元测试。相关代码:

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

我的单元测试 类 扩展 DataFixtureTestCase。调用 setUp() 时,我不断收到如下错误:

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

对造成这种情况的原因有任何想法吗?我的研究没有揭示错误的原因。 Doctrine 已通过 Composer 正确安装,我可以 运行 在终端中手动执行控制台命令。

doctrine/doctrine-bundledoctrine/doctrine-fixtures-bundle 应该安装到 运行 doctrine:schemadoctrine:fixtures 命令

按照 Denis 的建议,尝试通过 componser 添加 Doctrine 包:

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

您的问题是您为此使用了错误的 Application class。

查看您的 bin/console php 文件。 github link

变化:

use Symfony\Component\Console\Application;

use Symfony\Bundle\FrameworkBundle\Console\Application;