function ServiceDestructionTest::testDestructionUsed

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/DrupalKernel/ServiceDestructionTest.php \Drupal\KernelTests\Core\DrupalKernel\ServiceDestructionTest::testDestructionUsed()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/DrupalKernel/ServiceDestructionTest.php \Drupal\KernelTests\Core\DrupalKernel\ServiceDestructionTest::testDestructionUsed()
  3. 10 core/tests/Drupal/KernelTests/Core/DrupalKernel/ServiceDestructionTest.php \Drupal\KernelTests\Core\DrupalKernel\ServiceDestructionTest::testDestructionUsed()

Verifies that services are destructed when used.

File

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

Class

ServiceDestructionTest
Tests that services are correctly destructed.

Namespace

Drupal\KernelTests\Core\DrupalKernel

Code

public function testDestructionUsed() : void {
    // 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'));
    // Call the class and then terminate the kernel
    $this->container
        ->get('service_provider_test_class');
    $response = new Response();
    $kernel->terminate($request, $response);
    $this->assertTrue(\Drupal::state()->get('service_provider_test.destructed'));
}

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