我有一个运行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'));
正如这里所建议的。我也尝试了其他一些排列。但是它们都没有做我想要的,即关闭日志中的所有调试消息。有没有办法在测试中做我想做的事?
禁用内核中的调试
您可以在测试的 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
的测试环境执行命令
显然
--no-debug
禁用显示错误的调试模式。尝试使用 --env=test
在测试环境中运行命令。