function ExceptionLoggingSubscriberTest::testExceptionLogging

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/EventSubscriber/ExceptionLoggingSubscriberTest.php \Drupal\KernelTests\Core\EventSubscriber\ExceptionLoggingSubscriberTest::testExceptionLogging()

Tests \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber::onException().

@dataProvider exceptionDataProvider

File

core/tests/Drupal/KernelTests/Core/EventSubscriber/ExceptionLoggingSubscriberTest.php, line 37

Class

ExceptionLoggingSubscriberTest
Tests that HTTP exceptions are logged correctly.

Namespace

Drupal\KernelTests\Core\EventSubscriber

Code

public function testExceptionLogging(int $error_code, string $channel, int $log_level, string $exception = '') : void {
  $http_kernel = \Drupal::service('http_kernel');
  // Ensure that noting is logged.
  $this->assertEmpty($this->container
    ->get($this->testLogServiceName)
    ->cleanLogs());
  // Temporarily disable error log as the ExceptionLoggingSubscriber logs 5xx
  // HTTP errors using error_log().
  $error_log = ini_set('error_log', '/dev/null');
  $request = Request::create('/test-http-response-exception/' . $error_code);
  if ($exception) {
    $this->expectException($exception);
  }
  $http_kernel->handle($request);
  ini_set('error_log', $error_log);
  $logs = $this->container
    ->get($this->testLogServiceName)
    ->cleanLogs();
  $this->assertEquals($channel, $logs[0][2]['channel']);
  $this->assertEquals($log_level, $logs[0][0]);
  // Verify that @backtrace_string is removed from client error.
  if ($logs[0][2]['channel'] === 'client error') {
    $this->assertArrayNotHasKey('@backtrace_string', $logs[0][2]);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.