3 namespace Drupal\KernelTests\Core\DrupalKernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Symfony\Component\HttpFoundation\Response;
9 * Tests that services are correctly destructed.
13 class ServiceDestructionTest extends KernelTestBase {
16 * Verifies that services are destructed when used.
18 public function testDestructionUsed() {
19 // Enable the test module to add it to the container.
20 $this->enableModules(['service_provider_test']);
22 $request = $this->container->get('request_stack')->getCurrentRequest();
23 $kernel = $this->container->get('kernel');
24 $kernel->preHandle($request);
26 // The service has not been destructed yet.
27 $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));
29 // Call the class and then terminate the kernel
30 $this->container->get('service_provider_test_class');
32 $response = new Response();
33 $kernel->terminate($request, $response);
34 $this->assertTrue(\Drupal::state()->get('service_provider_test.destructed'));
38 * Verifies that services are not unnecessarily destructed when not used.
40 public function testDestructionUnused() {
41 // Enable the test module to add it to the container.
42 $this->enableModules(['service_provider_test']);
44 $request = $this->container->get('request_stack')->getCurrentRequest();
45 $kernel = $this->container->get('kernel');
46 $kernel->preHandle($request);
48 // The service has not been destructed yet.
49 $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));
51 // Terminate the kernel. The test class has not been called, so it should not
53 $response = new Response();
54 $kernel->terminate($request, $response);
55 $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));