You are here

public function ServiceDestructionTest::testDestructionUnused in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/DrupalKernel/ServiceDestructionTest.php \Drupal\KernelTests\Core\DrupalKernel\ServiceDestructionTest::testDestructionUnused()
  2. 10 core/tests/Drupal/KernelTests/Core/DrupalKernel/ServiceDestructionTest.php \Drupal\KernelTests\Core\DrupalKernel\ServiceDestructionTest::testDestructionUnused()

Verifies that services are not unnecessarily destructed when not used.

File

core/tests/Drupal/KernelTests/Core/DrupalKernel/ServiceDestructionTest.php, line 40

Class

ServiceDestructionTest
Tests that services are correctly destructed.

Namespace

Drupal\KernelTests\Core\DrupalKernel

Code

public function testDestructionUnused() {

  // Enable the test module to add it to the container.
  $this
    ->enableModules([
    'service_provider_test',
  ]);
  $request = $this->container
    ->get('request_stack')
    ->getCurrentRequest();
  $kernel = $this->container
    ->get('kernel');
  $kernel
    ->preHandle($request);

  // The service has not been destructed yet.
  $this
    ->assertNull(\Drupal::state()
    ->get('service_provider_test.destructed'));

  // Terminate the kernel. The test class has not been called, so it should not
  // be destructed.
  $response = new Response();
  $kernel
    ->terminate($request, $response);
  $this
    ->assertNull(\Drupal::state()
    ->get('service_provider_test.destructed'));
}