Sets up Drupal unit test environment.
Overrides UnitTestBase::setUp
DrupalUnitTestBase::$modules
protected function setUp() {
// Copy/prime extension file lists once to avoid filesystem scans.
if (!isset($this->moduleFiles)) {
$this->moduleFiles = \Drupal::state()
->get('system.module.files') ?: array();
$this->themeFiles = \Drupal::state()
->get('system.theme.files') ?: array();
$this->themeData = \Drupal::state()
->get('system.theme.data') ?: array();
}
$this->keyValueFactory = new KeyValueMemoryFactory();
parent::setUp();
// Build a minimal, partially mocked environment for unit tests.
$this
->containerBuild(drupal_container());
// Make sure it survives kernel rebuilds.
$GLOBALS['conf']['container_bundles'][] = 'Drupal\\simpletest\\TestBundle';
\Drupal::state()
->set('system.module.files', $this->moduleFiles);
\Drupal::state()
->set('system.theme.files', $this->themeFiles);
\Drupal::state()
->set('system.theme.data', $this->themeData);
// Bootstrap the kernel.
// No need to dump it; this test runs in-memory.
$this->kernel = new DrupalKernel('unit_testing', TRUE, drupal_classloader(), FALSE);
$this->kernel
->boot();
// Collect and set a fixed module list.
$class = get_class($this);
$modules = array();
while ($class) {
if (property_exists($class, 'modules')) {
// Only add the modules, if the $modules property was not inherited.
$rp = new \ReflectionProperty($class, 'modules');
if ($rp->class == $class) {
$modules[$class] = $class::$modules;
}
}
$class = get_parent_class($class);
}
// Modules have been collected in reverse class hierarchy order; modules
// defined by base classes should be sorted first. Then, merge the results
// together.
$modules = array_reverse($modules);
$modules = call_user_func_array('array_merge_recursive', $modules);
$this
->enableModules($modules, FALSE);
// In order to use theme functions default theme config needs to exist.
config('system.theme')
->set('default', 'stark');
}