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