View source
<?php
namespace Drupal\TestSite\Commands;
use Drupal\Core\Database\Database;
use Drupal\Core\Test\TestDatabase;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class TestSiteTearDownCommand extends Command {
protected function configure() {
$this
->setName('tear-down')
->setDescription('Removes a test site added by the install command')
->setHelp('All the database tables and files will be removed.')
->addArgument('db-prefix', InputArgument::REQUIRED, 'The database prefix for the test site.')
->addOption('db-url', NULL, InputOption::VALUE_OPTIONAL, 'URL for database. Defaults to the environment variable SIMPLETEST_DB.', getenv('SIMPLETEST_DB'))
->addOption('keep-lock', NULL, InputOption::VALUE_NONE, 'Keeps the database prefix lock. Useful for ensuring test isolation when running concurrent tests.')
->addUsage('test12345678')
->addUsage('test12345678 --db-url "mysql://username:password@localhost/databasename#table_prefix"')
->addUsage('test12345678 --keep-lock');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$db_prefix = $input
->getArgument('db-prefix');
try {
$test_database = new TestDatabase($db_prefix);
} catch (\InvalidArgumentException $e) {
$io = new SymfonyStyle($input, $output);
$io
->getErrorStyle()
->error("Invalid database prefix: {$db_prefix}\n\nValid database prefixes match the regular expression '/test(\\d+)\$/'. For example, 'test12345678'.");
$output
->writeln(sprintf('<info>%s</info>', sprintf($this
->getSynopsis(), $this
->getName())), OutputInterface::VERBOSITY_QUIET);
return 1;
}
$db_url = $input
->getOption('db-url');
putenv("SIMPLETEST_DB={$db_url}");
$this
->tearDown($test_database, $db_url);
if (!$input
->getOption('keep-lock')) {
$test_database
->releaseLock();
}
$output
->writeln("<info>Successfully uninstalled {$db_prefix} test site</info>");
return 0;
}
protected function tearDown(TestDatabase $test_database, $db_url) {
$root = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
$database = Database::convertDbUrlToConnectionInfo($db_url, $root);
$database['prefix'] = [
'default' => $test_database
->getDatabasePrefix(),
];
Database::addConnectionInfo(__CLASS__, 'default', $database);
$schema = Database::getConnection('default', __CLASS__)
->schema();
$tables = $schema
->findTables('%');
array_walk($tables, [
$schema,
'dropTable',
]);
$this
->fileUnmanagedDeleteRecursive($root . DIRECTORY_SEPARATOR . $test_database
->getTestSitePath(), [
BrowserTestBase::class,
'filePreDeleteCallback',
]);
}
protected function fileUnmanagedDeleteRecursive($path, $callback = NULL) {
if (isset($callback)) {
call_user_func($callback, $path);
}
if (is_dir($path)) {
$dir = dir($path);
while (($entry = $dir
->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
$this
->fileUnmanagedDeleteRecursive($entry_path, $callback);
}
$dir
->close();
return rmdir($path);
}
return unlink($path);
}
}