class EnvironmentCleaner
Same name in other branches
- 9 core/lib/Drupal/Core/Test/EnvironmentCleaner.php \Drupal\Core\Test\EnvironmentCleaner
- 8.9.x core/lib/Drupal/Core/Test/EnvironmentCleaner.php \Drupal\Core\Test\EnvironmentCleaner
- 11.x core/lib/Drupal/Core/Test/EnvironmentCleaner.php \Drupal\Core\Test\EnvironmentCleaner
Helper class for cleaning test environments.
@internal
Hierarchy
- class \Drupal\Core\Test\EnvironmentCleaner implements \Drupal\Core\Test\EnvironmentCleanerInterface
Expanded class hierarchy of EnvironmentCleaner
1 file declares its use of EnvironmentCleaner
- EnvironmentCleanerTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Test/ EnvironmentCleanerTest.php
File
-
core/
lib/ Drupal/ Core/ Test/ EnvironmentCleaner.php, line 14
Namespace
Drupal\Core\TestView source
class EnvironmentCleaner implements EnvironmentCleanerInterface {
/**
* Constructs a test environment cleaner.
*
* @param string $root
* The path to the root of the Drupal installation.
* @param \Drupal\Core\Database\Connection $testDatabase
* Connection to the database against which tests were run.
* @param \Drupal\Core\Test\TestRunResultsStorageInterface $testRunResultsStorage
* The test run results storage.
* @param \Symfony\Component\Console\Output\OutputInterface $output
* A Symfony console output object.
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* Drupal's file_system service.
*/
public function __construct(string $root, Connection $testDatabase, TestRunResultsStorageInterface $testRunResultsStorage, OutputInterface $output, FileSystemInterface $fileSystem) {
}
/**
* {@inheritdoc}
*/
public function cleanEnvironment(bool $clear_results = TRUE, bool $clear_temp_directories = TRUE, bool $clear_database = TRUE) : void {
$count = 0;
if ($clear_database) {
$this->doCleanDatabase();
}
if ($clear_temp_directories) {
$this->doCleanTemporaryDirectories();
}
if ($clear_results) {
$count = $this->cleanResults();
$this->output
->write('Test results removed: ' . $count);
}
else {
$this->output
->write('Test results were not removed.');
}
}
/**
* {@inheritdoc}
*/
public function cleanDatabase() : void {
$count = $this->doCleanDatabase();
if ($count > 0) {
$this->output
->write('Leftover tables removed: ' . $count);
}
else {
$this->output
->write('No leftover tables to remove.');
}
}
/**
* Performs the fixture database cleanup.
*
* @return int
* The number of tables that were removed.
*/
protected function doCleanDatabase() : int {
/** @var \Drupal\Core\Database\Schema $schema */
$schema = $this->testDatabase
->schema();
$tables = $schema->findTables('test%');
$count = 0;
foreach ($tables as $table) {
// Only drop tables which begin wih 'test' followed by digits, for example,
// {test12345678node__body}.
if (preg_match('/^test\\d+.*/', $table, $matches)) {
$schema->dropTable($matches[0]);
$count++;
}
}
return $count;
}
/**
* {@inheritdoc}
*/
public function cleanTemporaryDirectories() : void {
$count = $this->doCleanTemporaryDirectories();
if ($count > 0) {
$this->output
->write('Temporary directories removed: ' . $count);
}
else {
$this->output
->write('No temporary directories to remove.');
}
}
/**
* Performs the cleanup of temporary test directories.
*
* @return int
* The count of temporary directories removed.
*/
protected function doCleanTemporaryDirectories() : int {
$count = 0;
$simpletest_dir = $this->root . '/sites/simpletest';
if (is_dir($simpletest_dir)) {
$files = scandir($simpletest_dir);
foreach ($files as $file) {
if ($file[0] != '.') {
$path = $simpletest_dir . '/' . $file;
$this->fileSystem
->deleteRecursive($path, function ($any_path) {
@chmod($any_path, 0700);
});
$count++;
}
}
}
return $count;
}
/**
* {@inheritdoc}
*/
public function cleanResults(?TestRun $test_run = NULL) : int {
return $test_run ? $test_run->removeResults() : $this->testRunResultsStorage
->cleanUp();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
EnvironmentCleaner::cleanDatabase | public | function | Remove database entries left over in the fixture database. | Overrides EnvironmentCleanerInterface::cleanDatabase |
EnvironmentCleaner::cleanEnvironment | public | function | Removes all test-related database tables and directories. | Overrides EnvironmentCleanerInterface::cleanEnvironment |
EnvironmentCleaner::cleanResults | public | function | Clears test results from the results storage. | Overrides EnvironmentCleanerInterface::cleanResults |
EnvironmentCleaner::cleanTemporaryDirectories | public | function | Finds all leftover fixture site directories and removes them. | Overrides EnvironmentCleanerInterface::cleanTemporaryDirectories |
EnvironmentCleaner::doCleanDatabase | protected | function | Performs the fixture database cleanup. | |
EnvironmentCleaner::doCleanTemporaryDirectories | protected | function | Performs the cleanup of temporary test directories. | |
EnvironmentCleaner::__construct | public | function | Constructs a test environment cleaner. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.