3 namespace Drupal\KernelTests\Core\DrupalKernel;
5 use Drupal\Core\DrupalKernel;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
10 * Tests DIC compilation to disk.
14 class DrupalKernelTest extends KernelTestBase {
19 protected function setUp() {
20 // DrupalKernel relies on global $config_directories and requires those
21 // directories to exist. Therefore, create the directories, but do not
22 // invoke KernelTestBase::setUp(), since that would set up further
23 // environment aspects, which would distort this test, because it tests
24 // the DrupalKernel (re-)building itself.
25 $this->root = static::getDrupalRoot();
26 $this->bootEnvironment();
30 * Build a kernel for testings.
32 * Because the bootstrap is in DrupalKernel::boot and that involved loading
33 * settings from the filesystem we need to go to extra lengths to build a kernel
36 * @param \Symfony\Component\HttpFoundation\Request $request
37 * A request object to use in booting the kernel.
38 * @param array $modules_enabled
39 * A list of modules to enable on the kernel.
41 * @return \Drupal\Core\DrupalKernel
42 * New kernel for testing.
44 protected function getTestKernel(Request $request, array $modules_enabled = NULL) {
45 // Manually create kernel to avoid replacing settings.
46 $class_loader = require $this->root . '/autoload.php';
47 $kernel = DrupalKernel::createFromRequest($request, $class_loader, 'testing');
48 $this->setSetting('container_yamls', []);
49 $this->setSetting('hash_salt', $this->databasePrefix);
50 if (isset($modules_enabled)) {
51 $kernel->updateModules($modules_enabled);
59 * Tests DIC compilation.
61 public function testCompileDIC() {
62 // @todo: write a memory based storage backend for testing.
68 $request = Request::createFromGlobals();
69 $this->getTestKernel($request, $modules_enabled);
71 // Instantiate it a second time and we should get the compiled Container
73 $kernel = $this->getTestKernel($request);
74 $container = $kernel->getContainer();
75 $refClass = new \ReflectionClass($container);
76 $is_compiled_container = !$refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
77 $this->assertTrue($is_compiled_container);
78 // Verify that the list of modules is the same for the initial and the
79 // compiled container.
80 $module_list = array_keys($container->get('module_handler')->getModuleList());
81 $this->assertEqual(array_values($modules_enabled), $module_list);
83 // Get the container another time, simulating a "production" environment.
84 $container = $this->getTestKernel($request, NULL)
87 $refClass = new \ReflectionClass($container);
88 $is_compiled_container = !$refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
89 $this->assertTrue($is_compiled_container);
91 // Verify that the list of modules is the same for the initial and the
92 // compiled container.
93 $module_list = array_keys($container->get('module_handler')->getModuleList());
94 $this->assertEqual(array_values($modules_enabled), $module_list);
96 // Test that our synthetic services are there.
97 $class_loader = $container->get('class_loader');
98 $refClass = new \ReflectionClass($class_loader);
99 $this->assertTrue($refClass->hasMethod('loadClass'), 'Container has a class loader');
101 // We make this assertion here purely to show that the new container below
102 // is functioning correctly, i.e. we get a brand new ContainerBuilder
103 // which has the required new services, after changing the list of enabled
105 $this->assertFalse($container->has('service_provider_test_class'));
107 // Add another module so that we can test that the new module's bundle is
108 // registered to the new container.
109 $modules_enabled['service_provider_test'] = 'service_provider_test';
110 $this->getTestKernel($request, $modules_enabled);
112 // Instantiate it a second time and we should not get a ContainerBuilder
113 // class because we are loading the container definition from cache.
114 $kernel = $this->getTestKernel($request, $modules_enabled);
115 $container = $kernel->getContainer();
117 $refClass = new \ReflectionClass($container);
118 $is_container_builder = $refClass->isSubclassOf('Symfony\Component\DependencyInjection\ContainerBuilder');
119 $this->assertFalse($is_container_builder, 'Container is not a builder');
121 // Assert that the new module's bundle was registered to the new container.
122 $this->assertTrue($container->has('service_provider_test_class'), 'Container has test service');
124 // Test that our synthetic services are there.
125 $class_loader = $container->get('class_loader');
126 $refClass = new \ReflectionClass($class_loader);
127 $this->assertTrue($refClass->hasMethod('loadClass'), 'Container has a class loader');
129 // Check that the location of the new module is registered.
130 $modules = $container->getParameter('container.modules');
131 $this->assertEqual($modules['service_provider_test'], [
133 'pathname' => drupal_get_filename('module', 'service_provider_test'),
137 // Check that the container itself is not among the persist IDs because it
138 // does not make sense to persist the container itself.
139 $persist_ids = $container->getParameter('persist_ids');
140 $this->assertSame(FALSE, array_search('service_container', $persist_ids));
144 * Tests repeated loading of compiled DIC with different environment.
146 public function testRepeatedBootWithDifferentEnvironment() {
147 $request = Request::createFromGlobals();
148 $class_loader = require $this->root . '/autoload.php';
157 foreach ($environments as $environment) {
158 $kernel = DrupalKernel::createFromRequest($request, $class_loader, $environment);
159 $this->setSetting('container_yamls', []);
160 $this->setSetting('hash_salt', $this->databasePrefix);
164 $this->pass('Repeatedly loaded compiled DIC with different environment');
168 * Tests setting of site path after kernel boot.
170 public function testPreventChangeOfSitePath() {
171 // @todo: write a memory based storage backend for testing.
173 'system' => 'system',
177 $request = Request::createFromGlobals();
178 $kernel = $this->getTestKernel($request, $modules_enabled);
181 $kernel->setSitePath('/dev/null');
183 catch (\LogicException $e) {
186 $this->assertTrue($pass, 'Throws LogicException if DrupalKernel::setSitePath() is called after boot');
188 // Ensure no LogicException if DrupalKernel::setSitePath() is called with
189 // identical path after boot.
190 $path = $kernel->getSitePath();
191 $kernel->setSitePath($path);