This commit is contained in:
Adrian Nürnberger
2024-01-24 21:50:52 +01:00
parent e0f9d0bccf
commit 880b003bee

View File

@ -1,4 +1,6 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
/* /*
* This file is part of PHPUnit. * This file is part of PHPUnit.
* *
@ -7,14 +9,9 @@
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace PHPUnit\Logging\JUnit; namespace PHPUnit\Logging\JUnit;
use function assert;
use function basename;
use function is_int;
use function sprintf;
use function str_replace;
use function trim;
use DOMDocument; use DOMDocument;
use DOMElement; use DOMElement;
use PHPUnit\Event\Code\Test; use PHPUnit\Event\Code\Test;
@ -36,14 +33,24 @@ use PHPUnit\Event\UnknownSubscriberTypeException;
use PHPUnit\TextUI\Output\Printer; use PHPUnit\TextUI\Output\Printer;
use PHPUnit\Util\Xml; use PHPUnit\Util\Xml;
use function assert;
use function basename;
use function is_int;
use function sprintf;
use function str_replace;
use function trim;
/** /**
* @internal This class is not covered by the backward compatibility promise for PHPUnit * @internal This class is not covered by the backward compatibility promise for PHPUnit
*/ */
final class JunitXmlLogger final class JunitXmlLogger
{ {
private readonly Printer $printer; private readonly Printer $printer;
private readonly \Pest\Logging\Converter $converter; // pest-added private readonly \Pest\Logging\Converter $converter; // pest-added
private DOMDocument $document; private DOMDocument $document;
private DOMElement $root; private DOMElement $root;
/** /**
@ -80,10 +87,15 @@ final class JunitXmlLogger
* @psalm-var array<int,int> * @psalm-var array<int,int>
*/ */
private array $testSuiteTimes = [0]; private array $testSuiteTimes = [0];
private int $testSuiteLevel = 0; private int $testSuiteLevel = 0;
private ?DOMElement $currentTestCase = null; private ?DOMElement $currentTestCase = null;
private ?HRTime $time = null; private ?HRTime $time = null;
private bool $prepared = false; private bool $prepared = false;
private bool $preparationFailed = false; private bool $preparationFailed = false;
/** /**
@ -316,7 +328,7 @@ final class JunitXmlLogger
*/ */
private function handleFault(Errored|Failed $event, string $type): void private function handleFault(Errored|Failed $event, string $type): void
{ {
if (!$this->prepared) { if (! $this->prepared) {
$this->createTestCase($event); $this->createTestCase($event);
} }
@ -326,7 +338,7 @@ final class JunitXmlLogger
$throwable = $event->throwable(); $throwable = $event->throwable();
$buffer .= trim( $buffer .= trim(
$this->converter->getExceptionMessage($throwable) . PHP_EOL . // pest-changed $this->converter->getExceptionMessage($throwable).PHP_EOL. // pest-changed
$this->converter->getExceptionDetails($throwable), // pest-changed $this->converter->getExceptionDetails($throwable), // pest-changed
); );
@ -339,7 +351,7 @@ final class JunitXmlLogger
$this->currentTestCase->appendChild($fault); $this->currentTestCase->appendChild($fault);
if (!$this->prepared) { if (! $this->prepared) {
$this->handleFinish($event->telemetryInfo(), 0); $this->handleFinish($event->telemetryInfo(), 0);
} }
} }
@ -349,7 +361,7 @@ final class JunitXmlLogger
*/ */
private function handleIncompleteOrSkipped(MarkedIncomplete|Skipped $event): void private function handleIncompleteOrSkipped(MarkedIncomplete|Skipped $event): void
{ {
if (!$this->prepared) { if (! $this->prepared) {
$this->createTestCase($event); $this->createTestCase($event);
} }
@ -361,7 +373,7 @@ final class JunitXmlLogger
$this->testSuiteSkipped[$this->testSuiteLevel]++; $this->testSuiteSkipped[$this->testSuiteLevel]++;
if (!$this->prepared) { if (! $this->prepared) {
$this->handleFinish($event->telemetryInfo(), 0); $this->handleFinish($event->telemetryInfo(), 0);
} }
} }
@ -396,7 +408,7 @@ final class JunitXmlLogger
assert($test instanceof TestMethod); assert($test instanceof TestMethod);
if (!$test->testData()->hasDataFromDataProvider()) { if (! $test->testData()->hasDataFromDataProvider()) {
return $test->methodName(); return $test->methodName();
} }