diff --git a/core/lib/Drupal/Core/Test/JUnitConverter.php b/core/lib/Drupal/Core/Test/JUnitConverter.php
index 5c60694385..5af99f07d9 100644
--- a/core/lib/Drupal/Core/Test/JUnitConverter.php
+++ b/core/lib/Drupal/Core/Test/JUnitConverter.php
@@ -3,14 +3,16 @@
namespace Drupal\Core\Test;
/**
- * Converts JUnit XML to a form Drupal SimpleTest can use.
+ * Converts JUnit XML to Drupal's {simpletest} schema.
*
- * This is mainly for converting PHPUnit output.
+ * This is mainly for converting PHPUnit test results.
+ *
+ * This class is @internal and not considered to be API.
*/
class JUnitConverter {
/**
- * Converts PHPUnit's JUnit XML output to an array.
+ * Converts PHPUnit's JUnit XML output file to {simpletest} schema.
*
* @param int $test_id
* The current test ID.
@@ -20,14 +22,34 @@ class JUnitConverter {
* @return array[]
* The results as array of rows in a format that can be inserted into the
* {simpletest} table of the results database.
+ *
+ * @internal
*/
public static function xmlToRows($test_id, $phpunit_xml_file) {
$contents = @file_get_contents($phpunit_xml_file);
if (!$contents) {
return [];
}
+ return static::xmlElementToRows($test_id, new \SimpleXMLElement($contents));
+ }
+
+ /**
+ * Parse test cases from XML to {simpletest} schema.
+ *
+ * @param int $test_id
+ * The current test ID.
+ * @param \SimpleXMLElement $element
+ * The XML data from the JUnit file.
+ *
+ * @return array[]
+ * The results as array of rows in a format that can be inserted into the
+ * {simpletest} table of the results database.
+ *
+ * @internal
+ */
+ public static function xmlElementToRows($test_id, \SimpleXMLElement $element) {
$records = [];
- $test_cases = static::findTestCases(new \SimpleXMLElement($contents));
+ $test_cases = static::findTestCases($element);
foreach ($test_cases as $test_case) {
$records[] = static::convertTestCaseToSimpletestRow($test_id, $test_case);
}
@@ -44,6 +66,8 @@ public static function xmlToRows($test_id, $phpunit_xml_file) {
*
* @return array
* A list of all test cases.
+ *
+ * @internal
*/
public static function findTestCases(\SimpleXMLElement $element, \SimpleXMLElement $parent = NULL) {
if (!isset($parent)) {
@@ -81,6 +105,8 @@ public static function findTestCases(\SimpleXMLElement $element, \SimpleXMLEleme
*
* @return array
* An array containing the {simpletest} result row.
+ *
+ * @internal
*/
public static function convertTestCaseToSimpletestRow($test_id, \SimpleXMLElement $test_case) {
$message = '';
@@ -104,8 +130,8 @@ public static function convertTestCaseToSimpletestRow($test_id, \SimpleXMLElemen
'message' => $message,
'message_group' => 'Other',
'function' => $attributes->class . '->' . $attributes->name . '()',
- 'line' => $attributes->line ?: 0,
- 'file' => $attributes->file,
+ 'line' => (int) $attributes->line ?: 0,
+ 'file' => (string) $attributes->file,
];
return $record;
}
diff --git a/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php b/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php
index e1e1087ef0..47038d0509 100644
--- a/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php
+++ b/core/lib/Drupal/Core/Test/PhpUnitTestRunner.php
@@ -11,10 +11,11 @@
/**
* Run PHPUnit-based tests.
*
- * Drupal's test runners require a way to run PHPUnit-based tests and offer data
- * that can be converted from JUnit to a form SimpleTest can use.
+ * This class runs PHPUnit-based tests and converts their JUnit results to a
+ * format that can be stored in the {simpletest} database schema.
+ *
+ * This class is @internal and not considered to be API.
*
- * This class encapsulates that process. Example:
* @code
* $runner = PhpUnitTestRunner::create(\Drupal::getContainer());
* $results = $runner->runTests($test_id, $test_list['phpunit']);
@@ -76,6 +77,8 @@ public function __construct($app_root, $working_directory) {
*
* @return string
* Path to the PHPUnit XML file to use for the current $test_id.
+ *
+ * @internal
*/
public function xmlLogFilePath($test_id) {
return $this->workingDirectory . '/phpunit-' . $test_id . '.xml';
@@ -86,6 +89,8 @@ public function xmlLogFilePath($test_id) {
*
* @return string
* The command that can be run through exec().
+ *
+ * @internal
*/
public function phpUnitCommand() {
// Load the actual autoloader being used and determine its filename using
@@ -123,6 +128,8 @@ public function phpUnitCommand() {
*
* @return string
* The results as returned by exec().
+ *
+ * @internal
*/
public function runCommand(array $unescaped_test_classnames, $phpunit_file, &$status = NULL, &$output = NULL) {
global $base_url;
@@ -199,6 +206,8 @@ public function runCommand(array $unescaped_test_classnames, $phpunit_file, &$st
* @return array
* The parsed results of PHPUnit's JUnit XML output, in the format of
* {simpletest}'s schema.
+ *
+ * @internal
*/
public function runTests($test_id, array $unescaped_test_classnames, &$status = NULL) {
$phpunit_file = $this->xmlLogFilePath($test_id);
@@ -221,4 +230,49 @@ public function runTests($test_id, array $unescaped_test_classnames, &$status =
]];
}
+ /**
+ * Tallies test results per test class.
+ *
+ * @param string[][] $results
+ * Array of results in the {simpletest} schema. Can be the return value of
+ * PhpUnitTestRunner::runTests().
+ *
+ * @return int[][]
+ * Array of status tallies, keyed by test class name and status type.
+ *
+ * @internal
+ */
+ public function summarizeResults($results) {
+ $summaries = [];
+ foreach ($results as $result) {
+ if (!isset($summaries[$result['test_class']])) {
+ $summaries[$result['test_class']] = [
+ '#pass' => 0,
+ '#fail' => 0,
+ '#exception' => 0,
+ '#debug' => 0,
+ ];
+ }
+
+ switch ($result['status']) {
+ case 'pass':
+ $summaries[$result['test_class']]['#pass'] ++;
+ break;
+
+ case 'fail':
+ $summaries[$result['test_class']]['#fail'] ++;
+ break;
+
+ case 'exception':
+ $summaries[$result['test_class']]['#exception'] ++;
+ break;
+
+ case 'debug':
+ $summaries[$result['test_class']]['#debug'] ++;
+ break;
+ }
+ }
+ return $summaries;
+ }
+
}
diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index 562e535cf1..ed30ccb6a2 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -227,38 +227,16 @@ function simpletest_process_phpunit_results($phpunit_results) {
*
* @return array
* The test result summary. A row per test class.
+ *
+ * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
+ * \Drupal\Core\Test\PhpUnitTestRunner::summarizeResults() instead.
+ *
+ * @see https://www.drupal.org/node/2948547
*/
function simpletest_summarize_phpunit_result($results) {
- $summaries = [];
- foreach ($results as $result) {
- if (!isset($summaries[$result['test_class']])) {
- $summaries[$result['test_class']] = [
- '#pass' => 0,
- '#fail' => 0,
- '#exception' => 0,
- '#debug' => 0,
- ];
- }
-
- switch ($result['status']) {
- case 'pass':
- $summaries[$result['test_class']]['#pass']++;
- break;
-
- case 'fail':
- $summaries[$result['test_class']]['#fail']++;
- break;
-
- case 'exception':
- $summaries[$result['test_class']]['#exception']++;
- break;
-
- case 'debug':
- $summaries[$result['test_class']]['#debug']++;
- break;
- }
- }
- return $summaries;
+ $runner = PhpUnitTestRunner::create(\Drupal::getContainer());
+ @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\PhpUnitTestRunner::summarizeResults() instead. See https://www.drupal.org/node/2948547', E_USER_DEPRECATED);
+ return $runner->summarizeResults($results);
}
/**
diff --git a/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php b/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
index 389261fb6f..e5e3111e4f 100644
--- a/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
+++ b/core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
@@ -39,6 +39,7 @@ public function testDeprecatedServices() {
* @expectedDeprecation simpletest_phpunit_command is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\PhpUnitTestRunner::phpUnitCommand() instead. See https://www.drupal.org/node/2948547
* @expectedDeprecation simpletest_phpunit_find_testcases is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\JUnitConverter::findTestCases() instead. See https://www.drupal.org/node/2948547
* @expectedDeprecation simpletest_phpunit_testcase_to_row is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\JUnitConverter::convertTestCaseToSimpletestRow() instead. See https://www.drupal.org/node/2948547
+ * @expectedDeprecation simpletest_summarize_phpunit_result is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\PhpUnitTestRunner::summarizeResults() instead. See https://www.drupal.org/node/2948547
*/
public function testDeprecatedPhpUnitFunctions() {
// We can't test the deprecation errors for the following functions because
@@ -47,8 +48,11 @@ public function testDeprecatedPhpUnitFunctions() {
// - simpletest_phpunit_run_command().
// - simpletest_phpunit_xml_to_rows().
$this->assertStringEndsWith('/phpunit-23.xml', simpletest_phpunit_xml_filepath(23));
+
$this->assertInternalType('string', simpletest_phpunit_command());
+
$this->assertEquals([], simpletest_phpunit_find_testcases(new \SimpleXMLElement('')));
+
$this->assertEquals([
'test_id' => 23,
'test_class' => '',
@@ -59,6 +63,19 @@ public function testDeprecatedPhpUnitFunctions() {
'line' => 0,
'file' => NULL,
], simpletest_phpunit_testcase_to_row(23, new \SimpleXMLElement('')));
+
+ $this->assertEquals([
+ static::class => [
+ '#pass' => 0,
+ '#fail' => 0,
+ '#exception' => 0,
+ '#debug' => 1,
+ ]],
+ simpletest_summarize_phpunit_result([[
+ 'test_class' => static::class,
+ 'status' => 'debug',
+ ]])
+ );
}
}
diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh
index 7b2a0088b6..3496e0c6ba 100755
--- a/core/scripts/run-tests.sh
+++ b/core/scripts/run-tests.sh
@@ -14,10 +14,10 @@
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\PublicStream;
+use Drupal\Core\Test\PhpUnitTestRunner;
use Drupal\Core\Test\TestDatabase;
use Drupal\Core\Test\TestRunnerKernel;
use Drupal\simpletest\Form\SimpletestResultsForm;
-use Drupal\simpletest\TestBase;
use Drupal\Core\Test\TestDiscovery;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version;
@@ -790,12 +790,11 @@ function simpletest_script_run_phpunit($test_id, $class) {
set_time_limit($reflection->getStaticPropertyValue('runLimit'));
}
- $results = simpletest_run_phpunit_tests($test_id, [$class], $status);
+ $runner = PhpUnitTestRunner::create(\Drupal::getContainer());
+ $results = $runner->runTests($test_id, [$class], $status);
simpletest_process_phpunit_results($results);
- // Map phpunit results to a data structure we can pass to
- // _simpletest_format_summary_line.
- $summaries = simpletest_summarize_phpunit_result($results);
+ $summaries = $runner->summarizeResults($results);
foreach ($summaries as $class => $summary) {
simpletest_script_reporter_display_summary($class, $summary);
}
diff --git a/core/tests/Drupal/Tests/Core/Test/JUnitConverterTest.php b/core/tests/Drupal/Tests/Core/Test/JUnitConverterTest.php
new file mode 100644
index 0000000000..1aa5bdecb4
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Test/JUnitConverterTest.php
@@ -0,0 +1,100 @@
+assertEquals(count($res), 4, 'All testcases got extracted');
+ $this->assertNotEquals($res[0]['status'], 'pass');
+ $this->assertEquals($res[0]['status'], 'fail');
+
+ // Test nested testsuites, which appear when you use @dataProvider.
+ for ($i = 0; $i < 3; $i++) {
+ $this->assertNotEquals($res[$i + 1]['status'], 'pass');
+ $this->assertEquals($res[$i + 1]['status'], 'fail');
+ }
+
+ // Make sure xmlToRows() does not balk if there are no test results.
+ $this->assertSame([], JUnitConverter::xmlToRows(1, 'does_not_exist'));
+ }
+
+ /**
+ * @covers ::xmlToRows
+ */
+ public function testXmlToRowsEmptyFile() {
+ // File system with an empty XML file.
+ vfsStream::setup('junit_test', NULL, ['empty.xml' => '']);
+ $this->assertArrayEquals([], JUnitConverter::xmlToRows(23, vfsStream::url('junit_test/empty.xml')));
+ }
+
+ /**
+ * @covers ::xmlElementToRows
+ */
+ public function testXmlElementToRows() {
+ $junit = <<
+
+
+
+
+
+EOD;
+ $simpletest = [[
+ 'test_id' => 23,
+ 'test_class' => 'Drupal\Tests\simpletest\Unit\TestDiscoveryTest',
+ 'status' => 'pass',
+ 'message' => '',
+ 'message_group' => 'Other',
+ 'function' => 'Drupal\Tests\simpletest\Unit\TestDiscoveryTest->testGetTestClasses()',
+ 'line' => 108,
+ 'file' => '/Users/paul/projects/drupal/core/modules/simpletest/tests/src/Unit/TestDiscoveryTest.php',
+ ]];
+ $this->assertArrayEquals($simpletest, JUnitConverter::xmlElementToRows(23, new \SimpleXMLElement($junit)));
+ }
+
+ /**
+ * @covers ::convertTestCaseToSimpletestRow
+ */
+ public function testConvertTestCaseToSimpletestRow() {
+ $junit = <<
+EOD;
+ $simpletest = [
+ 'test_id' => 23,
+ 'test_class' => 'Drupal\Tests\simpletest\Unit\TestDiscoveryTest',
+ 'status' => 'pass',
+ 'message' => '',
+ 'message_group' => 'Other',
+ 'function' => 'Drupal\Tests\simpletest\Unit\TestDiscoveryTest->testGetTestClasses()',
+ 'line' => 108,
+ 'file' => '/Users/paul/projects/drupal/core/modules/simpletest/tests/src/Unit/TestDiscoveryTest.php',
+ ];
+ $this->assertArrayEquals($simpletest, JUnitConverter::convertTestCaseToSimpletestRow(23, new \SimpleXMLElement($junit)));
+ }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php b/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php
index 8668777070..b67a21833d 100644
--- a/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php
+++ b/core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php
@@ -66,4 +66,68 @@ function ($unescaped_test_classnames, $phpunit_file, &$status) {
$this->assertEquals($fail_row, $row);
}
+ /**
+ * @covers ::phpUnitCommand
+ */
+ public function testPhpUnitCommand() {
+ $runner = new PhpUnitTestRunner($this->root, sys_get_temp_dir());
+ $this->assertRegExp('/phpunit/', $runner->phpUnitCommand());
+ }
+
+ /**
+ * @covers ::xmlLogFilePath
+ */
+ public function testXmlLogFilePath() {
+ $runner = new PhpUnitTestRunner($this->root, sys_get_temp_dir());
+ $this->assertStringEndsWith('phpunit-23.xml', $runner->xmlLogFilePath(23));
+ }
+
+ public function providerTestSummarizeResults() {
+ return [
+ [
+ [[
+ 'test_class' => static::class,
+ 'status' => 'pass',
+ ]],
+ '#pass',
+ ],
+ [
+ [[
+ 'test_class' => static::class,
+ 'status' => 'fail',
+ ]],
+ '#fail',
+ ],
+ [
+ [[
+ 'test_class' => static::class,
+ 'status' => 'exception',
+ ]],
+ '#exception',
+ ],
+ [
+ [[
+ 'test_class' => static::class,
+ 'status' => 'debug',
+ ]],
+ '#debug',
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider providerTestSummarizeResults
+ * @covers ::summarizeResults
+ */
+ public function testSummarizeResults($results, $has_status) {
+ $runner = new PhpUnitTestRunner($this->root, sys_get_temp_dir());
+ $summary = $runner->summarizeResults($results);
+
+ $this->assertArrayHasKey(static::class, $summary);
+ $this->assertEquals(1, $summary[static::class][$has_status]);
+ foreach( array_diff(['#pass', '#fail', '#exception', '#debug'], [$has_status]) as $should_be_zero) {
+ $this->assertSame(0, $summary[static::class][$should_be_zero]);
+ }
+ }
+
}
diff --git a/core/tests/Drupal/Tests/Core/Test/fixtures/phpunit_error.xml b/core/tests/Drupal/Tests/Core/Test/fixtures/phpunit_error.xml
new file mode 100644
index 0000000000..82386aea74
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Test/fixtures/phpunit_error.xml
@@ -0,0 +1,43 @@
+
+
+
+
+
+
+
+
+
+ Drupal\Tests\Core\Extension\ModuleHandlerUnitTest::testloadInclude
+Undefined index: foo
+
+/home/chx/www/system/core/lib/Drupal/Core/Extension/ModuleHandler.php:219
+/home/chx/www/system/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerUnitTest.php:40
+
+
+
+
+
+
+
+
+ Drupal\Tests\Core\Route\RoleAccessCheckTest::testRoleAccess with data set #0 ('role_test_1', array(Drupal\user\Entity\User, Drupal\user\Entity\User))
+ Access granted for user with the roles role_test_1 on path: role_test_1
+ Failed asserting that false is true.
+
+
+
+ Drupal\Tests\Core\Route\RoleAccessCheckTest::testRoleAccess with data set #1 ('role_test_2', array(Drupal\user\Entity\User, Drupal\user\Entity\User))
+ Access granted for user with the roles role_test_2 on path: role_test_2
+ Failed asserting that false is true.
+
+
+
+ Drupal\Tests\Core\Route\RoleAccessCheckTest::testRoleAccess with data set #2 ('role_test_3', array(Drupal\user\Entity\User))
+ Access granted for user with the roles role_test_1, role_test_2 on path: role_test_3
+ Failed asserting that false is true.
+
+
+
+
+
+