class TestKernel

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Test/TestKernel.php \Drupal\Core\Test\TestKernel
  2. 10 core/lib/Drupal/Core/Test/TestKernel.php \Drupal\Core\Test\TestKernel
  3. 8.9.x core/lib/Drupal/Core/Test/TestKernel.php \Drupal\Core\Test\TestKernel

Kernel to mock requests to test simpletest.

Hierarchy

Expanded class hierarchy of TestKernel

7 files declare their use of TestKernel
ConfigEntityBaseUnitTest.php in core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
Contains \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest.
ContextHandlerTest.php in core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php
Contains \Drupal\Tests\Core\Plugin\ContextHandlerTest.
DependencySerializationTest.php in core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php
Contains \Drupal\Tests\Core\DependencyInjection\DependencySerializationTest.
DrupalKernelTest.php in core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php
http.php in core/modules/system/tests/http.php
Fake an HTTP request, for use during testing.

... See full list

File

core/lib/Drupal/Core/Test/TestKernel.php, line 12

Namespace

Drupal\Core\Test
View source
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;
  }

}

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