Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / DrupalKernel / ServiceDestructionTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\DrupalKernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Symfony\Component\HttpFoundation\Response;
7
8 /**
9  * Tests that services are correctly destructed.
10  *
11  * @group DrupalKernel
12  */
13 class ServiceDestructionTest extends KernelTestBase {
14
15   /**
16    * Verifies that services are destructed when used.
17    */
18   public function testDestructionUsed() {
19     // Enable the test module to add it to the container.
20     $this->enableModules(['service_provider_test']);
21
22     $request = $this->container->get('request_stack')->getCurrentRequest();
23     $kernel = $this->container->get('kernel');
24     $kernel->preHandle($request);
25
26     // The service has not been destructed yet.
27     $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));
28
29     // Call the class and then terminate the kernel
30     $this->container->get('service_provider_test_class');
31
32     $response = new Response();
33     $kernel->terminate($request, $response);
34     $this->assertTrue(\Drupal::state()->get('service_provider_test.destructed'));
35   }
36
37   /**
38    * Verifies that services are not unnecessarily destructed when not used.
39    */
40   public function testDestructionUnused() {
41     // Enable the test module to add it to the container.
42     $this->enableModules(['service_provider_test']);
43
44     $request = $this->container->get('request_stack')->getCurrentRequest();
45     $kernel = $this->container->get('kernel');
46     $kernel->preHandle($request);
47
48     // The service has not been destructed yet.
49     $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));
50
51     // Terminate the kernel. The test class has not been called, so it should not
52     // be destructed.
53     $response = new Response();
54     $kernel->terminate($request, $response);
55     $this->assertNull(\Drupal::state()->get('service_provider_test.destructed'));
56   }
57
58 }