You are here

public function TestDatabase::__construct in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Test/TestDatabase.php \Drupal\Core\Test\TestDatabase::__construct()

TestDatabase constructor.

Parameters

string|null $db_prefix: If not provided a new test lock is generated.

bool $create_lock: (optional) Whether or not to create a lock file. Defaults to FALSE. If the environment variable RUN_TESTS_CONCURRENCY is greater than 1 it will be overridden to TRUE regardless of its initial value.

Throws

\InvalidArgumentException Thrown when $db_prefix does not match the regular expression.

File

core/lib/Drupal/Core/Test/TestDatabase.php, line 73

Class

TestDatabase
Provides helper methods for interacting with the fixture database.

Namespace

Drupal\Core\Test

Code

public function __construct($db_prefix = NULL, $create_lock = FALSE) {
  if ($db_prefix === NULL) {
    $this->lockId = $this
      ->getTestLock($create_lock);
    $this->databasePrefix = 'test' . $this->lockId;
  }
  else {
    $this->databasePrefix = $db_prefix;

    // It is possible that we're running a test inside a test. In which case
    // $db_prefix will be something like test12345678test90123456 and the
    // generated lock ID for the running test method would be 90123456.
    preg_match('/test(\\d+)$/', $db_prefix, $matches);
    if (!isset($matches[1])) {
      throw new \InvalidArgumentException("Invalid database prefix: {$db_prefix}");
    }
    $this->lockId = $matches[1];
  }
}