trait DrupalTestCaseTrait

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/DrupalTestCaseTrait.php \Drupal\Tests\DrupalTestCaseTrait

Provides methods common across all Drupal abstract base test classes.

This trait is meant to be used only by test classes.

Hierarchy

3 files declare their use of DrupalTestCaseTrait
BuildTestBase.php in core/tests/Drupal/BuildTests/Framework/BuildTestBase.php
KernelTestBase.php in core/tests/Drupal/KernelTests/KernelTestBase.php
TestSetupTraitTest.php in core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php

File

core/tests/Drupal/Tests/DrupalTestCaseTrait.php, line 21

Namespace

Drupal\Tests
View source
trait DrupalTestCaseTrait {
  
  /**
   * The Drupal root directory.
   */
  protected string $root {
    get {
      if (!isset($this->root)) {
        $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
      }
      return $this->root;
    }
  }
  
  /**
   * Returns the Drupal root directory.
   *
   * @return string
   *   The Drupal root directory.
   *
   * @deprecated in drupal:12.0.0 and is removed from drupal:13.0.0. Access
   *   $this->root directly.
   *
   * @see https://www.drupal.org/node/3574112
   */
  protected static function getDrupalRoot() : string {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:12.0.0 and is removed from drupal:13.0.0. Access $this->root directly. See https://www.drupal.org/node/3574112', E_USER_DEPRECATED);
    return dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
  }
  
  /**
   * Registers the dumper CLI handler when the DebugDump extension is enabled.
   */
  public static function setDebugDumpHandler() : void {
    if (DebugDump::isEnabled()) {
      VarDumper::setHandler(DebugDump::class . '::cliHandler');
    }
  }
  
  /**
   * Checks the test error handler after test execution.
   */
  public function checkErrorHandlerOnTearDown() : void {
    // We expect that the current error handler is the one set during the
    // PHPUnit bootstrap. If not, the error handler was changed during the test
    // execution but not properly restored during ::tearDown().
    if (DeprecationHandler::isEnabled() && !get_error_handler() instanceof BootstrapErrorHandler) {
      throw new \RuntimeException(sprintf('%s registered its own error handler without restoring the previous one before or during tear down. This can cause unpredictable test results. Ensure the test cleans up after itself.', $this->name()));
    }
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::getDrupalRoot Deprecated protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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