diff --git a/core/lib/Drupal/Core/Test/JUnitListener.php b/core/lib/Drupal/Core/Test/JUnitListener.php index a7c4bcbd2e..2f23fcdcf9 100644 --- a/core/lib/Drupal/Core/Test/JUnitListener.php +++ b/core/lib/Drupal/Core/Test/JUnitListener.php @@ -22,399 +22,358 @@ */ class JUnitListener implements TestListener { - /** - * @var \DOMDocument - */ - protected $document; - - /** - * @var \DOMElement - */ - protected $root; - - /** - * @var bool - */ - protected $writeDocument = true; - - /** - * @var \DOMElement[] - */ - protected $testSuites = []; - - /** - * @var int[] - */ - 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]; - - /** - * @var int - */ - protected $testSuiteLevel = 0; - - /** - * @var DOMElement - */ - protected $currentTestCase; - - /** - * Constructor. - */ - public function __construct() { - $this->document = new \DOMDocument('1.0', 'UTF-8'); - $this->document->formatOutput = true; - - $this->root = $this->document->createElement('testsuites'); - $this->document->appendChild($this->root); + /** + * @var \DOMDocument + */ + protected $document; + + /** + * @var \DOMElement + */ + protected $root; + + /** + * @var bool + */ + protected $writeDocument = TRUE; + + /** + * @var \DOMElement[] + */ + protected $testSuites = []; + + /** + * @var int[] + */ + 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]; + + /** + * @var int + */ + protected $testSuiteLevel = 0; + + /** + * @var \DOMElement + */ + protected $currentTestCase; + + /** + * Constructor. + */ + public function __construct() { + $this->document = new \DOMDocument('1.0', 'UTF-8'); + $this->document->formatOutput = TRUE; + + $this->root = $this->document->createElement('testsuites'); + $this->document->appendChild($this->root); + } + + /** + * Destructor. + */ + public function __destruct() { + $junit_file = getenv('SIMPLETEST_JUNIT_FILE'); + if (!empty($junit_file)) { + @file_put_contents($junit_file, $this->getXML()); } - - /** - * Destructor. - */ - public function __destruct() { - $junit_file = getenv('SIMPLETEST_JUNIT_FILE'); - if (!empty($junit_file)) { - @file_put_contents($junit_file, $this->getXML()); + } + + /** + * An error occurred. + * + * @param \PHPUnit\Framework\Test $test + * @param \Exception $e + * @param float $time + */ + public function addError(Test $test, \Exception $e, $time) { + $this->doAddFault($test, $e, $time, 'error'); + $this->testSuiteErrors[$this->testSuiteLevel]++; + } + + /** + * A warning occurred. + * + * @param \PHPUnit\Framework\Test $test + * @param \PHPUnit\Framework\Warning $e + * @param float $time + */ + public function addWarning(Test $test, Warning $e, $time) { + $this->doAddFault($test, $e, $time, 'warning'); + $this->testSuiteFailures[$this->testSuiteLevel]++; + } + + /** + * A failure occurred. + * + * @param \PHPUnit\Framework\Test $test + * @param \PHPUnit\Framework\AssertionFailedError $e + * @param float $time + */ + public function addFailure(Test $test, AssertionFailedError $e, $time) { + $this->doAddFault($test, $e, $time, 'failure'); + $this->testSuiteFailures[$this->testSuiteLevel]++; + } + + /** + * Incomplete test. + * + * @param \PHPUnit\Framework\Test $test + * @param \Exception $e + * @param float $time + */ + public function addIncompleteTest(Test $test, \Exception $e, $time) { + $this->doAddSkipped($test, 'incomplete'); + } + + /** + * Risky test. + * + * @param \PHPUnit\Framework\Test $test + * @param \Exception $e + * @param float $time + */ + public function addRiskyTest(Test $test, \Exception $e, $time) { + $this->doAddSkipped($test, 'risky'); + } + + /** + * Skipped test. + * + * @param \PHPUnit\Framework\Test $test + * @param \Exception $e + * @param float $time + */ + public function addSkippedTest(Test $test, \Exception $e, $time) { + $this->doAddSkipped($test, 'skipped'); + } + + /** + * A testsuite started. + * + * @param \PHPUnit\Framework\TestSuite $suite + */ + public function startTestSuite(TestSuite $suite) { + $testSuite = $this->document->createElement('testsuite'); + $testSuite->setAttribute('name', $suite->getName()); + if (class_exists($suite->getName(), FALSE)) { + try { + $class = new \ReflectionClass($suite->getName()); + $testSuite->setAttribute('file', $class->getFileName()); + } catch (\ReflectionException $e) { + // Do nothing. } } - - /** - * An error occurred. - * - * @param Test $test - * @param \Exception $e - * @param float $time - */ - public function addError(Test $test, \Exception $e, $time) - { - $this->doAddFault($test, $e, $time, 'error'); - $this->testSuiteErrors[$this->testSuiteLevel]++; + if ($this->testSuiteLevel > 0) { + $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite); + } else { + $this->root->appendChild($testSuite); } - - /** - * A warning occurred. - * - * @param Test $test - * @param Warning $e - * @param float $time - */ - public function addWarning(Test $test, Warning $e, $time) - { - $this->doAddFault($test, $e, $time, 'warning'); - $this->testSuiteFailures[$this->testSuiteLevel]++; + $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; + } + + /** + * A testsuite ended. + * + * @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])); + 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]; } - - /** - * A failure occurred. - * - * @param Test $test - * @param AssertionFailedError $e - * @param float $time - */ - public function addFailure(Test $test, AssertionFailedError $e, $time) - { - $this->doAddFault($test, $e, $time, 'failure'); - $this->testSuiteFailures[$this->testSuiteLevel]++; + $this->testSuiteLevel--; + } + + /** + * A test started. + * + * @param \PHPUnit\Framework\Test $test + */ + public function startTest(Test $test) { + $testCase = $this->document->createElement('testcase'); + $testCase->setAttribute('name', $test->getName()); + + if ($test instanceof TestCase) { + $class = new \ReflectionClass($test); + $methodName = $test->getName(!$test->usesDataProvider()); + + if ($class->hasMethod($methodName)) { + $method = $class->getMethod($methodName); + + $testCase->setAttribute('class', $class->getName()); + $testCase->setAttribute('classname', str_replace('\\', '.', $class->getName())); + $testCase->setAttribute('file', $class->getFileName()); + $testCase->setAttribute('line', $method->getStartLine()); + } } - /** - * Incomplete test. - * - * @param Test $test - * @param \Exception $e - * @param float $time - */ - public function addIncompleteTest(Test $test, \Exception $e, $time) - { - $this->doAddSkipped($test, 'incomplete'); + $this->currentTestCase = $testCase; + } + + /** + * A test ended. + * + * @param \PHPUnit\Framework\Test $test + * @param float $time + */ + public function endTest(Test $test, $time) { + if ($test instanceof TestCase) { + $num_assertions = $test->getNumAssertions(); + $this->testSuiteAssertions[$this->testSuiteLevel] += $num_assertions; + + $this->currentTestCase->setAttribute('assertions', $num_assertions); } - /** - * Risky test. - * - * @param Test $test - * @param \Exception $e - * @param float $time - */ - public function addRiskyTest(Test $test, \Exception $e, $time) - { - if ($this->currentTestCase === null) { - return; - } - $this->doAddSkipped($test, 'risky'); - } + $this->currentTestCase->setAttribute('time', sprintf('%F', $time)); - /** - * Skipped test. - * - * @param Test $test - * @param \Exception $e - * @param float $time - */ - public function addSkippedTest(Test $test, \Exception $e, $time) - { - $this->doAddSkipped($test, 'skipped'); - } + $this->testSuites[$this->testSuiteLevel]->appendChild( + $this->currentTestCase + ); - /** - * A testsuite started. - * - * @param TestSuite $suite - */ - public function startTestSuite(TestSuite $suite) - { - $testSuite = $this->document->createElement('testsuite'); - $testSuite->setAttribute('name', $suite->getName()); - if (\class_exists($suite->getName(), false)) { - try { - $class = new \ReflectionClass($suite->getName()); - $testSuite->setAttribute('file', $class->getFileName()); - } catch (\ReflectionException $e) { - } - } - if ($this->testSuiteLevel > 0) { - $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite); - } 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->testSuiteTests[$this->testSuiteLevel]++; + $this->testSuiteTimes[$this->testSuiteLevel] += $time; - /** - * A testsuite ended. - * - * @param 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]) - ); - 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]; - } - $this->testSuiteLevel--; - } + if (method_exists($test, 'hasOutput') && $test->hasOutput()) { + $systemOut = $this->document->createElement('system-out', Xml::prepareString($test->getActualOutput())); - /** - * A test started. - * - * @param Test $test - */ - public function startTest(Test $test) - { - $testCase = $this->document->createElement('testcase'); - $testCase->setAttribute('name', $test->getName()); - - if ($test instanceof TestCase) { - $class = new \ReflectionClass($test); - $methodName = $test->getName(!$test->usesDataProvider()); - - if ($class->hasMethod($methodName)) { - $method = $class->getMethod($methodName); - - $testCase->setAttribute('class', $class->getName()); - $testCase->setAttribute('classname', str_replace('\\', '.', $class->getName())); - $testCase->setAttribute('file', $class->getFileName()); - $testCase->setAttribute('line', $method->getStartLine()); - } - } - - $this->currentTestCase = $testCase; + $this->currentTestCase->appendChild($systemOut); } - /** - * A test ended. - * - * @param Test $test - * @param float $time - */ - public function endTest(Test $test, $time) - { - if ($test instanceof TestCase) { - $numAssertions = $test->getNumAssertions(); - $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions; - - $this->currentTestCase->setAttribute( - 'assertions', - $numAssertions - ); - } - - $this->currentTestCase->setAttribute( - 'time', - sprintf('%F', $time) - ); - - $this->testSuites[$this->testSuiteLevel]->appendChild( - $this->currentTestCase - ); - - $this->testSuiteTests[$this->testSuiteLevel]++; - $this->testSuiteTimes[$this->testSuiteLevel] += $time; - - if (method_exists($test, 'hasOutput') && $test->hasOutput()) { - $systemOut = $this->document->createElement( - 'system-out', - Xml::prepareString($test->getActualOutput()) - ); - - $this->currentTestCase->appendChild($systemOut); - } - - $this->currentTestCase = null; + $this->currentTestCase = NULL; + } + + /** + * Returns the XML as a string. + * + * @return string + */ + public function getXML() { + return $this->document->saveXML(); + } + + /** + * Method which generalizes addError() and addFailure() + * + * @param \PHPUnit\Framework\Test $test + * @param \Exception $e + * @param float $time + * @param string $type + */ + private function doAddFault(Test $test, \Exception $e, $time, $type) { + if ($this->currentTestCase === NULL) { + return; } - /** - * Returns the XML as a string. - * - * @return string - */ - public function getXML() - { - return $this->document->saveXML(); + if ($test instanceof SelfDescribing) { + $buffer = $test->toString() . "\n"; + } else { + $buffer = ''; } - /** - * Method which generalizes addError() and addFailure() - * - * @param Test $test - * @param \Exception $e - * @param float $time - * @param string $type - */ - private function doAddFault(Test $test, \Exception $e, $time, $type) - { - if ($this->currentTestCase === null) { - return; - } - - if ($test instanceof SelfDescribing) { - $buffer = $test->toString() . "\n"; - } else { - $buffer = ''; - } - - $buffer .= TestFailure::exceptionToString($e) . "\n" . - Filter::getFilteredStacktrace($e); - - $fault = $this->document->createElement( - $type, - Xml::prepareString($buffer) - ); - - if ($e instanceof ExceptionWrapper) { - $fault->setAttribute('type', $e->getClassName()); - } else { - $fault->setAttribute('type', \get_class($e)); - } - - $this->currentTestCase->appendChild($fault); + $buffer .= TestFailure::exceptionToString($e) . "\n" . Filter::getFilteredStacktrace($e); + + $fault = $this->document->createElement($type, Xml::prepareString($buffer)); + + if ($e instanceof ExceptionWrapper) { + $fault->setAttribute('type', $e->getClassName()); + } else { + $fault->setAttribute('type', get_class($e)); } - private function doAddSkipped(Test $test, $message) - { - if ($this->currentTestCase === null) { - return; - } + $this->currentTestCase->appendChild($fault); + } + + /** + * Method that generalizes unproductive tests. + * + * @see ::addSkippedTest + * @see ::addIncompleteTest + * @see ::addRiskyTest + * + * @param \PHPUnit\Framework\Test $test + * @param string $message + */ + private function doAddSkipped(Test $test, $message) { + if ($this->currentTestCase === NULL) { + return; + } - $skipped = $this->document->createElement('skipped'); - $skipped->setAttribute('message', $message); - $this->currentTestCase->appendChild($skipped); + $skipped = $this->document->createElement('skipped'); + $skipped->setAttribute('message', $message); + $this->currentTestCase->appendChild($skipped); - switch ($message) { - case 'risky': - $this->testSuiteRisky[$this->testSuiteLevel]++; - break; + switch ($message) { + case 'risky': + $this->testSuiteRisky[$this->testSuiteLevel]++; + break; - case 'skipped': - $this->testSuiteSkipped[$this->testSuiteLevel]++; - break; + case 'skipped': + $this->testSuiteSkipped[$this->testSuiteLevel]++; + break; - case 'incomplete': - $this->testSuiteIncomplete[$this->testSuiteLevel]++; - break; + case 'incomplete': + $this->testSuiteIncomplete[$this->testSuiteLevel]++; + break; - } } + } + } diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index f49a8c42bc..d5c5beae5e 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -60,7 +60,7 @@ function simpletest_theme() { 'skipped' => 0, 'incomplete' => 0, 'exception' => 0, - 'debug' => 0 + 'debug' => 0, ], ], ]; @@ -451,7 +451,7 @@ function _simpletest_batch_operation($test_list_init, $test_id, &$context) { '#skipped' => 0, '#incomplete' => 0, '#exception' => 0, - '#debug' => 0 + '#debug' => 0, ]; } else {