You are here

public function PhpUnitTestRunner::runCommand in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Test/PhpUnitTestRunner.php \Drupal\Core\Test\PhpUnitTestRunner::runCommand()

Executes the PHPUnit command.

@internal

Parameters

string[] $unescaped_test_classnames: An array of test class names, including full namespaces, to be passed as a regular expression to PHPUnit's --filter option.

string $phpunit_file: A filepath to use for PHPUnit's --log-junit option.

int $status: (optional) The exit status code of the PHPUnit process will be assigned to this variable.

string[] $output: (optional) The output by running the phpunit command. If provided, this array will contain the lines output by the command.

Return value

string The results as returned by exec().

1 call to PhpUnitTestRunner::runCommand()
PhpUnitTestRunner::runTests in core/lib/Drupal/Core/Test/PhpUnitTestRunner.php
Executes PHPUnit tests and returns the results of the run.

File

core/lib/Drupal/Core/Test/PhpUnitTestRunner.php, line 128

Class

PhpUnitTestRunner
Run PHPUnit-based tests.

Namespace

Drupal\Core\Test

Code

public function runCommand(array $unescaped_test_classnames, $phpunit_file, &$status = NULL, &$output = NULL) {
  global $base_url;

  // Setup an environment variable containing the database connection so that
  // functional tests can connect to the database.
  putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl());

  // Setup an environment variable containing the base URL, if it is available.
  // This allows functional tests to browse the site under test. When running
  // tests via CLI, core/phpunit.xml.dist or core/scripts/run-tests.sh can set
  // this variable.
  if ($base_url) {
    putenv('SIMPLETEST_BASE_URL=' . $base_url);
    putenv('BROWSERTEST_OUTPUT_DIRECTORY=' . $this->workingDirectory);
  }
  $phpunit_bin = $this
    ->phpUnitCommand();
  $command = [
    $phpunit_bin,
    '--log-junit',
    escapeshellarg($phpunit_file),
    '--printer',
    escapeshellarg(SimpletestUiPrinter::class),
  ];

  // Optimized for running a single test.
  if (count($unescaped_test_classnames) == 1) {
    $class = new \ReflectionClass($unescaped_test_classnames[0]);
    $command[] = escapeshellarg($class
      ->getFileName());
  }
  else {

    // Double escape namespaces so they'll work in a regexp.
    $escaped_test_classnames = array_map(function ($class) {
      return addslashes($class);
    }, $unescaped_test_classnames);
    $filter_string = implode("|", $escaped_test_classnames);
    $command = array_merge($command, [
      '--filter',
      escapeshellarg($filter_string),
    ]);
  }

  // Need to change directories before running the command so that we can use
  // relative paths in the configuration file's exclusions.
  $old_cwd = getcwd();
  chdir($this->appRoot . "/core");

  // exec in a subshell so that the environment is isolated when running tests
  // via the simpletest UI.
  $ret = exec(implode(" ", $command), $output, $status);
  chdir($old_cwd);
  putenv('SIMPLETEST_DB=');
  if ($base_url) {
    putenv('SIMPLETEST_BASE_URL=');
    putenv('BROWSERTEST_OUTPUT_DIRECTORY=');
  }
  return $ret;
}