function DrupalKernelRequestStackTest::testRequestStackHandling

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelRequestStackTest.php \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelRequestStackTest::testRequestStackHandling()

Tests request stack when sub requests are made.

It compares master, current, and parent Request objects before and after StackedHttpKernel::handle(), StackedHttpKernel::terminate(), and sub requests

File

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

Class

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

Namespace

Drupal\KernelTests\Core\DrupalKernel

Code

public function testRequestStackHandling() : void {
  /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
  $request_stack = \Drupal::service('request_stack');
  // KernelTestBase pushes a request on to the stack.
  $request_stack->pop();
  $http_kernel = \Drupal::service('kernel');
  $main_request = Request::create('/http-kernel-test');
  $sub_request_1 = Request::create('/http-kernel-test');
  $sub_request_2 = Request::create('/http-kernel-test-sub-request');
  $request_404 = Request::create('/does_not_exist');
  $this->assertNull($request_stack->getMainRequest());
  // Make the main request.
  $this->recordedRequestStackCount = $this->recordedRequests = NULL;
  $main_response = $http_kernel->handle($main_request);
  $this->assertSame($main_request, $request_stack->getMainRequest());
  $this->assertSame($main_request, $request_stack->getCurrentRequest());
  $this->assertSame($main_request, $this->recordedRequests['current']);
  $this->assertSame($main_request, $this->recordedRequests['main']);
  $this->assertNull($this->recordedRequests['parent']);
  $this->assertSame(1, $this->recordedRequestStackCount);
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  // Make a sub request.
  $this->recordedRequestStackCount = $this->recordedRequests = NULL;
  $http_kernel->handle($sub_request_1, HttpKernelInterface::SUB_REQUEST);
  $this->assertSame($main_request, $request_stack->getMainRequest());
  $this->assertSame($main_request, $request_stack->getCurrentRequest());
  $this->assertSame($sub_request_1, $this->recordedRequests['current']);
  $this->assertSame($main_request, $this->recordedRequests['main']);
  $this->assertSame($main_request, $this->recordedRequests['parent']);
  $this->assertSame(2, $this->recordedRequestStackCount);
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  // Make a sub request that makes a sub request.
  $this->recordedRequestStackCount = $this->recordedRequests = NULL;
  $http_kernel->handle($sub_request_2, HttpKernelInterface::SUB_REQUEST);
  $this->assertSame($main_request, $request_stack->getMainRequest());
  $this->assertSame($main_request, $request_stack->getCurrentRequest());
  $this->assertNotSame($sub_request_2, $this->recordedRequests['current']);
  $this->assertSame('/http-kernel-test-sub-sub-request', $this->recordedRequests['current']
    ->getPathInfo());
  $this->assertSame($sub_request_2, $this->recordedRequests['parent']);
  $this->assertSame($main_request, $this->recordedRequests['main']);
  $this->assertSame(3, $this->recordedRequestStackCount);
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  // Make 404 sub request.
  $this->recordedRequestStackCount = $this->recordedRequests = NULL;
  $http_kernel->handle($request_404, HttpKernelInterface::SUB_REQUEST);
  $this->assertSame($main_request, $request_stack->getMainRequest());
  $this->assertSame($main_request, $request_stack->getCurrentRequest());
  $this->assertNotSame($request_404, $this->recordedRequests['current']);
  $this->assertSame('/does_not_exist', $this->recordedRequests['current']
    ->getPathInfo());
  $this->assertSame('system.404', $this->recordedRequests['current']->attributes
    ->get(RouteObjectInterface::ROUTE_NAME));
  $this->assertSame($request_404, $this->recordedRequests['parent']);
  $this->assertSame($main_request, $this->recordedRequests['main']);
  $this->assertSame(3, $this->recordedRequestStackCount);
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  $http_kernel->terminate($main_request, $main_response);
  // After termination the stack should be empty.
  $this->assertNull($request_stack->getMainRequest());
  $this->assertSame(0, $this->getRequestStackCount($request_stack));
  // Make 404 main request.
  $this->recordedRequestStackCount = $this->recordedRequests = NULL;
  $response_404 = $http_kernel->handle($request_404);
  $this->assertSame($request_404, $request_stack->getMainRequest());
  $this->assertSame($request_404, $request_stack->getCurrentRequest());
  $this->assertNotSame($request_404, $this->recordedRequests['current']);
  $this->assertSame('/does_not_exist', $this->recordedRequests['current']
    ->getPathInfo());
  $this->assertSame('system.404', $this->recordedRequests['current']->attributes
    ->get(RouteObjectInterface::ROUTE_NAME));
  $this->assertSame($request_404, $this->recordedRequests['parent']);
  $this->assertSame($request_404, $this->recordedRequests['main']);
  $this->assertSame(2, $this->recordedRequestStackCount);
  $this->assertSame(1, $this->getRequestStackCount($request_stack));
  $http_kernel->terminate($request_404, $response_404);
  // After termination the stack should be empty.
  $this->assertNull($request_stack->getMainRequest());
  $this->assertSame(0, $this->getRequestStackCount($request_stack));
}

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