Skip to content

Commit

Permalink
Register Facade Subscriber only where state is already global
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Feb 15, 2023
1 parent 3126416 commit 0268da3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 157 deletions.
44 changes: 0 additions & 44 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -545,18 +545,6 @@
<code>DefaultPrinter::from(
$configuration-&gt;logfileTeamcity()
)</code>
<code>EventFacade::registerTracer(
new EventLogger(
$configuration-&gt;logEventsText(),
false
)
)</code>
<code>EventFacade::registerTracer(
new EventLogger(
$configuration-&gt;logEventsVerboseText(),
true
)
)</code>
<code>OutputFacade::printerFor($configuration-&gt;logfileJunit())</code>
<code>OutputFacade::printerFor($configuration-&gt;logfileJunit())</code>
<code>atLeastVersion</code>
Expand All @@ -571,26 +559,6 @@
<code>logEventsVerboseText</code>
<code>logfileJunit</code>
<code>logfileTeamcity</code>
<code>new JunitXmlLogger(
OutputFacade::printerFor($configuration-&gt;logfileJunit()),
)</code>
<code>new JunitXmlLogger(
OutputFacade::printerFor($configuration-&gt;logfileJunit()),
)</code>
<code>new ResultCacheHandler($cache)</code>
<code>new ResultCacheHandler($cache)</code>
<code>new TeamCityLogger(
DefaultPrinter::from(
$configuration-&gt;logfileTeamcity()
)
)</code>
<code>new TeamCityLogger(
DefaultPrinter::from(
$configuration-&gt;logfileTeamcity()
)
)</code>
<code>new TestDoxResultCollector</code>
<code>new TestDoxResultCollector</code>
</MissingThrowsDocblock>
<UnresolvableInclude>
<code>include_once $filename</code>
Expand Down Expand Up @@ -803,18 +771,6 @@
<code>DefaultPrinter::standardOutput()</code>
<code>DefaultPrinter::standardOutput()</code>
<code>DefaultPrinter::standardOutput()</code>
<code>new DefaultProgressPrinter(
self::$printer,
$configuration-&gt;colors(),
$configuration-&gt;columns()
)</code>
<code>new DefaultProgressPrinter(
self::$printer,
$configuration-&gt;colors(),
$configuration-&gt;columns()
)</code>
<code>new TeamCityLogger(DefaultPrinter::standardOutput())</code>
<code>new TeamCityLogger(DefaultPrinter::standardOutput())</code>
<code>resourceUsageSinceStartOfRequest</code>
</MissingThrowsDocblock>
<NullableReturnStatement>
Expand Down
19 changes: 4 additions & 15 deletions src/Logging/JUnit/JunitXmlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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),
Expand All @@ -277,7 +266,7 @@ private function registerSubscribers(): void
new TestMarkedIncompleteSubscriber($this),
new TestSkippedSubscriber($this),
new TestRunnerExecutionFinishedSubscriber($this),
);
];
}

private function createDocument(): void
Expand Down
19 changes: 4 additions & 15 deletions src/Logging/TeamCity/TeamCityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();
}

Expand Down Expand Up @@ -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),
Expand All @@ -288,7 +277,7 @@ private function registerSubscribers(): void
new TestSkippedSubscriber($this),
new TestConsideredRiskySubscriber($this),
new TestRunnerExecutionFinishedSubscriber($this),
);
];
}

private function setFlowId(): void
Expand Down
23 changes: 4 additions & 19 deletions src/Logging/TestDox/TestMethod/TestResultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -54,15 +51,6 @@ final class TestResultCollector
*/
private array $testDoubles = [];

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public function __construct()
{
$this->registerSubscribers();
}

/**
* @psalm-return array<string, TestResultCollection>
*/
Expand Down Expand Up @@ -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),
Expand All @@ -204,6 +189,6 @@ private function registerSubscribers(): void
new TestPassedSubscriber($this),
new TestPreparedSubscriber($this),
new TestSkippedSubscriber($this),
);
];
}
}
44 changes: 16 additions & 28 deletions src/Runner/ResultCache/ResultCacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
);
}
}
14 changes: 4 additions & 10 deletions src/Runner/TestResult/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -174,7 +168,7 @@ public function __construct()
new TestTriggeredWarningSubscriber($this),
new TestRunnerTriggeredDeprecationSubscriber($this),
new TestRunnerTriggeredWarningSubscriber($this),
);
];
}

public function result(): TestResult
Expand Down
6 changes: 5 additions & 1 deletion src/Runner/TestResult/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHPUnit\TestRunner\TestResult;

use PHPUnit\Event\EventFacadeIsSealedException;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Event\UnknownSubscriberTypeException;

/**
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 0268da3

Please sign in to comment.