diff --git a/.psalm/baseline.xml b/.psalm/baseline.xml index 1743b199ce8..918f8bc639d 100644 --- a/.psalm/baseline.xml +++ b/.psalm/baseline.xml @@ -545,18 +545,6 @@ DefaultPrinter::from( $configuration->logfileTeamcity() ) - EventFacade::registerTracer( - new EventLogger( - $configuration->logEventsText(), - false - ) - ) - EventFacade::registerTracer( - new EventLogger( - $configuration->logEventsVerboseText(), - true - ) - ) OutputFacade::printerFor($configuration->logfileJunit()) OutputFacade::printerFor($configuration->logfileJunit()) atLeastVersion @@ -571,26 +559,6 @@ logEventsVerboseText logfileJunit logfileTeamcity - new JunitXmlLogger( - OutputFacade::printerFor($configuration->logfileJunit()), - ) - new JunitXmlLogger( - OutputFacade::printerFor($configuration->logfileJunit()), - ) - new ResultCacheHandler($cache) - new ResultCacheHandler($cache) - new TeamCityLogger( - DefaultPrinter::from( - $configuration->logfileTeamcity() - ) - ) - new TeamCityLogger( - DefaultPrinter::from( - $configuration->logfileTeamcity() - ) - ) - new TestDoxResultCollector - new TestDoxResultCollector include_once $filename @@ -803,18 +771,6 @@ DefaultPrinter::standardOutput() DefaultPrinter::standardOutput() DefaultPrinter::standardOutput() - new DefaultProgressPrinter( - self::$printer, - $configuration->colors(), - $configuration->columns() - ) - new DefaultProgressPrinter( - self::$printer, - $configuration->colors(), - $configuration->columns() - ) - new TeamCityLogger(DefaultPrinter::standardOutput()) - new TeamCityLogger(DefaultPrinter::standardOutput()) resourceUsageSinceStartOfRequest diff --git a/src/Logging/JUnit/JunitXmlLogger.php b/src/Logging/JUnit/JunitXmlLogger.php index 86ba86385c8..ccdc59574f8 100644 --- a/src/Logging/JUnit/JunitXmlLogger.php +++ b/src/Logging/JUnit/JunitXmlLogger.php @@ -21,8 +21,6 @@ use PHPUnit\Event\Code\Test; use PHPUnit\Event\Code\TestMethod; use PHPUnit\Event\Code\Throwable; -use PHPUnit\Event\EventFacadeIsSealedException; -use PHPUnit\Event\Facade; use PHPUnit\Event\InvalidArgumentException; use PHPUnit\Event\Telemetry\HRTime; use PHPUnit\Event\Test\Errored; @@ -33,7 +31,6 @@ use PHPUnit\Event\Test\Skipped; use PHPUnit\Event\TestData\NoDataSetFromDataProviderException; use PHPUnit\Event\TestSuite\Started; -use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\TextUI\Output\Printer; use PHPUnit\Util\Xml; use ReflectionClass; @@ -86,15 +83,10 @@ final class JunitXmlLogger private ?DOMElement $currentTestCase = null; private ?HRTime $time = null; - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ public function __construct(Printer $printer) { $this->printer = $printer; - $this->registerSubscribers(); $this->createDocument(); } @@ -261,13 +253,10 @@ public function testFailed(Failed $event): void $this->testSuiteFailures[$this->testSuiteLevel]++; } - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - private function registerSubscribers(): void + /** @return list<\PHPUnit\Event\Subscriber> */ + public function provideSubscribers(): array { - Facade::registerSubscribers( + return [ new TestSuiteStartedSubscriber($this), new TestSuiteFinishedSubscriber($this), new TestPreparedSubscriber($this), @@ -277,7 +266,7 @@ private function registerSubscribers(): void new TestMarkedIncompleteSubscriber($this), new TestSkippedSubscriber($this), new TestRunnerExecutionFinishedSubscriber($this), - ); + ]; } private function createDocument(): void diff --git a/src/Logging/TeamCity/TeamCityLogger.php b/src/Logging/TeamCity/TeamCityLogger.php index b37a99d8751..6c770d21542 100644 --- a/src/Logging/TeamCity/TeamCityLogger.php +++ b/src/Logging/TeamCity/TeamCityLogger.php @@ -20,8 +20,6 @@ use PHPUnit\Event\Code\TestMethod; use PHPUnit\Event\Code\Throwable; use PHPUnit\Event\Event; -use PHPUnit\Event\EventFacadeIsSealedException; -use PHPUnit\Event\Facade; use PHPUnit\Event\InvalidArgumentException; use PHPUnit\Event\Telemetry\HRTime; use PHPUnit\Event\Test\ConsideredRisky; @@ -35,7 +33,6 @@ use PHPUnit\Event\TestSuite\Started as TestSuiteStarted; use PHPUnit\Event\TestSuite\TestSuiteForTestClass; use PHPUnit\Event\TestSuite\TestSuiteForTestMethodWithDataProvider; -use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Framework\Exception as FrameworkException; use PHPUnit\TextUI\Output\Printer; @@ -49,15 +46,10 @@ final class TeamCityLogger private ?HRTime $time = null; private ?int $flowId; - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ public function __construct(Printer $printer) { $this->printer = $printer; - $this->registerSubscribers(); $this->setFlowId(); } @@ -271,13 +263,10 @@ public function flush(): void $this->printer->flush(); } - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - private function registerSubscribers(): void + /** @return list<\PHPUnit\Event\Subscriber> */ + public function provideSubscribers(): array { - Facade::registerSubscribers( + return [ new TestSuiteStartedSubscriber($this), new TestSuiteFinishedSubscriber($this), new TestPreparedSubscriber($this), @@ -288,7 +277,7 @@ private function registerSubscribers(): void new TestSkippedSubscriber($this), new TestConsideredRiskySubscriber($this), new TestRunnerExecutionFinishedSubscriber($this), - ); + ]; } private function setFlowId(): void diff --git a/src/Logging/TestDox/TestMethod/TestResultCollector.php b/src/Logging/TestDox/TestMethod/TestResultCollector.php index 5b035cec2ca..03aac8fd42f 100644 --- a/src/Logging/TestDox/TestMethod/TestResultCollector.php +++ b/src/Logging/TestDox/TestMethod/TestResultCollector.php @@ -12,8 +12,6 @@ use function assert; use PHPUnit\Event\Code\TestMethod; use PHPUnit\Event\Code\Throwable; -use PHPUnit\Event\EventFacadeIsSealedException; -use PHPUnit\Event\Facade; use PHPUnit\Event\InvalidArgumentException; use PHPUnit\Event\Telemetry\HRTime; use PHPUnit\Event\Test\ConsideredRisky; @@ -31,7 +29,6 @@ use PHPUnit\Event\Test\Skipped; use PHPUnit\Event\Test\TestProxyCreated; use PHPUnit\Event\Test\TestStubCreated; -use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Framework\TestStatus\TestStatus; use PHPUnit\Logging\TestDox\TestResult as TestDoxTestMethod; use SoapClient; @@ -54,15 +51,6 @@ final class TestResultCollector */ private array $testDoubles = []; - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - public function __construct() - { - $this->registerSubscribers(); - } - /** * @psalm-return array */ @@ -182,13 +170,10 @@ public function testFinished(Finished $event): void $this->testDoubles = []; } - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - private function registerSubscribers(): void + /** @return list<\PHPUnit\Event\Subscriber> */ + public function provideSubscribers(): array { - Facade::registerSubscribers( + return [ new TestConsideredRiskySubscriber($this), new TestCreatedMockObjectForAbstractClassSubscriber($this), new TestCreatedMockObjectForTraitSubscriber($this), @@ -204,6 +189,6 @@ private function registerSubscribers(): void new TestPassedSubscriber($this), new TestPreparedSubscriber($this), new TestSkippedSubscriber($this), - ); + ]; } } diff --git a/src/Runner/ResultCache/ResultCacheHandler.php b/src/Runner/ResultCache/ResultCacheHandler.php index 58a4bafca9e..f9ef4c37b64 100644 --- a/src/Runner/ResultCache/ResultCacheHandler.php +++ b/src/Runner/ResultCache/ResultCacheHandler.php @@ -11,8 +11,6 @@ use function round; use PHPUnit\Event\Event; -use PHPUnit\Event\EventFacadeIsSealedException; -use PHPUnit\Event\Facade; use PHPUnit\Event\Telemetry\HRTime; use PHPUnit\Event\Test\ConsideredRisky; use PHPUnit\Event\Test\Errored; @@ -21,7 +19,6 @@ use PHPUnit\Event\Test\MarkedIncomplete; use PHPUnit\Event\Test\Prepared; use PHPUnit\Event\Test\Skipped; -use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Framework\InvalidArgumentException; use PHPUnit\Framework\TestStatus\TestStatus; @@ -34,15 +31,9 @@ final class ResultCacheHandler private ?HRTime $time = null; private int $testSuite = 0; - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ public function __construct(ResultCache $cache) { $this->cache = $cache; - - $this->registerSubscribers(); } public function testSuiteStarted(): void @@ -121,6 +112,22 @@ public function testFinished(Finished $event): void $this->time = null; } + /** @return list<\PHPUnit\Event\Subscriber> */ + public function provideSubscribers(): array + { + return [ + new TestSuiteStartedSubscriber($this), + new TestSuiteFinishedSubscriber($this), + new TestPreparedSubscriber($this), + new TestMarkedIncompleteSubscriber($this), + new TestConsideredRiskySubscriber($this), + new TestErroredSubscriber($this), + new TestFailedSubscriber($this), + new TestSkippedSubscriber($this), + new TestFinishedSubscriber($this), + ]; + } + /** * @throws \PHPUnit\Event\InvalidArgumentException * @throws InvalidArgumentException @@ -133,23 +140,4 @@ private function duration(Event $event): float return round($event->telemetryInfo()->time()->duration($this->time)->asFloat(), 3); } - - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - private function registerSubscribers(): void - { - Facade::registerSubscribers( - new TestSuiteStartedSubscriber($this), - new TestSuiteFinishedSubscriber($this), - new TestPreparedSubscriber($this), - new TestMarkedIncompleteSubscriber($this), - new TestConsideredRiskySubscriber($this), - new TestErroredSubscriber($this), - new TestFailedSubscriber($this), - new TestSkippedSubscriber($this), - new TestFinishedSubscriber($this), - ); - } } diff --git a/src/Runner/TestResult/Collector.php b/src/Runner/TestResult/Collector.php index f39528ada73..a99bb185ac8 100644 --- a/src/Runner/TestResult/Collector.php +++ b/src/Runner/TestResult/Collector.php @@ -12,8 +12,6 @@ use function assert; use function str_contains; use PHPUnit\Event\Code\TestMethod; -use PHPUnit\Event\EventFacadeIsSealedException; -use PHPUnit\Event\Facade; use PHPUnit\Event\Test\BeforeFirstTestMethodErrored; use PHPUnit\Event\Test\ConsideredRisky; use PHPUnit\Event\Test\DeprecationTriggered; @@ -40,7 +38,6 @@ use PHPUnit\Event\TestSuite\Started as TestSuiteStarted; use PHPUnit\Event\TestSuite\TestSuiteForTestClass; use PHPUnit\Event\TestSuite\TestSuiteForTestMethodWithDataProvider; -use PHPUnit\Event\UnknownSubscriberTypeException; /** * @internal This class is not covered by the backward compatibility promise for PHPUnit @@ -143,13 +140,10 @@ final class Collector */ private array $testRunnerTriggeredDeprecationEvents = []; - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - public function __construct() + /** @return list<\PHPUnit\Event\Subscriber> */ + public function provideSubscribers(): array { - Facade::registerSubscribers( + return [ new ExecutionStartedSubscriber($this), new TestSuiteSkippedSubscriber($this), new TestSuiteStartedSubscriber($this), @@ -174,7 +168,7 @@ public function __construct() new TestTriggeredWarningSubscriber($this), new TestRunnerTriggeredDeprecationSubscriber($this), new TestRunnerTriggeredWarningSubscriber($this), - ); + ]; } public function result(): TestResult diff --git a/src/Runner/TestResult/Facade.php b/src/Runner/TestResult/Facade.php index e4d964a5123..392d35e9173 100644 --- a/src/Runner/TestResult/Facade.php +++ b/src/Runner/TestResult/Facade.php @@ -10,6 +10,7 @@ namespace PHPUnit\TestRunner\TestResult; use PHPUnit\Event\EventFacadeIsSealedException; +use PHPUnit\Event\Facade as EventFacade; use PHPUnit\Event\UnknownSubscriberTypeException; /** @@ -98,7 +99,10 @@ public static function hasTestMarkedIncompleteEvents(): bool private static function collector(): Collector { if (self::$collector === null) { - self::$collector = new Collector; + $collector = new Collector; + EventFacade::registerSubscribers(...$collector->provideSubscribers()); + + self::$collector = $collector; } return self::$collector; diff --git a/src/TextUI/Application.php b/src/TextUI/Application.php index b71621a6800..3345dbfc5af 100644 --- a/src/TextUI/Application.php +++ b/src/TextUI/Application.php @@ -17,7 +17,9 @@ use function sprintf; use function trim; use function unlink; +use PHPUnit\Event\EventFacadeIsSealedException; use PHPUnit\Event\Facade as EventFacade; +use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Framework\TestSuite; use PHPUnit\Logging\EventLogger; use PHPUnit\Logging\JUnit\JunitXmlLogger; @@ -451,6 +453,10 @@ private function writeRandomSeedInformation(Printer $printer, Configuration $con } } + /** + * @throws EventFacadeIsSealedException + * @throws UnknownSubscriberTypeException + */ private function registerLogfileWriters(Configuration $configuration): void { if ($configuration->hasLogEventsText()) { @@ -480,37 +486,48 @@ private function registerLogfileWriters(Configuration $configuration): void } if ($configuration->hasLogfileJunit()) { - new JunitXmlLogger( + EventFacade::registerSubscribers(...(new JunitXmlLogger( OutputFacade::printerFor($configuration->logfileJunit()), - ); + ))->provideSubscribers()); } if ($configuration->hasLogfileTeamcity()) { - new TeamCityLogger( + EventFacade::registerSubscribers(...(new TeamCityLogger( DefaultPrinter::from( $configuration->logfileTeamcity() ) - ); + ))->provideSubscribers()); } } + /** + * @throws EventFacadeIsSealedException + * @throws UnknownSubscriberTypeException + */ private function testDoxResultCollector(Configuration $configuration): ?TestDoxResultCollector { if ($configuration->hasLogfileTestdoxHtml() || $configuration->hasLogfileTestdoxText() || $configuration->outputIsTestDox()) { - return new TestDoxResultCollector; + $testResultCollector = new TestDoxResultCollector; + EventFacade::registerSubscribers(...$testResultCollector->provideSubscribers()); + + return $testResultCollector; } return null; } + /** + * @throws EventFacadeIsSealedException + * @throws UnknownSubscriberTypeException + */ private function initializeTestResultCache(Configuration $configuration): ResultCache { if ($configuration->cacheResult()) { $cache = new DefaultResultCache($configuration->testResultCacheFile()); - new ResultCacheHandler($cache); + EventFacade::registerSubscribers(...(new ResultCacheHandler($cache))->provideSubscribers()); return $cache; } diff --git a/src/TextUI/Output/Default/ProgressPrinter/ProgressPrinter.php b/src/TextUI/Output/Default/ProgressPrinter/ProgressPrinter.php index 99bcdd133b1..cf467363ccf 100644 --- a/src/TextUI/Output/Default/ProgressPrinter/ProgressPrinter.php +++ b/src/TextUI/Output/Default/ProgressPrinter/ProgressPrinter.php @@ -14,11 +14,8 @@ use function str_contains; use function str_repeat; use function strlen; -use PHPUnit\Event\EventFacadeIsSealedException; -use PHPUnit\Event\Facade; use PHPUnit\Event\Test\Errored; use PHPUnit\Event\TestRunner\ExecutionStarted; -use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Framework\TestStatus\TestStatus; use PHPUnit\TextUI\Output\Printer; use PHPUnit\Util\Color; @@ -39,17 +36,11 @@ final class ProgressPrinter private ?TestStatus $status = null; private bool $prepared = false; - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ public function __construct(Printer $printer, bool $colors, int $numberOfColumns) { $this->printer = $printer; $this->colors = $colors; $this->numberOfColumns = $numberOfColumns; - - $this->registerSubscribers(); } public function testRunnerExecutionStarted(ExecutionStarted $event): void @@ -153,13 +144,10 @@ public function testFinished(): void $this->prepared = false; } - /** - * @throws EventFacadeIsSealedException - * @throws UnknownSubscriberTypeException - */ - private function registerSubscribers(): void + /** @return list<\PHPUnit\Event\Subscriber> */ + public function provideSubscribers(): array { - Facade::registerSubscribers( + return [ new BeforeTestClassMethodErroredSubscriber($this), new TestConsideredRiskySubscriber($this), new TestErroredSubscriber($this), @@ -177,7 +165,7 @@ private function registerSubscribers(): void new TestTriggeredPhpunitWarningSubscriber($this), new TestTriggeredPhpWarningSubscriber($this), new TestTriggeredWarningSubscriber($this), - ); + ]; } private function updateTestStatus(TestStatus $status): void diff --git a/src/TextUI/Output/Facade.php b/src/TextUI/Output/Facade.php index 6a96cf013a7..f60848e4b18 100644 --- a/src/TextUI/Output/Facade.php +++ b/src/TextUI/Output/Facade.php @@ -9,6 +9,9 @@ */ namespace PHPUnit\TextUI\Output; +use PHPUnit\Event\EventFacadeIsSealedException; +use PHPUnit\Event\Facade as EventFacade; +use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Logging\TeamCity\TeamCityLogger; use PHPUnit\Logging\TestDox\TestResultCollection; use PHPUnit\TestRunner\TestResult\TestResult; @@ -31,16 +34,20 @@ final class Facade private static bool $colors = false; private static bool $defaultProgressPrinter = false; + /** + * @throws EventFacadeIsSealedException + * @throws UnknownSubscriberTypeException + */ public static function init(Configuration $configuration): Printer { self::$printer = self::createPrinter($configuration); if (self::useDefaultProgressPrinter($configuration)) { - new DefaultProgressPrinter( + EventFacade::registerSubscribers(...(new DefaultProgressPrinter( self::$printer, $configuration->colors(), $configuration->columns() - ); + ))->provideSubscribers()); self::$defaultProgressPrinter = true; } @@ -66,7 +73,9 @@ public static function init(Configuration $configuration): Printer } if ($configuration->outputIsTeamCity()) { - new TeamCityLogger(DefaultPrinter::standardOutput()); + EventFacade::registerSubscribers(...(new TeamCityLogger( + DefaultPrinter::standardOutput() + ))->provideSubscribers()); } self::$colors = $configuration->colors();