TestServiceProvider.php in Drupal 8
File
core/tests/Drupal/KernelTests/TestServiceProvider.php
View source
<?php
namespace Drupal\KernelTests;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Symfony\Component\DependencyInjection\Definition;
class TestServiceProvider implements ServiceProviderInterface, ServiceModifierInterface {
public static $currentTest;
public function register(ContainerBuilder $container) {
if (static::$currentTest && method_exists(static::$currentTest, 'containerBuild')) {
static::$currentTest
->containerBuild($container);
}
}
public function alter(ContainerBuilder $container) {
if (static::$currentTest instanceof KernelTestBase) {
static::addRouteProvider($container);
}
}
public static function addRouteProvider(ContainerBuilder $container) {
$route_provider_service_name = 'router.route_provider';
$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));
}
}