You are here

protected function BrowserTestBase::getTestMethodCaller in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/BrowserTestBase.php \Drupal\Tests\BrowserTestBase::getTestMethodCaller()
  2. 9 core/tests/Drupal/Tests/BrowserTestBase.php \Drupal\Tests\BrowserTestBase::getTestMethodCaller()

Retrieves the current calling line in the class under test.

Return value

array An associative array with keys 'file', 'line' and 'function'.

Overrides BrowserHtmlDebugTrait::getTestMethodCaller

2 calls to BrowserTestBase::getTestMethodCaller()
GetTestMethodCallerExtendsTest::testGetTestMethodCallerChildClass in core/tests/Drupal/FunctionalTests/GetTestMethodCallerExtendsTest.php
A test method that is not present in the parent class.
GetTestMethodCallerTest::testGetTestMethodCaller in core/tests/Drupal/FunctionalTests/GetTestMethodCallerTest.php
Tests BrowserTestBase::getTestMethodCaller().

File

core/tests/Drupal/Tests/BrowserTestBase.php, line 645

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\Tests

Code

protected function getTestMethodCaller() {
  $backtrace = debug_backtrace();

  // Find the test class that has the test method.
  while ($caller = Error::getLastCaller($backtrace)) {

    // If we match PHPUnit's TestCase::runTest, then the previously processed
    // caller entry is where our test method sits.
    if (isset($last_caller) && isset($caller['function']) && $caller['function'] === 'PHPUnit\\Framework\\TestCase->runTest()') {

      // Return the last caller since that has to be the test class.
      $caller = $last_caller;
      break;
    }

    // If the test method is implemented by a test class's parent then the
    // class name of $this will not be part of the backtrace.
    // In that case we process the backtrace until the caller is not a
    // subclass of $this and return the previous caller.
    if (isset($last_caller) && (!isset($caller['class']) || !is_subclass_of($this, $caller['class']))) {

      // Return the last caller since that has to be the test class.
      $caller = $last_caller;
      break;
    }
    if (isset($caller['class']) && $caller['class'] === static::class) {
      break;
    }

    // Otherwise we have not reached our test class yet: save the last caller
    // and remove an element from to backtrace to process the next call.
    $last_caller = $caller;
    array_shift($backtrace);
  }
  return $caller;
}