diff --git a/core/lib/Drupal/Core/RunTests/Config.php b/core/lib/Drupal/Core/RunTests/Config.php index 8fb76a3..a0e7c31 100644 --- a/core/lib/Drupal/Core/RunTests/Config.php +++ b/core/lib/Drupal/Core/RunTests/Config.php @@ -14,18 +14,7 @@ */ class Config extends \ArrayObject { - /** - * Factory to create a config object based on input options. - * - * @param InputInterface $input - * Console input object. - * - * @return static - * Newly-created object. - */ - public static function createFromInput(InputInterface $input) { - // Create a new array object with default values. - $config = new static([ + static $defaults = [ 'script' => '', 'help' => FALSE, 'list' => FALSE, @@ -43,7 +32,7 @@ public static function createFromInput(InputInterface $input) { 'color' => FALSE, 'verbose' => FALSE, 'keep-results' => FALSE, - 'test_names' => array(), + 'test_names' => [], 'repeat' => 1, 'die-on-fail' => FALSE, 'browser' => FALSE, @@ -52,7 +41,20 @@ public static function createFromInput(InputInterface $input) { 'execute-test' => '', 'xml' => '', 'non-html' => FALSE, - ]); + ]; + + /** + * Factory to create a config object based on input options. + * + * @param InputInterface $input + * Console input object. + * + * @return static + * Newly-created object. + */ + public static function createFromInput(InputInterface $input) { + // Create a new array object with default values. + $config = new static(static::$defaults); // Add in our input options. $options = array_keys((array) $config); foreach ($options as $option) { @@ -60,15 +62,40 @@ public static function createFromInput(InputInterface $input) { $config[$option] = $input->getOption($option); } } - // Gather our test_names. - $config['test_names'] = []; if ($input->hasArgument('test_names')) { - $test_names = $input->getArgument('test_names'); - if (!empty($test_names)) { - $config['test_names'] = explode(',', $test_names); - } + $config['test_names'] = $input->getArgument('test_names'); + } + // Gather our test_names. + return $config->parseTestNames(); + } + + /** + * Factory to create a Config object from an array. + * + * @param array $array + * @return static + */ + public static function createFromArray($array) { + $config = new static(array_merge(static::$defaults, $array)); + return $config->parseTestNames(); + } + + /** + * Parse the test_names argument. + * + * The test_names argument arrives in comma-delimited form, and we want to + * turn that into an array. + * + * @return self + */ + protected function parseTestNames() { + if (!empty($this['test_names'])) { + $this['test_names'] = explode(',', $this['test_names']); + } + else { + $this['test_names'] = []; } - return $config; + return $this; } } diff --git a/core/lib/Drupal/Core/RunTests/RunTestsCommand.php b/core/lib/Drupal/Core/RunTests/RunTestsCommand.php index 00f19e5..db7f80e 100644 --- a/core/lib/Drupal/Core/RunTests/RunTestsCommand.php +++ b/core/lib/Drupal/Core/RunTests/RunTestsCommand.php @@ -35,6 +35,16 @@ /** * Test runner console command. + * + * This command discovers available tests, filters them based on input options, + * sets up the running environment for all the tests, and then runs each test in + * a separate process. + * + * @todo This class has methods with simpletest_underscore_names. Some of these + * are the names of the functions in the original run-tests.sh, and exist to + * aid in reference during development. We must change these at some point for + * coding standards. Other of these methods are named this way because they + * shim functions provided by the simpletest module, and enable testing. */ class RunTestsCommand extends Command { @@ -96,7 +106,7 @@ class RunTestsCommand extends Command { protected $test_ids = []; /** - * A list of all the test classes we'll be running. + * A list of the names of all the test classes we'll be running. * * @var string[] */ @@ -204,6 +214,8 @@ protected function execute(InputInterface $input, OutputInterface $output) { return SIMPLETEST_SCRIPT_EXIT_EXCEPTION; } + // If we got --execute-test, this is probably a child process running an + // individual test. if (!empty($this->config['execute-test'])) { $test_id = $this->config['test-id']; @@ -272,15 +284,6 @@ protected function execute(InputInterface $input, OutputInterface $output) { } /** - * Shim for simpletest_clean_results_table(). - * - * Enables testing. - */ - private function simpletest_clean_results_table() { - \simpletest_clean_results_table(); - } - - /** * Validate the configuration. * * @throws DrupalRunTestsException @@ -332,24 +335,6 @@ protected function listTests($module = NULL) { } /** - * Shim for \simpletest_test_get_all(). - * - * Enables testing. - * - * @param string|null $module - * (optional) Name of a module. If set then only tests belonging to this - * module are returned. Otherwise all tests are returned. Defaults to NULL. - * - * @return array[] - * An array of tests keyed with the groups, and then keyed by test classes. - * - * @see \simpletest_test_get_all() - */ - private function simpletest_test_get_all($module = NULL) { - return \simpletest_test_get_all($module); - } - - /** * Support the --clean option. * * @return int @@ -733,11 +718,13 @@ protected function getTestClassesForFileContents($contents) { } /** - * Get list of tests to run based on options. + * Get the list of tests to run based on input options. * * If --all is specified then return all available tests, otherwise read the * list of tests. * + * Unless otherwise specified, the list of tests is assumed to be test groups. + * * @return string[] * List of tests. * @@ -1495,4 +1482,33 @@ protected function simpletest_script_run_phpunit($test_id, $class) { return $status; } + /** + * Shim for simpletest_clean_results_table(). + * + * Enables testing. + * + * @see \simpletest_clean_results_table() + */ + private function simpletest_clean_results_table() { + \simpletest_clean_results_table(); + } + + /** + * Shim for \simpletest_test_get_all(). + * + * Enables testing. + * + * @param string|null $module + * (optional) Name of a module. If set then only tests belonging to this + * module are returned. Otherwise all tests are returned. Defaults to NULL. + * + * @return array[] + * An array of tests keyed with the groups, and then keyed by test classes. + * + * @see \simpletest_test_get_all() + */ + private function simpletest_test_get_all($module = NULL) { + return \simpletest_test_get_all($module); + } + } diff --git a/core/tests/Drupal/Tests/Core/RunTests/RunTestsCommandTest.php b/core/tests/Drupal/Tests/Core/RunTests/RunTestsCommandTest.php index a0b6e8a..55e4238 100644 --- a/core/tests/Drupal/Tests/Core/RunTests/RunTestsCommandTest.php +++ b/core/tests/Drupal/Tests/Core/RunTests/RunTestsCommandTest.php @@ -8,10 +8,9 @@ namespace Drupal\Tests\Core\RunTests; use Drupal\Tests\UnitTestCase; -use Drupal\Core\Session\AnonymousUserSession; -use Drupal\user\RoleInterface; use Drupal\Core\RunTests\RunTestsCommand; use Drupal\Core\RunTests\DrupalRunTestsException; +use Drupal\Core\RunTests\Config; /** * @coversDefaultClass \Drupal\Core\RunTests\RunTestsCommand @@ -113,7 +112,11 @@ public function provideGetClassesForFileContents() { // Class only. [ ['Bar'], - "class Bar", + 'class Bar', + ], + [ + [], + 'abstract class Bar', ], ]; } @@ -135,4 +138,56 @@ public function testGetClassesForFileContents($expected, $contents) { $getClassesForFileContents->invokeArgs($mock_command, [$contents]) ); } + + /** + * @return array + * - Expected command line string. + * - Config array. + * - Test ID as a string. + * - Test class name string. + */ + public function provideSimpletestScriptCommand() { + return [ + [ + "'' './core/scripts/' --url '' --php '' --test-id 23 --execute-test 'test\class'", + [], + '23', + 'test\class', + ], + [ + "'' './core/scripts/test_script' --url 'http:://awesome' --sqlite 'test.sqlite' --dburl 'yoursql://admin@password:foo' --php '' --test-id 24 --execute-test 'test\class'", + [ + 'script' => 'test_script', + 'url' => 'http:://awesome', + 'sqlite' => 'test.sqlite', + 'dburl' => 'yoursql://admin@password:foo', + ], + '24', + 'test\class', + ], + ]; + } + + /** + * @covers ::simpletest_script_command + * @dataProvider provideSimpletestScriptCommand + */ + public function testSimpletestScriptCommand($expected, $config_array, $test_id, $test_class) { + $mock_command = $this->getMockBuilder(RunTestsCommand::class) + ->disableOriginalConstructor() + ->getMock(); + + $simpletestScriptCommand = new \ReflectionMethod($mock_command, 'simpletest_script_command'); + $simpletestScriptCommand->setAccessible(TRUE); + + $ref_config = new \ReflectionProperty($mock_command, 'config'); + $ref_config->setAccessible(TRUE); + $ref_config->setValue($mock_command, Config::createFromArray($config_array)); + + $this->assertEquals( + $expected, + $simpletestScriptCommand->invokeArgs($mock_command, [$test_id, $test_class]) + ); + } + }