TestKernel.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Test/TestKernel.php
  2. 8.9.x core/lib/Drupal/Core/Test/TestKernel.php
  3. 10 core/lib/Drupal/Core/Test/TestKernel.php

Namespace

Drupal\Core\Test

File

core/lib/Drupal/Core/Test/TestKernel.php

View source
<?php

namespace Drupal\Core\Test;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Component\DependencyInjection\ReverseContainer;
use Drupal\Core\DrupalKernel;

/**
 * Kernel that is only used by mock front controllers.
 */
class TestKernel extends DrupalKernel {
    
    /**
     * {@inheritdoc}
     */
    public function __construct($environment, $class_loader, $allow_dumping = TRUE) {
        // Exit if we should be in a test environment but aren't.
        if (!drupal_valid_test_ua()) {
            header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
            exit;
        }
        parent::__construct($environment, $class_loader, $allow_dumping);
    }
    
    /**
     * Sets a container with a kernel service on the Drupal class.
     *
     * @return \Drupal\Component\DependencyInjection\ContainerInterface
     *   A container with the kernel service set.
     */
    public static function setContainerWithKernel() {
        $container = new ContainerBuilder();
        $kernel = new DrupalKernel('test', NULL);
        // Objects of the same type will have access to each others private and
        // protected members even though they are not the same instances. This is
        // because the implementation specific details are already known when
        // inside those objects.
        $kernel->container = $container;
        $container->set('kernel', $kernel);
        $container->set(ReverseContainer::class, new ReverseContainer($container));
        \Drupal::setContainer($container);
        return $container;
    }

}

Classes

Title Deprecated Summary
TestKernel Kernel that is only used by mock front controllers.

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