You are here

function _simpletest_batch_operation in Drupal 8

Same name and namespace in other branches
  1. 7 modules/simpletest/simpletest.module \_simpletest_batch_operation()

Implements callback_batch_operation().

1 call to _simpletest_batch_operation()
TestDeprecatedTestHooks::testHookTestFinished in core/modules/simpletest/tests/src/Kernel/TestDeprecatedTestHooks.php
@expectedDeprecation The deprecated hook hook_test_finished() is implemented in these functions: simpletest_deprecation_test_test_finished(). Convert your test to a PHPUnit-based one and implement test listeners. See https://www.drupal.org/node/2934242
1 string reference to '_simpletest_batch_operation'
simpletest_run_tests in core/modules/simpletest/simpletest.module
Runs tests.

File

core/modules/simpletest/simpletest.module, line 328
Provides testing functionality.

Code

function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
  \Drupal::service('test_discovery')
    ->registerTestNamespaces();

  // Get working values.
  if (!isset($context['sandbox']['max'])) {

    // First iteration: initialize working values.
    $test_list = $test_list_init;
    $context['sandbox']['max'] = count($test_list);
    $test_results = [
      '#pass' => 0,
      '#fail' => 0,
      '#exception' => 0,
      '#debug' => 0,
    ];
  }
  else {

    // Nth iteration: get the current values where we last stored them.
    $test_list = $context['sandbox']['tests'];
    $test_results = $context['sandbox']['test_results'];
  }
  $max = $context['sandbox']['max'];

  // Perform the next test.
  $test_class = array_shift($test_list);
  if (is_subclass_of($test_class, TestCase::class)) {
    $runner = PhpUnitTestRunner::create(\Drupal::getContainer());
    $phpunit_results = $runner
      ->runTests($test_id, [
      $test_class,
    ]);
    TestDatabase::processPhpUnitResults($phpunit_results);
    $test_results[$test_class] = simpletest_summarize_phpunit_result($phpunit_results)[$test_class];
  }
  else {
    $test = new $test_class($test_id);
    $test
      ->run();
    \Drupal::moduleHandler()
      ->invokeAllDeprecated('Convert your test to a PHPUnit-based one and implement test listeners. See https://www.drupal.org/node/2934242', 'test_finished', [
      $test->results,
    ]);
    $test_results[$test_class] = $test->results;
  }
  $size = count($test_list);
  $info = TestDiscovery::getTestInfo($test_class);

  // Gather results and compose the report.
  foreach ($test_results[$test_class] as $key => $value) {
    $test_results[$key] += $value;
  }
  $test_results[$test_class]['#name'] = $info['name'];
  $items = [];
  foreach (Element::children($test_results) as $class) {
    $class_test_result = $test_results[$class] + [
      '#theme' => 'simpletest_result_summary',
      '#label' => t($test_results[$class]['#name'] . ':'),
    ];
    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::service('renderer')
    ->render($overall_results);
  $item_list = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  $context['message'] .= \Drupal::service('renderer')
    ->render($item_list);

  // Save working values for the next iteration.
  $context['sandbox']['tests'] = $test_list;
  $context['sandbox']['test_results'] = $test_results;

  // The test_id is the only thing we need to save for the report page.
  $context['results']['test_id'] = $test_id;

  // Multistep processing: report progress.
  $context['finished'] = 1 - $size / $max;
}