You are here

class TestServiceProvider in Drupal 8

Same name in this branch
  1. 8 core/tests/Drupal/KernelTests/TestServiceProvider.php \Drupal\KernelTests\TestServiceProvider
  2. 8 core/modules/simpletest/src/TestServiceProvider.php \Drupal\simpletest\TestServiceProvider

Provides special routing services for tests.

Hierarchy

Expanded class hierarchy of TestServiceProvider

2 files declare their use of TestServiceProvider
KernelTestBase.php in core/modules/simpletest/src/KernelTestBase.php
TestServiceProvider.php in core/modules/simpletest/src/TestServiceProvider.php

File

core/tests/Drupal/KernelTests/TestServiceProvider.php, line 13

Namespace

Drupal\KernelTests
View source
class TestServiceProvider implements ServiceProviderInterface, ServiceModifierInterface {

  /**
   * @var \Drupal\simpletest\TestBase
   */
  public static $currentTest;

  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) {
    if (static::$currentTest && method_exists(static::$currentTest, 'containerBuild')) {
      static::$currentTest
        ->containerBuild($container);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
    if (static::$currentTest instanceof KernelTestBase) {
      static::addRouteProvider($container);
    }
  }

  /**
   * Add the on demand rebuild route provider service.
   *
   * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
   */
  public static function addRouteProvider(ContainerBuilder $container) {
    $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("simpletest.{$route_provider_service_name}", $definition);
    $container
      ->setDefinition($id, new Definition(RouteProvider::class));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestServiceProvider::$currentTest public static property
TestServiceProvider::addRouteProvider public static function Add the on demand rebuild route provider service.
TestServiceProvider::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter
TestServiceProvider::register public function Registers services to the container. Overrides ServiceProviderInterface::register