1

我有一个运行 Symfony 2.0 命令的测试。在测试中,我像这样运行命令:

$application = new Application(static::$kernel);
$application->add(new MyCommand());

$command = $application->find('mycommand:name');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));

我想使用 --no-debug 标志运行命令,它是 app:console 的内置标志(我猜?)。我试过了:

$commandTester->execute(array('command' => $command->getName(), '--no-debug' => true));

我也试过:

$application->run(new StringInput('mycommand:name --no-debug'));

正如这里建议的那样。我也尝试了其他一些排列。但是他们都没有做我想要的,即关闭日志中的所有调试消息。有没有办法在测试中做我想做的事?

4

2 回答 2

2

在内核中禁用调试

您可以在测试的 setUp() 方法中初始化内核,并将调试设置为 false。

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class YourCommandTest extends WebTestCase
{

   public function setUp()
   {
      static::$kernel = static::createKernel(array('debug' => false));
      static::$kernel->boot();

      // ...
   }

看看WebTestCase->createKernel()

使用环境

如果您不希望日志中的调试输出编辑/创建app/config/config_test.yml和编辑...

monolog:
    handlers:
        file:
           type: stream
            level: debug       # log-level ...   

... 到另一个适合您需要的日志级别。

然后使用测试环境执行您的命令--env=test

于 2013-06-18T23:58:53.910 回答
0

--no-debug显然禁用显示错误的调试模式。尝试在测试环境中使用--env=test.

于 2013-06-19T06:03:11.207 回答