如何使用Symfony中的MonologAssert一行被记录

nwo49xxi  于 2023-04-12  发布在  其他
关注(0)|答案(3)|浏览(91)

我在Symfony2中使用Monolog,使用默认的MonologBundle。我试图在我的测试中Assert,一行被记录。我已经在我的config_test.yml中配置了这个:

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

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

kq0g1dla

kq0g1dla1#

作为溶液:
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
dohp0rv5

dohp0rv52#

在你的命令类中,你必须简单地用pushHandler()设置处理程序:

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());
    }
}

关注array('verbosity' => OutputInterface::VERBOSITY_DEBUG)

这样你就可以获得所有的日志(在本例中是INFO,用$logger->info('Starting <info>acme:your:command</info>');设置):

[2015-08-13 23:39:22] app.INFO: Starting acme:your:command:

现在您可以使用$this->assertRegExp()来检查是否记录了特定的行。
您还可以使用以下命令将string转换为array

explode('\n', $commandTester->getDisplay())

该溶液为found here,并在此处的Monolog文件中进行了解释。
关于Monolog and Symfony (Symfony Docu)
Monolog Handlers(Monolog Docu)

Symfony 5(自动装配),PHP 7.4

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);
    }
}
gudnpqoy

gudnpqoy3#

很老的问题,但给我2美分。
处理程序都注册为服务,因此您可以轻松使用依赖注入。
Package /检测/monolog.yaml

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)));
}

相关问题