Commit bc7f7f3c authored by catch's avatar catch

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

parent 24526aed
......@@ -8,7 +8,7 @@
namespace Drupal\block\Tests;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\Core\Plugin\TestConfigurablePlugin;
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
use Drupal\Tests\UnitTestCase;
/**
......
......@@ -228,21 +228,30 @@ function simpletest_phpunit_configuration_filepath() {
function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file) {
$phpunit_bin = simpletest_phpunit_command();
// 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(
$phpunit_bin,
'--filter',
escapeshellarg($filter_string),
'--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.
$escaped_test_classnames = array_map(function($class) {
return addslashes($class);
}, $unescaped_test_classnames);
$filter_string = implode("|", $escaped_test_classnames);
$command = array_merge($command, array(
'--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();
......@@ -811,20 +820,10 @@ function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
if (!$contents) {
return;
}
$xml = new SimpleXMLElement($contents);
$records = array();
foreach ($xml->testsuite as $testsuite) {
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) {
$records[] = simpletest_phpunit_testcase_to_row($test_id, $testcase, $file);
}
}
$testcases = simpletest_phpunit_find_testcases(new SimpleXMLElement($contents));
foreach ($testcases as $testcase) {
$records[] = simpletest_phpunit_testcase_to_row($test_id, $testcase);
}
return $records;
}
......@@ -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.
*
* @param \SimpleXMLElement[] $suite
* The list of test cases contained in the PHPUnit XML.
* @param \SimpleXMLElement $element
* The PHPUnit xml to search for test cases.
* @param \SimpleXMLElement $suite
* (Optional) The parent of the current element. Defaults to NULL.
*
* @return array
* A list of all test cases.
*/
function simpletest_phpunit_find_testcases($suite) {
function simpletest_phpunit_find_testcases(\SimpleXMLElement $element, \SimpleXMLElement $parent = NULL) {
$testcases = array();
foreach ($suite as $testcase) {
// Beside from being 'testcases', it could be also a group of testcases.
// This happens if you use a data provider in the PHPUnit tests.
if ($testcase->getName() === 'testcase') {
$testcases[] = $testcase;
if (!isset($parent)) {
$parent = $element;
}
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) {
foreach ($testcase->testcase as $childtestcase) {
// Add the class attribute since the child test case will not have it.
$childtestcase->addAttribute('class', $suite->attributes()->name);
$testcases[] = $childtestcase;
$testcases[] = $element;
}
else {
foreach ($element as $child) {
$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;
......@@ -865,13 +875,11 @@ function simpletest_phpunit_find_testcases($suite) {
* The current test ID.
* @param \SimpleXMLElement $testcase
* The PHPUnit test case represented as XML element.
* @param string $file
* The path to test file, which was executed.
*
* @return array
* 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 = '';
$pass = TRUE;
if ($testcase->failure) {
......@@ -895,7 +903,7 @@ function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcas
'message_group' => 'Other',
'function' => $attributes->class . '->' . $attributes->name . '()',
'line' => $attributes->line ?: 0,
'file' => $file,
'file' => $attributes->file,
);
return $record;
}
......@@ -7,8 +7,7 @@
namespace Drupal\Tests\Core\Plugin;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
/**
* Tests the default plugin bag with configurable plugins.
......@@ -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();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment