diff --git a/composer.json b/composer.json index f3b2f879..77b14c3e 100644 --- a/composer.json +++ b/composer.json @@ -107,7 +107,8 @@ "Pest\\Plugins\\Snapshot", "Pest\\Plugins\\Verbose", "Pest\\Plugins\\Version", - "Pest\\Plugins\\Parallel" + "Pest\\Plugins\\Parallel", + "Pest\\Plugins\\JUnit" ] }, "phpstan": { diff --git a/src/Logging/JUnit/JUnitLogger.php b/src/Logging/JUnit/JUnitLogger.php index 497fe8ef..ba71f88f 100644 --- a/src/Logging/JUnit/JUnitLogger.php +++ b/src/Logging/JUnit/JUnitLogger.php @@ -16,7 +16,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 PHPUnit\Event\Code\TestMethod; use PHPUnit\Event\EventFacadeIsSealedException; use PHPUnit\Event\Facade; use PHPUnit\Event\InvalidArgumentException; @@ -33,14 +32,12 @@ use PHPUnit\Event\TestSuite\Started; use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\TextUI\Output\Printer; use PHPUnit\Util\Xml; -use Symfony\Component\Console\Output\OutputInterface; /** * @internal */ final class JUnitLogger { - private DOMDocument $document; private DOMElement $root; @@ -76,7 +73,7 @@ final class JUnitLogger private array $testSuiteSkipped = [0]; /** - * @psalm-var array + * @psalm-var array */ private array $testSuiteTimes = [0]; @@ -94,7 +91,6 @@ final class JUnitLogger */ public function __construct( private readonly Printer $printer, - private readonly OutputInterface $output, private readonly Converter $converter, ) { $this->registerSubscribers(); @@ -103,7 +99,7 @@ final class JUnitLogger public function flush(): void { - $this->printer->print($this->document->saveXML()); + $this->printer->print((string) $this->document->saveXML()); $this->printer->flush(); } @@ -240,8 +236,8 @@ final class JUnitLogger */ private function handleFinish(Info $telemetryInfo, int $numberOfAssertionsPerformed): void { - assert($this->currentTestCase !== null); - assert($this->time !== null); + assert($this->currentTestCase instanceof \DOMElement); + assert($this->time instanceof \PHPUnit\Event\Telemetry\HRTime); $time = $telemetryInfo->time()->duration($this->time)->asFloat(); @@ -292,8 +288,6 @@ final class JUnitLogger private function createDocument(): void { - $this->output->writeln('Start Junit'); - $this->document = new DOMDocument('1.0', 'UTF-8'); $this->document->formatOutput = true; @@ -311,7 +305,7 @@ final class JUnitLogger $this->createTestCase($event); } - assert($this->currentTestCase !== null); + assert($this->currentTestCase instanceof \DOMElement); $throwable = $event->throwable(); @@ -349,7 +343,7 @@ final class JUnitLogger $this->createTestCase($event); } - assert($this->currentTestCase !== null); + assert($this->currentTestCase instanceof \DOMElement); $skipped = $this->document->createElement('skipped'); @@ -379,8 +373,6 @@ final class JUnitLogger $testCase->setAttribute('file', $file); if ($test->isTestMethod()) { - assert($test instanceof TestMethod); - $className = $this->converter->getTrimmedTestClassName($test); $testCase->setAttribute('class', $className); $testCase->setAttribute('classname', str_replace('\\', '.', $className)); diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 6a906c81..0d0b4d92 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -320,7 +320,7 @@ final class Expectation public function toHaveProperties(iterable $names, string $message = ''): self { 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; diff --git a/src/Plugins/Cache.php b/src/Plugins/Cache.php index 682b938c..7312691b 100644 --- a/src/Plugins/Cache.php +++ b/src/Plugins/Cache.php @@ -31,10 +31,9 @@ final class Cache implements HandlesArguments public function handleArguments(array $arguments): array { if (! $this->hasArgument('--cache-directory', $arguments)) { - $arguments = $this->pushArgument( - sprintf('--cache-directory=%s', realpath(self::TEMPORARY_FOLDER)), - $arguments - ); + $arguments = $this->pushArgument('--cache-directory', $arguments); + + $arguments = $this->pushArgument((string) realpath(self::TEMPORARY_FOLDER), $arguments); } if (! $this->hasArgument('--parallel', $arguments)) { diff --git a/src/Plugins/JUnit.php b/src/Plugins/JUnit.php new file mode 100644 index 00000000..3ee9bdf7 --- /dev/null +++ b/src/Plugins/JUnit.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/src/Subscribers/EnsureJunitEnabled.php b/src/Subscribers/EnsureJunitEnabled.php index b6f6a339..b57c32e6 100644 --- a/src/Subscribers/EnsureJunitEnabled.php +++ b/src/Subscribers/EnsureJunitEnabled.php @@ -13,7 +13,6 @@ use PHPUnit\Event\TestRunner\ConfiguredSubscriber; use PHPUnit\TextUI\Configuration\Configuration; use PHPUnit\TextUI\Output\DefaultPrinter; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; /** * @internal @@ -25,7 +24,6 @@ final class EnsureJunitEnabled implements ConfiguredSubscriber */ public function __construct( private readonly InputInterface $input, - private readonly OutputInterface $output, private readonly TestSuite $testSuite, ) { } @@ -39,9 +37,11 @@ final class EnsureJunitEnabled implements ConfiguredSubscriber return; } + $configuration = Container::getInstance()->get(Configuration::class); + assert($configuration instanceof Configuration); + new JUnitLogger( - DefaultPrinter::from(Container::getInstance()->get(Configuration::class)->logfileJunit()), - $this->output, + DefaultPrinter::from($configuration->logfileJunit()), new Converter($this->testSuite->rootPath), ); } diff --git a/tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap b/tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap index 87b0ec07..7d08bd34 100644 --- a/tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap +++ b/tests/.pest/snapshots/Visual/Help/visual_snapshot_of_help_command_output.snap @@ -1,5 +1,5 @@ - Pest Testing Framework 2.31.0. + Pest Testing Framework 2.32.0. USAGE: pest [options] @@ -84,6 +84,7 @@ --reverse-list .............................. Print defects in reverse order --teamcity . Replace default progress and result output with TeamCity 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 LOGGING OPTIONS: diff --git a/tests/.pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap b/tests/.pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap index 969fdf6e..b698a1f8 100644 --- a/tests/.pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap +++ b/tests/.pest/snapshots/Visual/Version/visual_snapshot_of_help_command_output.snap @@ -1,3 +1,3 @@ - Pest Testing Framework 2.31.0. + Pest Testing Framework 2.32.0.