In a test, how do I assert that nothing was logged? For the life of me, I can't figure this out.
You could for example check the content of the log file and assert that the file is empty.
You can use Monolog's test handler. The cleanest approach is to create a separate logging channel for tests:
// config/logging.php:
'channels' => [
(...)
'tests' => [
'driver' => 'monolog',
'handler' => Monolog\Handler\TestHandler::class,
],
];
// phpunit.xml:
<env name="LOG_CHANNEL" value="tests"/>
// Test case:
/** @var Monolog\Handler\TestHandler $handler */
$handler = Log::getLogger()->getHandlers()[0];
logger('Debug message');
$this->assertTrue($handler->hasDebugRecords());
report('Error message');
$this->assertTrue($handler->hasErrorRecords());
$this->assertFalse($handler->hasInfoRecords());
Your problem can be easily solved by Tim McDonald's log fake package . The assertNothingLogged function is right there.
You could get the log file, split and filter the content to transform it into a readable array, then take the last element and compare it with the value you expect in the log.
Use the File facade for this.
Please sign in or create an account to participate in this conversation.