function KernelTestBase::register

Registers test-specific services.

Extend this method in your test to register additional services. This method is called whenever the kernel is rebuilt.

Parameters

\Drupal\Core\DependencyInjection\ContainerBuilder $container: The service container to enhance.

Overrides ServiceProviderInterface::register

See also

\Drupal\KernelTests\KernelTestBase::bootKernel()

37 calls to KernelTestBase::register()
AdminAccountSwitcherTest::register in core/tests/Drupal/KernelTests/Core/DefaultContent/AdminAccountSwitcherTest.php
Registers test-specific services.
BlockConfigSyncTest::register in core/modules/block/tests/src/Kernel/BlockConfigSyncTest.php
Registers test-specific services.
CacheCollectorTest::register in core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
Registers test-specific services.
ConfigImporterMissingContentTest::register in core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php
Registers test-specific services.
CronQueueTest::register in core/modules/system/tests/src/Kernel/System/CronQueueTest.php
Registers test-specific services.

... See full list

37 methods override KernelTestBase::register()
AdminAccountSwitcherTest::register in core/tests/Drupal/KernelTests/Core/DefaultContent/AdminAccountSwitcherTest.php
Registers test-specific services.
BlockConfigSyncTest::register in core/modules/block/tests/src/Kernel/BlockConfigSyncTest.php
Registers test-specific services.
CacheCollectorTest::register in core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
Registers test-specific services.
ConfigImporterMissingContentTest::register in core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php
Registers test-specific services.
CronQueueTest::register in core/modules/system/tests/src/Kernel/System/CronQueueTest.php
Registers test-specific services.

... See full list

File

core/tests/Drupal/KernelTests/KernelTestBase.php, line 536

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\KernelTests

Code

public function register(ContainerBuilder $container) {
  // Keep the container object around for tests.
  $this->container = $container;
  $container->register('datetime.time', 'Drupal\\Component\\Datetime\\Time');
  $container->register('flood', 'Drupal\\Core\\Flood\\MemoryBackend')
    ->addArgument(new Reference('request_stack'));
  $container->register('lock', 'Drupal\\Core\\Lock\\NullLockBackend');
  // Explicitly configure all cache bins to use the memory backend.
  foreach (array_keys($container->findTaggedServiceIds('cache.bin')) as $id) {
    $definition = $container->getDefinition($id);
    $tags = $definition->getTags();
    $tags['cache.bin'][0]['default_backend'] = 'cache.backend.memory';
    $definition->setTags($tags);
  }
  // Disable the super user access policy so that we are sure our tests check
  // for the right permissions.
  if (!isset($this->usesSuperUserAccessPolicy)) {
    $test_file_name = (new \ReflectionClass($this))->getFileName();
    // @todo Decide in https://www.drupal.org/project/drupal/issues/3437926
    //   how to remove this fallback behavior.
    $this->usesSuperUserAccessPolicy = !str_starts_with($test_file_name, $this->root . DIRECTORY_SEPARATOR . 'core');
  }
  $container->setParameter('security.enable_super_user', $this->usesSuperUserAccessPolicy);
  // Use memory for key value storages to avoid database queries. Store the
  // key value factory on the test object so that key value storages persist
  // container rebuilds, otherwise all state data would vanish.
  if (!isset($this->keyValue)) {
    $this->keyValue = new KeyValueMemoryFactory();
  }
  $container->set('keyvalue', $this->keyValue);
  $container->getDefinition('keyvalue')
    ->setSynthetic(TRUE);
  // Set the default language on the minimal container.
  $container->setParameter('language.default_values', Language::$defaultValues);
  // Determine whether the test is a core test.
  $test_file_name = (new \ReflectionClass($this))->getFileName();
  // @todo Decide in https://www.drupal.org/project/drupal/issues/3395099 when/how to trigger deprecation errors or even failures for contrib modules.
  $is_core_test = str_starts_with($test_file_name, $this->root . DIRECTORY_SEPARATOR . 'core');
  if ($this->strictConfigSchema) {
    $container->register('testing.config_schema_checker', ConfigSchemaChecker::class)
      ->addArgument(new Reference('config.typed'))
      ->addArgument($this->getConfigSchemaExclusions())
      ->addArgument($is_core_test)
      ->addTag('event_subscriber');
  }
  // Add event subscriber to check that an entity schema is installed before
  // any field storages are created on the entity.
  $container->register('testing.field_storage_create_check', FieldStorageCreateCheckSubscriber::class)
    ->addArgument(new Reference('database'))
    ->addArgument(new Reference('entity_type.manager'))
    ->addArgument($is_core_test)
    ->addTag('event_subscriber');
  // Relax the password hashing cost in tests to avoid performance issues.
  if ($container->hasDefinition('password')) {
    $container->getDefinition('password')
      ->setArguments([
      PASSWORD_BCRYPT,
      [
        'cost' => 4,
      ],
    ]);
  }
  // Add the on demand rebuild route provider service.
  $route_provider_service_name = 'router.route_provider';
  // While $container->get() does a recursive resolve, getDefinition() does
  // not, so do it ourselves.
  $id = $route_provider_service_name;
  while ($container->hasAlias($id)) {
    $id = (string) $container->getAlias($id);
  }
  $definition = $container->getDefinition($id);
  $definition->clearTag('needs_destruction');
  $container->setDefinition("test.{$route_provider_service_name}", $definition);
  $route_provider_definition = new Definition(RouteProvider::class);
  $route_provider_definition->setPublic(TRUE);
  $container->setDefinition($id, $route_provider_definition);
  // Remove the stored configuration importer so if used again it will be
  // built with up-to-date services.
  $this->configImporter = NULL;
  // Allow kernel tests to register hooks.
  $definition = $container->register(static::class, static::class)
    ->setSynthetic(TRUE);
  $container->set(static::class, $this);
  $container->addCompilerPass(new KernelTestCompilerPass($definition), priority: -100);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.