class TestDatabase (View source)

Provides helper methods for interacting with the fixture database.

Properties

protected int $lockId

A random number used to ensure that test fixtures are unique to each test method.

protected string $databasePrefix

The test database prefix.

Methods

static Connection
getConnection()

Returns the database connection to the site running Simpletest.

__construct(string|null $db_prefix = NULL, bool $create_lock = FALSE)

TestDatabase constructor.

string
getTestSitePath()

Gets the relative path to the test site directory.

string
getDatabasePrefix()

Gets the test database prefix.

int
getTestLock(bool $create_lock = FALSE)

Generates a unique lock ID for the test method.

bool
releaseLock()

Releases a lock.

static 
releaseAllTestLocks()

Releases all test locks.

string
getLockFile(int $lock_id)

Gets the lock file path.

static int
insertAssert(string $test_id, string $test_class, bool|string $status, string $message = '', string $group = 'Other', array $caller = [])

Store an assertion from outside the testing context.

static array
lastTestGet(int $test_id)

Get information about the last test that ran given a test ID.

bool
logRead(int $test_id, string $test_class)

Reads the error log and reports any errors as assertion failures.

static array
testingSchema()

Defines the database schema for run-tests.sh and simpletest module.

static 
processPhpUnitResults(array[] $phpunit_results)

Inserts the parsed PHPUnit results into {simpletest}.

Details

static Connection getConnection()

Returns the database connection to the site running Simpletest.

Return Value

Connection

The database connection to use for inserting assertions.

See also

\Drupal\simpletest\TestBase::prepareEnvironment()

__construct(string|null $db_prefix = NULL, bool $create_lock = FALSE)

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.

Exceptions

InvalidArgumentException

string getTestSitePath()

Gets the relative path to the test site directory.

Return Value

string

The relative path to the test site directory.

string getDatabasePrefix()

Gets the test database prefix.

Return Value

string

The test database prefix.

protected int getTestLock(bool $create_lock = FALSE)

Generates a unique lock ID for the test method.

Parameters

bool $create_lock

(optional) Whether or not to create a lock file. Defaults to FALSE.

Return Value

int

The unique lock ID for the test method.

bool releaseLock()

Releases a lock.

Return Value

bool

TRUE if successful, FALSE if not.

static releaseAllTestLocks()

Releases all test locks.

This should only be called once all the test fixtures have been cleaned up.

protected string getLockFile(int $lock_id)

Gets the lock file path.

Parameters

int $lock_id

The test method lock ID.

Return Value

string

A file path to the symbolic link that prevents the lock ID being re-used.

static int insertAssert(string $test_id, string $test_class, bool|string $status, string $message = '', string $group = 'Other', array $caller = [])

internal  
 

Store an assertion from outside the testing context.

This is useful for inserting assertions that can only be recorded after the test case has been destroyed, such as PHP fatal errors. The caller information is not automatically gathered since the caller is most likely inserting the assertion on behalf of other code. In all other respects the method behaves just like \Drupal\simpletest\TestBase::assert() in terms of storing the assertion.

Parameters

string $test_id

The test ID to which the assertion relates.

string $test_class

The test class to store an assertion for.

bool|string $status

A boolean or a string of 'pass' or 'fail'. TRUE means 'pass'.

string $message

The assertion message.

string $group

The assertion message group.

array $caller

The an array containing the keys 'file' and 'line' that represent the file and line number of that file that is responsible for the assertion.

Return Value

int

Message ID of the stored assertion.

static array lastTestGet(int $test_id)

internal  
 

Get information about the last test that ran given a test ID.

Parameters

int $test_id

The test ID to get the last test from.

Return Value

array

Associative array containing the last database prefix used and the last test class that ran.

bool logRead(int $test_id, string $test_class)

internal  
 

Reads the error log and reports any errors as assertion failures.

The errors in the log should only be fatal errors since any other errors will have been recorded by the error handler.

Parameters

int $test_id

The test ID to which the log relates.

string $test_class

The test class to which the log relates.

Return Value

bool

Whether any fatal errors were found.

static array testingSchema()

internal  
 

Defines the database schema for run-tests.sh and simpletest module.

Return Value

array

Array suitable for use in a hook_schema() implementation.

static processPhpUnitResults(array[] $phpunit_results)

internal  
 

Inserts the parsed PHPUnit results into {simpletest}.

Parameters

array[] $phpunit_results

An array of test results, as returned from \Drupal\Core\Test\JUnitConverter::xmlToRows(). These results are in a form suitable for inserting into the {simpletest} table of the test results database.