Мне удалось протестировать вывод Monolog, добавив TestHandler в monolog "config / packages / test / monolog.yaml"
monolog:
handlers:
test:
type: test
level: debug
Вот код в моем TestClass
class MyClassCommandTest extends KernelTestCase
{
/**
* First validation tests all invalid options
*/
public function testMyCommand()
{
$kernel = static::createKernel();
$application = new Application($kernel);
$command = $application->find('my:command:name');
$commandTester = new CommandTester($command);
$options['command'] = $command->getName();
$commandTester->execute([
'option_name' => 'value'
], [
'verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE
]);
//I injected the logger inside my command and added a function getLogger to access it
$logger = $command->getLogger();
$handlers = $logger->getHandlers();
$logs = null
foreach ($handlers as $handler) {
if ($handler instanceof TestHandler) {
$logs = $handler;
}
}
$this->assertTrue($logs->hasRecordThatContains('ERROR_STRING_1', Logger::ERROR), 'Missing Error');
$this->assertTrue($logs->hasRecordThatContains('CRITICAL_STRING_1', Logger::CRITICAL), 'Missing Critical Error');
$this->assertFalse($logs->hasRecordThatContains('SUCCESS_STRING_1', Logger::INFO), 'Has Success Message');
}
}
У кого-нибудь есть другие предложения о том, как проверить вывод?