class DrupalTestCase

Same name and namespace in other branches
  1. 7.x modules/simpletest/drupal_web_test_case.php \DrupalTestCase

Provides the root base class for all Drupal core tests.

Hierarchy

Expanded class hierarchy of DrupalTestCase

Related topics

3 files declare their use of DrupalTestCase
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/DrupalTestCase.php, line 16

Namespace

Drupal\Tests
View source
abstract class DrupalTestCase extends TestCase {
  use 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) {
      static::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 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);
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCase::$root protected property The Drupal root directory.
DrupalTestCase::getDrupalRoot Deprecated protected function Returns the Drupal root directory. 1
DrupalTestCase::setUpRoot final protected function Ensure that the $root property is set initially.
DrupalTestCase::skipTestWithAttribute final protected function Supports skipping tests with the Skip attribute.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
ForwardCompatibilityTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
ForwardCompatibilityTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.

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