This commit is contained in:
Adrian Nürnberger
2023-08-20 17:55:17 +02:00
parent e5dc6f0ae2
commit 117694f210
2 changed files with 24 additions and 74 deletions

View File

@ -4,22 +4,13 @@ declare(strict_types=1);
namespace Pest\Logging\JUnit;
use NunoMaduro\Collision\Adapters\Phpunit\State;
use Pest\Exceptions\ShouldNotHappen;
use Pest\Support\StateGenerator;
use Pest\Support\Str;
use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Code\Throwable;
use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;
use PHPUnit\Event\Test\ConsideredRisky;
use PHPUnit\Event\Test\Errored;
use PHPUnit\Event\Test\Failed;
use PHPUnit\Event\Test\MarkedIncomplete;
use PHPUnit\Event\Test\Skipped;
use PHPUnit\Event\TestSuite\TestSuite;
use PHPUnit\Framework\Exception as FrameworkException;
use PHPUnit\TestRunner\TestResult\TestResult as PhpUnitTestResult;
/**
* @internal
@ -28,15 +19,12 @@ final class Converter
{
private const PREFIX = 'P\\';
private readonly StateGenerator $stateGenerator;
/**
* Creates a new instance of the Converter.
*/
public function __construct(
private readonly string $rootPath,
) {
$this->stateGenerator = new StateGenerator();
}
/**
@ -73,6 +61,14 @@ final class Converter
return "$relativePath::$description";
}
/**
* Gets the trimmed test class name.
*/
public function getTrimmedTestClassName(TestMethod $test): string
{
return Str::after($test->className(), self::PREFIX);
}
/**
* Gets the exception message.
*/
@ -176,14 +172,6 @@ final class Converter
return $this->toRelativePath($path);
}
/**
* Gets the test suite size.
*/
public function getTestSuiteSize(TestSuite $testSuite): int
{
return $testSuite->count();
}
/**
* Transforms the given path in relative path.
*/
@ -192,37 +180,4 @@ final class Converter
// Remove cwd from the path.
return str_replace("$this->rootPath".DIRECTORY_SEPARATOR, '', $path);
}
/**
* Get the test result.
*/
public function getStateFromResult(PhpUnitTestResult $result): State
{
$events = [
...$result->testErroredEvents(),
...$result->testFailedEvents(),
...$result->testSkippedEvents(),
...array_merge(...array_values($result->testConsideredRiskyEvents())),
...$result->testMarkedIncompleteEvents(),
];
$numberOfNotPassedTests = count(
array_unique(
array_map(
function (BeforeFirstTestMethodErrored|Errored|Failed|Skipped|ConsideredRisky|MarkedIncomplete $event): string {
if ($event instanceof BeforeFirstTestMethodErrored) {
return $event->testClassName();
}
return $this->getTestCaseLocation($event->test());
},
$events
)
)
);
$numberOfPassedTests = $result->numberOfTestsRun() - $numberOfNotPassedTests;
return $this->stateGenerator->fromPhpUnitTestResult($numberOfPassedTests, $result);
}
}

View File

@ -15,7 +15,6 @@ use Pest\Logging\JUnit\Subscriber\TestRunnerExecutionFinishedSubscriber;
use Pest\Logging\JUnit\Subscriber\TestSkippedSubscriber;
use Pest\Logging\JUnit\Subscriber\TestSuiteFinishedSubscriber;
use Pest\Logging\JUnit\Subscriber\TestSuiteStartedSubscriber;
use Pest\TestSuite;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\EventFacadeIsSealedException;
use PHPUnit\Event\Facade;
@ -88,7 +87,7 @@ final class JUnitLogger
private bool $prepared = false;
/**
/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
@ -271,7 +270,7 @@ final class JUnitLogger
$this->prepared = false;
}
/**
/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
@ -315,16 +314,16 @@ final class JUnitLogger
assert($this->currentTestCase !== null);
$throwable = $event->throwable();
$testName = $this->converter->getTestCaseMethodName($event->test());
// $message = $this->converter->getExceptionMessage($event->throwable());
// $details = $this->converter->getExceptionDetails($event->throwable());
$message = $this->converter->getExceptionMessage($throwable);
$details = $this->converter->getExceptionDetails($throwable);
$buffer = $testName;
$throwable = $event->throwable();
$buffer .= trim(
$throwable->description().PHP_EOL.
$throwable->stackTrace(),
$message.PHP_EOL.
$details,
);
$fault = $this->document->createElement(
@ -374,22 +373,18 @@ final class JUnitLogger
{
$testCase = $this->document->createElement('testcase');
$file = $this->converter->getTestCaseLocation($event->test());
$test = $event->test();
$file = $this->converter->getTestCaseLocation($test);
$testCase->setAttribute('name', $this->converter->getTestCaseMethodName($event->test()));
// $testCase->setAttribute('name', $event->test()->name());
$testCase->setAttribute('name', $this->converter->getTestCaseMethodName($test));
$testCase->setAttribute('file', $file);
// $testCase->setAttribute('file', $event->test()->file());
if ($event->test()->isTestMethod()) {
assert($event->test() instanceof TestMethod);
if ($test->isTestMethod()) {
assert($test instanceof TestMethod);
//dd(TestSuite::getInstance()->tests->get($file));
// add classname, and line to this
$testCase->setAttribute('line', (string) $event->test()->line()); //@todo figure out how to get line number in original pest file
$testCase->setAttribute('class', $event->test()->name());
$testCase->setAttribute('classname', str_replace('\\', '.', $event->test()->name()));
$className = $this->converter->getTrimmedTestClassName($test);
$testCase->setAttribute('class', $className);
$testCase->setAttribute('classname', str_replace('\\', '.', $className));
}
$this->currentTestCase = $testCase;