fix: missing things on junit

This commit is contained in:
Nuno Maduro
2024-01-20 13:21:57 +00:00
parent fef02594db
commit b9d2be87a2
8 changed files with 63 additions and 26 deletions

View File

@ -107,7 +107,8 @@
"Pest\\Plugins\\Snapshot", "Pest\\Plugins\\Snapshot",
"Pest\\Plugins\\Verbose", "Pest\\Plugins\\Verbose",
"Pest\\Plugins\\Version", "Pest\\Plugins\\Version",
"Pest\\Plugins\\Parallel" "Pest\\Plugins\\Parallel",
"Pest\\Plugins\\JUnit"
] ]
}, },
"phpstan": { "phpstan": {

View File

@ -16,7 +16,6 @@ use Pest\Logging\JUnit\Subscriber\TestRunnerExecutionFinishedSubscriber;
use Pest\Logging\JUnit\Subscriber\TestSkippedSubscriber; use Pest\Logging\JUnit\Subscriber\TestSkippedSubscriber;
use Pest\Logging\JUnit\Subscriber\TestSuiteFinishedSubscriber; use Pest\Logging\JUnit\Subscriber\TestSuiteFinishedSubscriber;
use Pest\Logging\JUnit\Subscriber\TestSuiteStartedSubscriber; use Pest\Logging\JUnit\Subscriber\TestSuiteStartedSubscriber;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\EventFacadeIsSealedException; use PHPUnit\Event\EventFacadeIsSealedException;
use PHPUnit\Event\Facade; use PHPUnit\Event\Facade;
use PHPUnit\Event\InvalidArgumentException; use PHPUnit\Event\InvalidArgumentException;
@ -33,14 +32,12 @@ use PHPUnit\Event\TestSuite\Started;
use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Event\UnknownSubscriberTypeException;
use PHPUnit\TextUI\Output\Printer; use PHPUnit\TextUI\Output\Printer;
use PHPUnit\Util\Xml; use PHPUnit\Util\Xml;
use Symfony\Component\Console\Output\OutputInterface;
/** /**
* @internal * @internal
*/ */
final class JUnitLogger final class JUnitLogger
{ {
private DOMDocument $document; private DOMDocument $document;
private DOMElement $root; private DOMElement $root;
@ -76,7 +73,7 @@ final class JUnitLogger
private array $testSuiteSkipped = [0]; private array $testSuiteSkipped = [0];
/** /**
* @psalm-var array<int,int> * @psalm-var array<int,int|float>
*/ */
private array $testSuiteTimes = [0]; private array $testSuiteTimes = [0];
@ -94,7 +91,6 @@ final class JUnitLogger
*/ */
public function __construct( public function __construct(
private readonly Printer $printer, private readonly Printer $printer,
private readonly OutputInterface $output,
private readonly Converter $converter, private readonly Converter $converter,
) { ) {
$this->registerSubscribers(); $this->registerSubscribers();
@ -103,7 +99,7 @@ final class JUnitLogger
public function flush(): void public function flush(): void
{ {
$this->printer->print($this->document->saveXML()); $this->printer->print((string) $this->document->saveXML());
$this->printer->flush(); $this->printer->flush();
} }
@ -240,8 +236,8 @@ final class JUnitLogger
*/ */
private function handleFinish(Info $telemetryInfo, int $numberOfAssertionsPerformed): void private function handleFinish(Info $telemetryInfo, int $numberOfAssertionsPerformed): void
{ {
assert($this->currentTestCase !== null); assert($this->currentTestCase instanceof \DOMElement);
assert($this->time !== null); assert($this->time instanceof \PHPUnit\Event\Telemetry\HRTime);
$time = $telemetryInfo->time()->duration($this->time)->asFloat(); $time = $telemetryInfo->time()->duration($this->time)->asFloat();
@ -292,8 +288,6 @@ final class JUnitLogger
private function createDocument(): void private function createDocument(): void
{ {
$this->output->writeln('Start Junit');
$this->document = new DOMDocument('1.0', 'UTF-8'); $this->document = new DOMDocument('1.0', 'UTF-8');
$this->document->formatOutput = true; $this->document->formatOutput = true;
@ -311,7 +305,7 @@ final class JUnitLogger
$this->createTestCase($event); $this->createTestCase($event);
} }
assert($this->currentTestCase !== null); assert($this->currentTestCase instanceof \DOMElement);
$throwable = $event->throwable(); $throwable = $event->throwable();
@ -349,7 +343,7 @@ final class JUnitLogger
$this->createTestCase($event); $this->createTestCase($event);
} }
assert($this->currentTestCase !== null); assert($this->currentTestCase instanceof \DOMElement);
$skipped = $this->document->createElement('skipped'); $skipped = $this->document->createElement('skipped');
@ -379,8 +373,6 @@ final class JUnitLogger
$testCase->setAttribute('file', $file); $testCase->setAttribute('file', $file);
if ($test->isTestMethod()) { if ($test->isTestMethod()) {
assert($test instanceof TestMethod);
$className = $this->converter->getTrimmedTestClassName($test); $className = $this->converter->getTrimmedTestClassName($test);
$testCase->setAttribute('class', $className); $testCase->setAttribute('class', $className);
$testCase->setAttribute('classname', str_replace('\\', '.', $className)); $testCase->setAttribute('classname', str_replace('\\', '.', $className));

View File

@ -320,7 +320,7 @@ final class Expectation
public function toHaveProperties(iterable $names, string $message = ''): self public function toHaveProperties(iterable $names, string $message = ''): self
{ {
foreach ($names as $name => $value) { foreach ($names as $name => $value) {
is_int($name) ? $this->toHaveProperty($value, message: $message) : $this->toHaveProperty($name, $value, $message); is_int($name) ? $this->toHaveProperty($value, message: $message) : $this->toHaveProperty($name, $value, $message); // @phpstan-ignore-line
} }
return $this; return $this;

View File

@ -31,10 +31,9 @@ final class Cache implements HandlesArguments
public function handleArguments(array $arguments): array public function handleArguments(array $arguments): array
{ {
if (! $this->hasArgument('--cache-directory', $arguments)) { if (! $this->hasArgument('--cache-directory', $arguments)) {
$arguments = $this->pushArgument( $arguments = $this->pushArgument('--cache-directory', $arguments);
sprintf('--cache-directory=%s', realpath(self::TEMPORARY_FOLDER)),
$arguments $arguments = $this->pushArgument((string) realpath(self::TEMPORARY_FOLDER), $arguments);
);
} }
if (! $this->hasArgument('--parallel', $arguments)) { if (! $this->hasArgument('--parallel', $arguments)) {

44
src/Plugins/JUnit.php Normal file
View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugins\Concerns\HandleArguments;
/**
* @internal
*/
final class JUnit implements HandlesArguments
{
use HandleArguments;
/**
* Handles the arguments, adding the cache directory and the cache result arguments.
*/
public function handleArguments(array $arguments): array
{
if (! $this->hasArgument('--log-junit', $arguments)) {
return $arguments;
}
$logUnitArgument = null;
$arguments = array_filter($arguments, function (string $argument) use (&$logUnitArgument): bool {
if (str_starts_with($argument, '--log-junit')) {
$logUnitArgument = $argument;
return false;
}
return true;
});
assert(is_string($logUnitArgument));
$arguments[] = $logUnitArgument;
return array_values($arguments);
}
}

View File

@ -13,7 +13,6 @@ use PHPUnit\Event\TestRunner\ConfiguredSubscriber;
use PHPUnit\TextUI\Configuration\Configuration; use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\TextUI\Output\DefaultPrinter; use PHPUnit\TextUI\Output\DefaultPrinter;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/** /**
* @internal * @internal
@ -25,7 +24,6 @@ final class EnsureJunitEnabled implements ConfiguredSubscriber
*/ */
public function __construct( public function __construct(
private readonly InputInterface $input, private readonly InputInterface $input,
private readonly OutputInterface $output,
private readonly TestSuite $testSuite, private readonly TestSuite $testSuite,
) { ) {
} }
@ -39,9 +37,11 @@ final class EnsureJunitEnabled implements ConfiguredSubscriber
return; return;
} }
$configuration = Container::getInstance()->get(Configuration::class);
assert($configuration instanceof Configuration);
new JUnitLogger( new JUnitLogger(
DefaultPrinter::from(Container::getInstance()->get(Configuration::class)->logfileJunit()), DefaultPrinter::from($configuration->logfileJunit()),
$this->output,
new Converter($this->testSuite->rootPath), new Converter($this->testSuite->rootPath),
); );
} }

View File

@ -1,5 +1,5 @@
Pest Testing Framework 2.31.0. Pest Testing Framework 2.32.0.
USAGE: pest <file> [options] USAGE: pest <file> [options]
@ -84,6 +84,7 @@
--reverse-list .............................. Print defects in reverse order --reverse-list .............................. Print defects in reverse order
--teamcity . Replace default progress and result output with TeamCity format --teamcity . Replace default progress and result output with TeamCity format
--testdox ................ Replace default result output with TestDox format --testdox ................ Replace default result output with TestDox format
--debug Replace default progress and result output with debugging information
--compact ................ Replace default result output with Compact format --compact ................ Replace default result output with Compact format
LOGGING OPTIONS: LOGGING OPTIONS:

View File

@ -1,3 +1,3 @@
Pest Testing Framework 2.31.0. Pest Testing Framework 2.32.0.