public function FileLogTest::testFileLog in File Log 8
Same name and namespace in other branches
- 2.0.x tests/src/Unit/FileLogTest.php \Drupal\Tests\filelog\Unit\FileLogTest::testFileLog()
Test a particular configuration.
Ensure that it logs the correct events.
@dataProvider providerFileLog
Parameters
array $config: The filelog settings.
array $events: The events to be logged.
string $expected: The expected content of the log file after the test.
File
- tests/
src/ Unit/ FileLogTest.php, line 92
Class
- FileLogTest
- Test the file logger.
Namespace
Drupal\Tests\filelog\UnitCode
public function testFileLog(array $config, array $events, $expected = '') : void {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'filelog.settings' => $config,
]);
/** @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject $state */
$state_data = [
'filelog.rotation' => 0,
];
$state = $this
->createMock(StateInterface::class);
$state
->method('get')
->willReturnCallback(static function ($key) use (&$state_data) {
return $state_data[$key];
});
$state
->method('set')
->willReturnCallback(static function ($key, $value) use (&$state_data) {
$state_data[$key] = $value;
});
$logger = new FileLog($configFactory, $state, $this->token, $this->time, $this->logMessageParser, new LogFileManager($configFactory, $this->fileSystem));
foreach ($events as $event) {
$logger
->log($event['level'], $event['message'], $event['context']);
}
// Read log output and remove file for the next test.
$content = '';
if ($this->virtualFileSystem
->hasChild(LogFileManager::FILENAME)) {
$content = file_get_contents($this->virtualFileSystem
->getChild(LogFileManager::FILENAME)
->url());
$this->virtualFileSystem
->removeChild(LogFileManager::FILENAME);
}
$this
->assertEquals($expected, $content);
// Check that the timestamp was saved if and only if a log was created.
$timestamp = $state
->get('filelog.rotation');
$this
->assertEquals($content ? $_SERVER['REQUEST_TIME'] : 0, $timestamp);
}