Skip to content
Snippets Groups Projects
Commit bc7f7f3c authored by catch's avatar catch
Browse files

Issue #2226183 by alexpott: Running PHPUnit tests through run-tests.sh is unnecessarily slow.

parent 24526aed
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace Drupal\block\Tests; namespace Drupal\block\Tests;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\Core\Plugin\TestConfigurablePlugin; use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
......
...@@ -228,20 +228,29 @@ function simpletest_phpunit_configuration_filepath() { ...@@ -228,20 +228,29 @@ function simpletest_phpunit_configuration_filepath() {
function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file) { function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file) {
$phpunit_bin = simpletest_phpunit_command(); $phpunit_bin = simpletest_phpunit_command();
$command = array(
$phpunit_bin,
'--log-junit',
escapeshellarg($phpunit_file),
);
// 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. // 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); return addslashes($class);
}, $unescaped_test_classnames); }, $unescaped_test_classnames);
$filter_string = implode("|", $escaped_test_classnames); $filter_string = implode("|", $escaped_test_classnames);
$command = array_merge($command, array(
$command = array(
$phpunit_bin,
'--filter', '--filter',
escapeshellarg($filter_string), escapeshellarg($filter_string),
'--log-junit', ));
escapeshellarg($phpunit_file), }
);
// Need to change directories before running the command so that we can use // Need to change directories before running the command so that we can use
// relative paths in the configuration file's exclusions. // relative paths in the configuration file's exclusions.
...@@ -811,20 +820,10 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) { ...@@ -811,20 +820,10 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
if (!$contents) { if (!$contents) {
return; return;
} }
$xml = new SimpleXMLElement($contents);
$records = array(); $records = array();
foreach ($xml->testsuite as $testsuite) { $testcases = simpletest_phpunit_find_testcases(new SimpleXMLElement($contents));
foreach ($testsuite as $suite) {
// The file element won't be on testcase objects created with
// @dataProvider, so just use the one from the test suite at this level
// because it should be the same.
$file = (string) $suite->attributes()->file;
$testcases = simpletest_phpunit_find_testcases($suite);
foreach ($testcases as $testcase) { foreach ($testcases as $testcase) {
$records[] = simpletest_phpunit_testcase_to_row($test_id, $testcase, $file); $records[] = simpletest_phpunit_testcase_to_row($test_id, $testcase);
}
}
} }
return $records; return $records;
} }
...@@ -832,27 +831,38 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) { ...@@ -832,27 +831,38 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
/** /**
* Finds all test cases recursively from a test suite list. * Finds all test cases recursively from a test suite list.
* *
* @param \SimpleXMLElement[] $suite * @param \SimpleXMLElement $element
* The list of test cases contained in the PHPUnit XML. * The PHPUnit xml to search for test cases.
* @param \SimpleXMLElement $suite
* (Optional) The parent of the current element. Defaults to NULL.
* *
* @return array * @return array
* A list of all test cases. * A list of all test cases.
*/ */
function simpletest_phpunit_find_testcases($suite) { function simpletest_phpunit_find_testcases(\SimpleXMLElement $element, \SimpleXMLElement $parent = NULL) {
$testcases = array(); $testcases = array();
foreach ($suite as $testcase) { if (!isset($parent)) {
// Beside from being 'testcases', it could be also a group of testcases. $parent = $element;
// This happens if you use a data provider in the PHPUnit tests. }
if ($testcase->getName() === 'testcase') {
$testcases[] = $testcase; if ($element->getName() === 'testcase' && (int) $parent->attributes()->tests > 0) {
// Add the class attribute if the testcase does not have one. This is the
// case for tests using a data provider. The name of the parent testsuite
// will be in the format class::method.
if (!$element->attributes()->class) {
$name = explode('::', $parent->attributes()->name, 2);
$element->addAttribute('class', $name[0]);
} }
elseif (isset($testcase->testcase) && ((int) $testcase->attributes()->tests) > 0) { $testcases[] = $element;
foreach ($testcase->testcase as $childtestcase) { }
// Add the class attribute since the child test case will not have it. else {
$childtestcase->addAttribute('class', $suite->attributes()->name); foreach ($element as $child) {
$testcases[] = $childtestcase; $file = (string) $parent->attributes()->file;
if ($file && !$child->attributes()->file) {
$child->addAttribute('file', $file);
} }
$testcases = array_merge($testcases, simpletest_phpunit_find_testcases($child, $element));
} }
} }
return $testcases; return $testcases;
...@@ -865,13 +875,11 @@ function simpletest_phpunit_find_testcases($suite) { ...@@ -865,13 +875,11 @@ function simpletest_phpunit_find_testcases($suite) {
* The current test ID. * The current test ID.
* @param \SimpleXMLElement $testcase * @param \SimpleXMLElement $testcase
* The PHPUnit test case represented as XML element. * The PHPUnit test case represented as XML element.
* @param string $file
* The path to test file, which was executed.
* *
* @return array * @return array
* An array containing the {simpletest} result row. * An array containing the {simpletest} result row.
*/ */
function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcase, $file) { function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcase) {
$message = ''; $message = '';
$pass = TRUE; $pass = TRUE;
if ($testcase->failure) { if ($testcase->failure) {
...@@ -895,7 +903,7 @@ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcas ...@@ -895,7 +903,7 @@ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcas
'message_group' => 'Other', 'message_group' => 'Other',
'function' => $attributes->class . '->' . $attributes->name . '()', 'function' => $attributes->class . '->' . $attributes->name . '()',
'line' => $attributes->line ?: 0, 'line' => $attributes->line ?: 0,
'file' => $file, 'file' => $attributes->file,
); );
return $record; return $record;
} }
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
namespace Drupal\Tests\Core\Plugin; namespace Drupal\Tests\Core\Plugin;
use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
use Drupal\Component\Plugin\PluginBase;
/** /**
* Tests the default plugin bag with configurable plugins. * Tests the default plugin bag with configurable plugins.
...@@ -72,28 +71,3 @@ public function testConfigurableSetConfiguration() { ...@@ -72,28 +71,3 @@ public function testConfigurableSetConfiguration() {
} }
} }
class TestConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array();
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin.
*/
namespace Drupal\Tests\Core\Plugin\Fixtures;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginBase;
class TestConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment