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
namespace AppBundle\Command;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class YourCommand extends ContainerAwareCommand
{
// ...
protected function execute(InputInterface $input, OutputInterface $output)
{
$logger = $this->getContainer()->get('logger');
// PUSH THE OutputInterface OBJECT INTO MONOLOG
$logger->pushHandler(new ConsoleHandler($output));
// Your command logic here...
}
在您的测试中,使用CommandTester:
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());
}
}
namespace App\Command;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class YourCommand extends Command
{
protected static $defaultName = 'acme:your:command';
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// PUSH THE OutputInterface OBJECT INTO MONOLOG
if ($this->logger instanceof Logger) {
$this->logger->pushHandler(new ConsoleHandler($output));
}
// Your command logic here...
return self::SUCCESS;
}
}
在您的测试中,使用CommandTester:
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 = static::createKernel();
$application = new Application($kernel);
$command = $application->find('acme:your:command');
$commandTester = new CommandTester($command);
$commandTester->execute(
['command' => $command->getName()],
/**
* Here set the verbosity
*/
['verbosity' => OutputInterface::VERBOSITY_DEBUG]
);
$output = $commandTester->getDisplay();
// die(print_r($commandTester->getDisplay()));
self::assertStringContainsString('/.../', $$output);
}
}
monolog:
handlers:
testing:
type: test
level: info
在你的测试中:
use Monolog\Handler\TestHandler;
// ...
private TestHandler $testHandler;
public function setUp(): void
{
parent::setUp();
$this->testHandler = $this->get('monolog.handler.testing');
}
public function testSomething()
{
// ...
// Get records
$this->testHandler->getRecords();
}
如果你有一个测试的基类,你可以创建一个帮助器:
protected function assertIsLogged(string $partOfMessage, int $level = Logger::DEBUG): void
{
/** @var TestHandler $handler */
$handler = $this->get('monolog.handler.testing');
foreach ($handler->getRecords() as $record) {
// Ignore logs below expected level
if ($record['level'] < $level) {
continue;
}
if (\str_contains($record['message'], $partOfMessage)) {
$this->addToAssertionCount(1);
return;
}
}
$this->fail(sprintf('Failed to assert that a log contains "%s" for minimum level %s.', $partOfMessage, Logger::getLevelName($level)));
}
3条答案
按热度按时间kq0g1dla1#
作为溶液:
从
monolog
服务获取所有处理程序并搜索测试处理程序。dohp0rv52#
在你的命令类中,你必须简单地用
pushHandler()
设置处理程序:在您的测试中,使用
CommandTester
:关注
array('verbosity' => OutputInterface::VERBOSITY_DEBUG)
。这样你就可以获得所有的日志(在本例中是INFO,用
$logger->info('Starting <info>acme:your:command</info>');
设置):现在您可以使用
$this->assertRegExp()
来检查是否记录了特定的行。您还可以使用以下命令将
string
转换为array
该溶液为found here,并在此处的Monolog文件中进行了解释。
关于Monolog and Symfony (Symfony Docu)
Monolog Handlers(Monolog Docu)
Symfony 5(自动装配),PHP 7.4
在您的测试中,使用
CommandTester
:gudnpqoy3#
很老的问题,但给我2美分。
处理程序都注册为服务,因此您可以轻松使用依赖注入。
Package /检测/monolog.yaml
在你的测试中:
如果你有一个测试的基类,你可以创建一个帮助器: