diff --git a/core/lib/Drupal/Core/Test/JUnitListener.php b/core/lib/Drupal/Core/Test/JUnitListener.php index 2f23fcdcf9..0f586337c7 100644 --- a/core/lib/Drupal/Core/Test/JUnitListener.php +++ b/core/lib/Drupal/Core/Test/JUnitListener.php @@ -12,7 +12,6 @@ use PHPUnit\Framework\TestSuite; use PHPUnit\Framework\Warning; use PHPUnit\Util\Filter; -use PHPUnit\Util\Printer; use PHPUnit\Util\Xml; /** @@ -43,44 +42,9 @@ class JUnitListener implements TestListener { protected $testSuites = []; /** - * @var int[] + * @var array */ - protected $testSuiteTests = [0]; - - /** - * @var int[] - */ - protected $testSuiteAssertions = [0]; - - /** - * @var int[] - */ - protected $testSuiteErrors = [0]; - - /** - * @var int[] - */ - protected $testSuiteFailures = [0]; - - /** - * @var int[] - */ - protected $testSuiteRisky = [0]; - - /** - * @var int[] - */ - protected $testSuiteSkipped = [0]; - - /** - * @var int[] - */ - protected $testSuiteIncomplete = [0]; - - /** - * @var int[] - */ - protected $testSuiteTimes = [0]; + protected $testSuiteRuns = []; /** * @var int @@ -122,7 +86,7 @@ public function __destruct() { */ public function addError(Test $test, \Exception $e, $time) { $this->doAddFault($test, $e, $time, 'error'); - $this->testSuiteErrors[$this->testSuiteLevel]++; + $this->testSuiteRuns[$this->testSuiteLevel]['errors']++; } /** @@ -134,7 +98,7 @@ public function addError(Test $test, \Exception $e, $time) { */ public function addWarning(Test $test, Warning $e, $time) { $this->doAddFault($test, $e, $time, 'warning'); - $this->testSuiteFailures[$this->testSuiteLevel]++; + $this->testSuiteRuns[$this->testSuiteLevel]['failures']++; } /** @@ -146,7 +110,7 @@ public function addWarning(Test $test, Warning $e, $time) { */ public function addFailure(Test $test, AssertionFailedError $e, $time) { $this->doAddFault($test, $e, $time, 'failure'); - $this->testSuiteFailures[$this->testSuiteLevel]++; + $this->testSuiteRuns[$this->testSuiteLevel]['failures']++; } /** @@ -194,25 +158,29 @@ public function startTestSuite(TestSuite $suite) { try { $class = new \ReflectionClass($suite->getName()); $testSuite->setAttribute('file', $class->getFileName()); - } catch (\ReflectionException $e) { + } + catch (\ReflectionException $e) { // Do nothing. } } if ($this->testSuiteLevel > 0) { $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite); - } else { + } + else { $this->root->appendChild($testSuite); } $this->testSuiteLevel++; $this->testSuites[$this->testSuiteLevel] = $testSuite; - $this->testSuiteTests[$this->testSuiteLevel] = 0; - $this->testSuiteAssertions[$this->testSuiteLevel] = 0; - $this->testSuiteErrors[$this->testSuiteLevel] = 0; - $this->testSuiteRisky[$this->testSuiteLevel] = 0; - $this->testSuiteFailures[$this->testSuiteLevel] = 0; - $this->testSuiteSkipped[$this->testSuiteLevel] = 0; - $this->testSuiteIncomplete[$this->testSuiteLevel] = 0; - $this->testSuiteTimes[$this->testSuiteLevel] = 0; + $this->testSuiteRuns[$this->testSuiteLevel] = [ + 'tests' => 0, + 'assertions' => 0, + 'errors' => 0, + 'failures' => 0, + 'risky' => 0, + 'skipped' => 0, + 'incomplete' => 0, + 'time' => 0, + ]; } /** @@ -221,23 +189,28 @@ public function startTestSuite(TestSuite $suite) { * @param \PHPUnit\Framework\TestSuite $suite */ public function endTestSuite(TestSuite $suite) { - $this->testSuites[$this->testSuiteLevel]->setAttribute('tests', $this->testSuiteTests[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('assertions', $this->testSuiteAssertions[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('errors', $this->testSuiteErrors[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('failures', $this->testSuiteFailures[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('risky', $this->testSuiteRisky[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('skipped', $this->testSuiteSkipped[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('incomplete', $this->testSuiteIncomplete[$this->testSuiteLevel]); - $this->testSuites[$this->testSuiteLevel]->setAttribute('time', sprintf('%F', $this->testSuiteTimes[$this->testSuiteLevel])); + $properties = [ + 'tests', + 'assertions', + 'errors', + 'failures', + 'risky', + 'skipped', + 'incomplete', + ]; + + // Add summary to the DOM element. + foreach ($properties as $property) { + $this->testSuites[$this->testSuiteLevel]->setAttribute($property, $this->testSuiteRuns[$this->testSuiteLevel][$property]); + } + $this->testSuites[$this->testSuiteLevel]->setAttribute('time', sprintf('%F', $this->testSuiteRuns[$this->testSuiteLevel]['time'])); + + // When nesting, sum up results to the parent testsuite. if ($this->testSuiteLevel > 1) { - $this->testSuiteTests[$this->testSuiteLevel - 1] += $this->testSuiteTests[$this->testSuiteLevel]; - $this->testSuiteAssertions[$this->testSuiteLevel - 1] += $this->testSuiteAssertions[$this->testSuiteLevel]; - $this->testSuiteErrors[$this->testSuiteLevel - 1] += $this->testSuiteErrors[$this->testSuiteLevel]; - $this->testSuiteFailures[$this->testSuiteLevel - 1] += $this->testSuiteFailures[$this->testSuiteLevel]; - $this->testSuiteRisky[$this->testSuiteLevel - 1] += $this->testSuiteRisky[$this->testSuiteLevel]; - $this->testSuiteSkipped[$this->testSuiteLevel - 1] += $this->testSuiteSkipped[$this->testSuiteLevel]; - $this->testSuiteIncomplete[$this->testSuiteLevel - 1] += $this->testSuiteIncomplete[$this->testSuiteLevel]; - $this->testSuiteTimes[$this->testSuiteLevel - 1] += $this->testSuiteTimes[$this->testSuiteLevel]; + foreach ($properties as $property) { + $this->testSuiteRuns[$this->testSuiteLevel - 1][$property] += $this->testSuiteRuns[$this->testSuiteLevel][$property]; + } + $this->testSuiteRuns[$this->testSuiteLevel - 1]['time'] += $this->testSuiteRuns[$this->testSuiteLevel]['time']; } $this->testSuiteLevel--; } @@ -252,7 +225,7 @@ public function startTest(Test $test) { $testCase->setAttribute('name', $test->getName()); if ($test instanceof TestCase) { - $class = new \ReflectionClass($test); + $class = new \ReflectionClass($test); $methodName = $test->getName(!$test->usesDataProvider()); if ($class->hasMethod($methodName)) { @@ -277,7 +250,7 @@ public function startTest(Test $test) { public function endTest(Test $test, $time) { if ($test instanceof TestCase) { $num_assertions = $test->getNumAssertions(); - $this->testSuiteAssertions[$this->testSuiteLevel] += $num_assertions; + $this->testSuiteRuns[$this->testSuiteLevel]['assertions'] += $num_assertions; $this->currentTestCase->setAttribute('assertions', $num_assertions); } @@ -288,8 +261,8 @@ public function endTest(Test $test, $time) { $this->currentTestCase ); - $this->testSuiteTests[$this->testSuiteLevel]++; - $this->testSuiteTimes[$this->testSuiteLevel] += $time; + $this->testSuiteRuns[$this->testSuiteLevel]['tests']++; + $this->testSuiteRuns[$this->testSuiteLevel]['time'] += $time; if (method_exists($test, 'hasOutput') && $test->hasOutput()) { $systemOut = $this->document->createElement('system-out', Xml::prepareString($test->getActualOutput())); @@ -324,7 +297,8 @@ private function doAddFault(Test $test, \Exception $e, $time, $type) { if ($test instanceof SelfDescribing) { $buffer = $test->toString() . "\n"; - } else { + } + else { $buffer = ''; } @@ -334,7 +308,8 @@ private function doAddFault(Test $test, \Exception $e, $time, $type) { if ($e instanceof ExceptionWrapper) { $fault->setAttribute('type', $e->getClassName()); - } else { + } + else { $fault->setAttribute('type', get_class($e)); } @@ -362,15 +337,15 @@ private function doAddSkipped(Test $test, $message) { switch ($message) { case 'risky': - $this->testSuiteRisky[$this->testSuiteLevel]++; + $this->testSuiteRuns[$this->testSuiteLevel]['risky']++; break; case 'skipped': - $this->testSuiteSkipped[$this->testSuiteLevel]++; + $this->testSuiteRuns[$this->testSuiteLevel]['skipped']++; break; case 'incomplete': - $this->testSuiteIncomplete[$this->testSuiteLevel]++; + $this->testSuiteRuns[$this->testSuiteLevel]['incomplete']++; break; }