trait DrupalTestCaseTrait

Same name and namespace in other branches
  1. main 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 22

Namespace

Drupal\Tests
View source
trait DrupalTestCaseTrait {
  
  /**
   * The Drupal root directory.
   */
  protected string $root;
  
  /**
   * Supports skipping tests with the Skip attribute.
   *
   * This is run with a high priority to prevent any setUp() functions from
   * executing, avoiding any Drupal bootstrap.
   *
   * @internal
   */
  final protected function skipTestWithAttribute() : void {
    $reflection = new \ReflectionMethod(static::class, $this->name());
    $attribute = $reflection->getAttributes(Skip::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? NULL;
    if ($attribute) {
      Assert::markTestSkipped($attribute->newInstance()->message);
    }
  }
  
  /**
   * Ensure that the $root property is set initially.
   *
   * This is run with a high priority since other test setup code that runs in
   * #[Before] hooks or setUp() requires access to $root.
   *
   * @internal
   */
  final protected function setUpRoot() : void {
    if (isset($this->root)) {
      throw new \LogicException("setUpRoot should be called exactly once by PHPUnit's Before test hook and root overrides should happen after.");
    }
    $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
  }
  
  /**
   * Returns the Drupal root directory.
   *
   * @return string
   *   The Drupal root directory.
   *
   * @deprecated in drupal:11.4.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:11.4.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 legacy SYMFONY_DEPRECATIONS_HELPER env variable is not used.
   */
  public function checkLegacySymfonyDeprecationHelperEnvVariable() : void {
    if (getenv('SYMFONY_DEPRECATIONS_HELPER') !== FALSE) {
      @trigger_error("Using the SYMFONY_DEPRECATIONS_HELPER environment variable to configure test runs is deprecated in drupal:11.5.0 and is removed from drupal:12.0.0. See https://www.drupal.org/node/3594014", E_USER_DEPRECATED);
    }
  }
  
  /**
   * 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 (DeprecationHandlerConfiguration::instance()->projectIgnoresEnabled && !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()));
    }
  }
  
  /**
   * Expects an exactly matching exception message.
   *
   * Forward compatibility for PHPUnit 13.
   *
   * @param string $message
   *   The expected exception message.
   */
  protected function expectExceptionMessageIs(string $message) : void {
    $this->expectExceptionMessage($message);
  }
  
  /**
   * Expects an exception message containing a specified string.
   *
   * Forward compatibility for PHPUnit 13.
   *
   * @param string $message
   *   The expected exception message.
   */
  protected function expectExceptionMessageIsOrContains(string $message) : void {
    $this->expectExceptionMessage($message);
  }

}

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::checkLegacySymfonyDeprecationHelperEnvVariable public function Checks legacy SYMFONY_DEPRECATIONS_HELPER env variable is not used.
DrupalTestCaseTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
DrupalTestCaseTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.
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.
DrupalTestCaseTrait::setUpRoot final protected function Ensure that the $root property is set initially.
DrupalTestCaseTrait::skipTestWithAttribute final protected function Supports skipping tests with the Skip attribute.

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