Как подтвердить, что строка зарегистрирована с помощью Monolog внутри Symfony

Я использую Monolog внутри Symfony2, используя MonologBundle по умолчанию. Я пытаюсь утверждать в своих тестах, что строка регистрируется. Я настроил это в своем config_test.yml:

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

Как мне получить результаты TestHandler от Monolog в моих тестах (которые унаследованы от WebTestCase от Symfony2)?


person hvtilborg    schedule 05.06.2015    source источник


Ответы (2)


Как решение:

Получить все обработчики из службы 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
person ZhukV    schedule 06.06.2015
comment
Обратите внимание: этот метод устарел в Symfony 3.4 и не работает в Symfony 4. - person aneth101; 19.03.2018

В вашем классе команд вы должны просто установить обработчик с помощью 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())

Это решение было найдено здесь и объясняется в документации Monolog здесь.

Подробнее о Monolog и Symfony (Symfony Docu).

Подробнее о обработчиках Monolog (документ Monolog) .

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);
    }
}
person Aerendir    schedule 13.08.2015
comment
Я не пытался утверждать строки журнала в команде. Было бы оценено более общее, общее решение. - person hvtilborg; 14.08.2015
comment
Я думаю, что не понимаю, чего вы хотели бы достичь... Можете ли вы уточнить свой вопрос, добавив больше деталей? Опубликуйте код теста, чтобы мы могли увидеть, как вы его реализовали, и дать вам решение. - person Aerendir; 14.08.2015