【问题标题】:How do I set a command-line flag when running a Symfony2 Command test?运行 Symfony2 命令测试时如何设置命令行标志?
【发布时间】:2013-06-18 23:43:02
【问题描述】:

我有一个运行 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。我也尝试了其他一些排列。但是他们都没有做我想要的,即关闭日志中的所有调试消息。有没有办法在测试中做我想做的事?

【问题讨论】:

    标签: php testing symfony


    【解决方案1】:

    在内核中禁用调试

    您可以在测试的 setUp() 方法中初始化内核,并将 debug 设置为 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的测试环境执行你的命令

    【讨论】:

    • manual page 表示“除了更改环境之外,您还可以选择禁用调试模式。这在您希望在开发环境中运行命令但避免性能下降的情况下很有用收集调试数据:$ php app/console list --no-debug。”而我的经验正是如此,当我从命令行运行 --no-debug 标志时,它会关闭调试。我知道在配置文件中设置日志级别,但我只想关闭这个特定测试的调试,而不是所有测试。
    • 好点如何在您的测试中不调试而初始化内核? $kernel = new AppKernel('test', false);
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2015-05-22
      • 2016-02-03
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      相关资源
      最近更新 更多