LogsHttpRegisterEventTest.php in Logs HTTP 8
File
tests/src/Kernel/LogsHttpRegisterEventTest.php
View source
<?php
namespace Drupal\Tests\logs_http\Kernel;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\KernelTests\KernelTestBase;
class LogsHttpRegisterEventTest extends KernelTestBase {
private $logsHttpConfig;
private $logsHttpLogger;
protected static $modules = [
'logs_http',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig(static::$modules);
$this->logsHttpConfig = \Drupal::configFactory()
->getEditable('logs_http.settings');
$this->logsHttpConfig
->set('url', 'http://www.example.com');
$this->logsHttpConfig
->save();
$this->logsHttpLogger = \Drupal::service('logs_http.logs_http_logger');
}
public function testRegisterEvent() {
\Drupal::logger('logs_http')
->notice('Notice 1');
$events = $this->logsHttpLogger
->getEvents();
$this
->assertEmpty($events, 'No notice events registered, as severity level was too high.');
$this->logsHttpConfig
->set('severity_level', RfcLogLevel::NOTICE);
$this->logsHttpConfig
->save();
$this->logsHttpLogger
->reset();
\Drupal::logger('logs_http')
->error('Notice 1');
$events = $this->logsHttpLogger
->getEvents();
$this
->assertEquals(1, count($events), 'Notice events registered.');
$this->logsHttpLogger
->reset();
\Drupal::logger('logs_http')
->notice('Notice 1');
\Drupal::logger('logs_http')
->notice('Notice 1');
\Drupal::logger('logs_http')
->notice('Notice 2');
$events = $this->logsHttpLogger
->getEvents();
$this
->assertEquals(2, count($events), 'Correct number of events registered.');
$event1 = array_shift($events);
$event2 = array_shift($events);
$this
->assertEquals('Notice 1', $event1['message'], 'Correct first event registered.');
$this
->assertEquals('Notice 2', $event2['message'], 'Correct second event registered.');
}
}