如何断言在 Symfony 中使用 Monolog 记录一行

2023-12-23

我在 Symfony2 中使用 Monolog,使用默认的 MonologBu​​ndle。我试图在我的测试中断言已记录一行。我已经在我的config_test.yml:

monolog:
    handlers:
        main:
            type:   test
            level:  debug

我如何获得 Monolog 的结果TestHandler在我的测试中(继承自 Symfony2 的WebTestCase)?


作为解决方案:

获取所有处理程序monolog服务和搜索测试处理程序。

foreach ($this->container->get('monolog')->getHandlers() as $handler) {
  if ($handler instanceof TestHandler) {
    $testHandler = $handler;
    break;
  }
}

if (!$testHandler) {
  throw new \RuntimeException('Oops, not exist "test" handler in monolog.');
}

$this->assertFalse($testHandler->hasCritical()); // Or another assertions
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何断言在 Symfony 中使用 Monolog 记录一行 的相关文章

随机推荐