use Drupal\Core\Test\TestDatabase;
use Drupal\simpletest\TestDiscovery;
use Drupal\Tests\Listeners\SimpletestUiPrinter;
+use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\PhpExecutableFinder;
use Drupal\Core\Test\TestStatus;
*
* @return string
* The path to core's phpunit.xml.dist configuration file.
+ *
+ * @deprecated in Drupal 8.4.x for removal before Drupal 9.0.0. PHPUnit test
+ * runners should change directory into core/ and then run the phpunit tool.
+ * See simpletest_phpunit_run_command() for an example.
+ *
+ * @see simpletest_phpunit_run_command()
*/
function simpletest_phpunit_configuration_filepath() {
+ @trigger_error('The ' . __FUNCTION__ . ' function is deprecated since version 8.4.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
return \Drupal::root() . '/core/phpunit.xml.dist';
}
}
else {
// Double escape namespaces so they'll work in a regexp.
- $escaped_test_classnames = array_map(function($class) {
+ $escaped_test_classnames = array_map(function ($class) {
return addslashes($class);
}, $unescaped_test_classnames);
// exec in a subshell so that the environment is isolated when running tests
// via the simpletest UI.
- $ret = exec(join($command, " "), $output, $status);
+ $ret = exec(implode(" ", $command), $output, $status);
chdir($old_cwd);
putenv('SIMPLETEST_DB=');
// The file in Composer's bin dir is a *nix link, which does not work when
// extracted from a tarball and generally not on Windows.
- $command = $vendor_dir . '/phpunit/phpunit/phpunit';
+ $command = escapeshellarg($vendor_dir . '/phpunit/phpunit/phpunit');
if (substr(PHP_OS, 0, 3) == 'WIN') {
// On Windows it is necessary to run the script using the PHP executable.
$php_executable_finder = new PhpExecutableFinder();
$php = $php_executable_finder->find();
- $command = $php . ' -f ' . escapeshellarg($command) . ' --';
+ $command = $php . ' -f ' . $command . ' --';
}
return $command;
}
* Implements callback_batch_operation().
*/
function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
- simpletest_classloader_register();
+ \Drupal::service('test_discovery')->registerTestNamespaces();
// Get working values.
if (!isset($context['sandbox']['max'])) {
// First iteration: initialize working values.
// Perform the next test.
$test_class = array_shift($test_list);
- if (is_subclass_of($test_class, \PHPUnit_Framework_TestCase::class)) {
+ if (is_subclass_of($test_class, TestCase::class)) {
$phpunit_results = simpletest_run_phpunit_tests($test_id, [$test_class]);
simpletest_process_phpunit_results($phpunit_results);
$test_results[$test_class] = simpletest_summarize_phpunit_result($phpunit_results)[$test_class];
'#theme' => 'simpletest_result_summary',
'#label' => t($test_results[$class]['#name'] . ':'),
];
- array_unshift($items, drupal_render($class_test_result));
+ array_unshift($items, \Drupal::service('renderer')->render($class_test_result));
}
$context['message'] = t('Processed test @num of @max - %test.', ['%test' => $info['name'], '@num' => $max - $size, '@max' => $max]);
$overall_results = $test_results + [
'#theme' => 'simpletest_result_summary',
'#label' => t('Overall results:'),
];
- $context['message'] .= drupal_render($overall_results);
+ $context['message'] .= \Drupal::service('renderer')->render($overall_results);
$item_list = [
'#theme' => 'item_list',
'#items' => $items,
];
- $context['message'] .= drupal_render($item_list);
+ $context['message'] .= \Drupal::service('renderer')->render($item_list);
// Save working values for the next iteration.
$context['sandbox']['tests'] = $test_list;
*/
function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
if ($success) {
- drupal_set_message(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed]));
+ \Drupal::messenger()->addStatus(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed]));
}
else {
// Use the test_id passed as a parameter to _simpletest_batch_operation().
list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
simpletest_log_read($test_id, $last_prefix, $last_test_class);
- drupal_set_message(t('The test run did not successfully finish.'), 'error');
- drupal_set_message(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning');
+ \Drupal::messenger()->addError(t('The test run did not successfully finish.'));
+ \Drupal::messenger()->addWarning(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'));
}
\Drupal::moduleHandler()->invokeAll('test_group_finished');
}
* instead.
*/
function simpletest_test_get_all($extension = NULL, array $types = []) {
+ @trigger_error('The ' . __FUNCTION__ . ' function is deprecated in version 8.3.x and will be removed in 9.0.0. Use \Drupal::service(\'test_discovery\')->getTestClasses($extension, $types) instead.', E_USER_DEPRECATED);
return \Drupal::service('test_discovery')->getTestClasses($extension, $types);
}
* \Drupal::service('test_discovery')->registerTestNamespaces() instead.
*/
function simpletest_classloader_register() {
+ @trigger_error('The ' . __FUNCTION__ . ' function is deprecated in version 8.3.x and will be removed in 9.0.0. Use \Drupal::service(\'test_discovery\')->registerTestNamespaces() instead.', E_USER_DEPRECATED);
\Drupal::service('test_discovery')->registerTestNamespaces();
}
simpletest_clean_temporary_directories();
if (\Drupal::config('simpletest.settings')->get('clear_results')) {
$count = simpletest_clean_results_table();
- drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.'));
+ \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.'));
}
else {
- drupal_set_message(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
+ \Drupal::messenger()->addWarning(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
}
-
- // Detect test classes that have been added, renamed or deleted.
- \Drupal::cache()->delete('simpletest');
- \Drupal::cache()->delete('simpletest_phpunit');
}
/**
* Removes prefixed tables from the database from crashed tests.
*/
function simpletest_clean_database() {
+ $schema = Database::getConnection()->schema();
$tables = db_find_tables('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)) {
- db_drop_table($matches[0]);
+ $schema->dropTable($matches[0]);
$count++;
}
}
if ($count > 0) {
- drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
+ \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
}
else {
- drupal_set_message(t('No leftover tables to remove.'));
+ \Drupal::messenger()->addStatus(t('No leftover tables to remove.'));
}
}
}
if ($count > 0) {
- drupal_set_message(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
+ \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
}
else {
- drupal_set_message(t('No temporary directories to remove.'));
+ \Drupal::messenger()->addStatus(t('No temporary directories to remove.'));
}
}
* @param $phpunit_xml_file
* Path to the PHPUnit XML file.
*
- * @return array[]
+ * @return array[]|null
* The results as array of rows in a format that can be inserted into
- * {simpletest}.
+ * {simpletest}. If the phpunit_xml_file does not have any contents then the
+ * function will return NULL.
*/
function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
$contents = @file_get_contents($phpunit_xml_file);
*
* @param \SimpleXMLElement $element
* The PHPUnit xml to search for test cases.
- * @param \SimpleXMLElement $suite
+ * @param \SimpleXMLElement $parent
* (Optional) The parent of the current element. Defaults to NULL.
*
* @return array
$attributes = $testcase->attributes();
+ $function = $attributes->class . '->' . $attributes->name . '()';
$record = [
'test_id' => $test_id,
'test_class' => (string) $attributes->class,
'message' => $message,
// @todo: Check on the proper values for this.
'message_group' => 'Other',
- 'function' => $attributes->class . '->' . $attributes->name . '()',
+ 'function' => $function,
'line' => $attributes->line ?: 0,
- 'file' => $attributes->file,
+ // There are situations when the file will not be present because a PHPUnit
+ // @requires has caused a test to be skipped.
+ 'file' => $attributes->file ?: $function,
];
return $record;
}