Symfony 3.4-“在"doctrine:schema"命名空间中未定义命令"尝试在类中运行控制台命令时 [英] Symfony 3.4 - "no commands defined in the “doctrine:schema” namespace" when attempting to run console command in class
问题描述
我正在与此博客一起创建广告单元以我的数据装置为基础的测试.相关代码:
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
我的单元测试类扩展了DataFixtureTestCase
.调用setUp()
时,我不断收到如下错误:
My unit test classes extend DataFixtureTestCase
. When setUp()
is invoked, I keep getting errors like:
在"doctrine:schema"命名空间中没有定义命令
There are no commands defined in the "doctrine:schema" namespace
关于什么原因的任何想法?我的研究尚未发现错误的原因.通过Composer正确安装了Doctrine,我可以在终端中手动运行控制台命令.
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.
推荐答案
您的问题是为此使用了错误的 Application 类.
Your problem is that you are using the wrong Application class for this.
查看您的 bin/console php文件. github链接
Have a look at your bin/console php file. github link
更改:
use Symfony\Component\Console\Application;
到
use Symfony\Bundle\FrameworkBundle\Console\Application;
这篇关于Symfony 3.4-“在"doctrine:schema"命名空间中未定义命令"尝试在类中运行控制台命令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!