如何在Symfony中使用Monolog声明一行日志
我在Symfony2中使用独白,使用默认的独白包。我试图在我的测试中断言,有一行记录在案。我已经在我的如何在Symfony中使用Monolog声明一行日志,symfony,phpunit,monolog,Symfony,Phpunit,Monolog,我在Symfony2中使用独白,使用默认的独白包。我试图在我的测试中断言,有一行记录在案。我已经在我的config\u test.yml中配置了这个: monolog: handlers: main: type: test level: debug 如何在测试中获得Monolog的TestHandler的结果(继承自Symfony2的WebTestCase)?作为解决方案: 从monolog服务和搜索测试处理程序中获
config\u 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
在命令类中,只需使用pushHandler()
设置处理程序:
在测试中,使用:
注意数组('verbosity'=>OutputInterface::verbosity\u DEBUG)
这样,您就可以获得所有日志(在本例中是一个信息,使用$logger->INFO('Starting acme:your:command');
)进行设置:
现在,您可以使用$this->assertRegExp()
检查是否记录了特定的行
您还可以使用转换数组中的字符串
explode('\n', $commandTester->getDisplay())
这个解决方案在Monolog的文档中有解释
更多关于
更多关于
Symfony 5(自动布线),PHP 7.4
namespace-App\Command;
使用Monolog\Logger;
使用Psr\Log\logger接口;
使用Symfony\Bridge\Monolog\Handler\ConsoleHandler;
使用Symfony\Component\Console\Command\Command;
使用Symfony\Component\Console\Input\InputInterface;
使用Symfony\Component\Console\Output\OutputInterface;
类YourCommand扩展命令
{
受保护的静态$defaultName='acme:your:command';
私人记录器接口$logger;
公共函数构造(LoggerInterface$logger)
{
$this->logger=$logger;
}
受保护的函数执行(InputInterface$input,OutputInterface$output)
{
//将OutputInterface对象推入MONOLOG
如果($this->logger instanceof logger){
$this->logger->pushHandler(新的控制台句柄($output));
}
//你的命令逻辑在这里。。。
回归自我:成功;
}
}
在测试中,使用:
namespace-AppBundle\Tests\Command;
使用AppBundle\Command\YourCommand;
使用Symfony\Bundle\FrameworkBundle\Console\Application;
使用Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
使用Symfony\Component\Console\Output\OutputInterface;
使用Symfony\Component\Console\Tester\CommandTester;
类YourCommandTest扩展了KernelTestCase
{
公共函数testExecute()
{
$kernel=static::createKernel();
$application=新应用程序($kernel);
$command=$application->find('acme:your:command');
$commandTester=新commandTester($command);
$commandTester->执行(
['command'=>$command->getName()],
/**
*在这里设置详细信息
*/
['verbosity'=>OutputInterface::verbosity\u DEBUG]
);
$output=$commandTester->getDisplay();
//模具(打印($commandTester->getDisplay());
self::assertStringContainsString(“/…/”,$$output);
}
}
我没有试图在命令中断言日志行。一个更通用的,全面的解决方案将不胜感激。我想我不明白你想要实现什么。。。你能用更多的细节更新你的问题吗?发布测试代码,这样我们就可以看到您是如何实现它的,并为您提供解决方案。注意:此方法在Symfony 3.4中已被弃用,并将在Symfony 4中中断
namespace AppBundle\Tests\Command;
use AppBundle\Command\YourCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class YourCommandTest extends KernelTestCase
{
public function testExecute()
{
$kernel = $this->createKernel();
$kernel->boot();
// mock the Kernel or create one depending on your needs
$application = new Application($kernel);
$application->add(new YourCommand());
$command = $application->find('acme:your:command');
$commandTester = new CommandTester($command);
$commandTester->execute(
array('command' => $command->getName()),
/**
* Here set the verbosity
*/
array('verbosity' => OutputInterface::VERBOSITY_DEBUG)
);
// die(print_r($commandTester->getDisplay()));
$this->assertRegExp('/.../', $commandTester->getDisplay());
}
}
[2015-08-13 23:39:22] app.INFO: Starting acme:your:command:
explode('\n', $commandTester->getDisplay())