public function DrupalUnitTestBase::containerBuild

Sets up the base service container for this test.

Extend this method in your test to register additional service overrides that need to persist a DrupalKernel reboot. This method is called whenever the kernel is rebuilt.

See also

DrupalUnitTestBase::setUp()

DrupalUnitTestBase::enableModules()

DrupalUnitTestBase::disableModules()

2 calls to DrupalUnitTestBase::containerBuild()
DrupalUnitTestBase::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php
Sets up Drupal unit test environment.
TreeAccessTest::containerBuild in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/TreeAccessTest.php
Overrides \Drupal\simpletest\DrupalUnitTestBase::containerBuild().
1 method overrides DrupalUnitTestBase::containerBuild()
TreeAccessTest::containerBuild in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/TreeAccessTest.php
Overrides \Drupal\simpletest\DrupalUnitTestBase::containerBuild().

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php, line 143
Contains Drupal\simpletest\DrupalUnitTestBase.

Class

DrupalUnitTestBase
Base test case class for Drupal unit tests.

Namespace

Drupal\simpletest

Code

public function containerBuild(ContainerBuilder $container) {
  global $conf;

  // Keep the container object around for tests.
  $this->container = $container;
  $container
    ->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend');
  $this
    ->settingsSet('cache', array(
    'default' => 'cache.backend.memory',
  ));
  $container
    ->register('config.storage', 'Drupal\\Core\\Config\\FileStorage')
    ->addArgument($this->configDirectories[CONFIG_ACTIVE_DIRECTORY]);
  $conf['keyvalue_default'] = 'keyvalue.memory';
  $container
    ->set('keyvalue.memory', $this->keyValueFactory);
  if (!$container
    ->has('keyvalue')) {

    // TestBase::setUp puts a completely empty container in
    // drupal_container() which is somewhat the mirror of the empty
    // environment being set up. Unit tests need not to waste time with
    // getting a container set up for them. Drupal Unit Tests might just get
    // away with a simple container holding the absolute bare minimum. When
    // a kernel is overridden then there's no need to re-register the keyvalue
    // service but when a test is happy with the superminimal container put
    // together here, it still might a keyvalue storage for anything (for
    // eg. module_enable) using \Drupal::state() -- that's why a memory
    // service was added in the first place.
    $container
      ->register('keyvalue', 'Drupal\\Core\\KeyValueStore\\KeyValueFactory')
      ->addArgument(new Reference('service_container'));
    $container
      ->register('state', 'Drupal\\Core\\KeyValueStore\\KeyValueStoreInterface')
      ->setFactoryService(new Reference('keyvalue'))
      ->setFactoryMethod('get')
      ->addArgument('state');
  }
  if ($container
    ->hasDefinition('path_processor_alias')) {

    // Prevent the alias-based path processor, which requires a url_alias db
    // table, from being registered to the path processor manager. We do this
    // by removing the tags that the compiler pass looks for. This means the
    // url generator can safely be used within DUTB tests.
    $definition = $container
      ->getDefinition('path_processor_alias');
    $definition
      ->clearTag('path_processor_inbound')
      ->clearTag('path_processor_outbound');
  }
}