我有一个运行 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'));
按照建议here .我也尝试过其他一些排列。但是他们都没有做我想要的,即关闭日志中的所有调试消息。有没有办法在测试中做我想做的事情?
最佳答案
在内核中禁用调试
您可以在测试的 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 ...
... 到另一个 log level满足您的需求。
然后使用带有 --env=test
的测试环境执行您的命令
关于php - 运行 Symfony2 命令测试时如何设置命令行标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17180758/