function DrupalKernelRequestStackTest::testRequestStackAfterFailedTermination

Tests the request stack when termination fails.

File

core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelRequestStackTest.php, line 158

Class

DrupalKernelRequestStackTest
Tests the usage of the request stack as part of request processing.

Namespace

Drupal\KernelTests\Core\DrupalKernel

Code

public function testRequestStackAfterFailedTermination() : void {
  /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
  $request_stack = \Drupal::service('request_stack');
  // KernelTestBase pushes a request onto the stack.
  $request_stack->pop();
  /** @var \Drupal\Core\DrupalKernel $http_kernel */
  $http_kernel = \Drupal::service('kernel');
  $main_request = Request::create('/http-kernel-test');
  $main_response = $http_kernel->handle($main_request);
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  $this->throwOnTerminate = TRUE;
  try {
    $http_kernel->terminate($main_request, $main_response);
    $this->fail('Expected exception was not thrown.');
  } catch (\RuntimeException $e) {
    $this->assertSame('Termination failed.', $e->getMessage());
  }
  // The main request is popped even though termination failed.
  $this->assertNull($request_stack->getMainRequest());
  $this->assertSame(0, $this->getRequestStackCount($request_stack));
  // A request that bypasses preHandle(), such as a page cache hit, must not
  // be terminated or pop a request it did not push.
  $other_request = Request::create('/http-kernel-test');
  $request_stack->push($other_request);
  $http_kernel->terminate($other_request, $main_response);
  $this->assertSame($other_request, $request_stack->getMainRequest());
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  $request_stack->pop();
  $this->throwOnTerminate = FALSE;
}

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