Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 14, 2023
1 parent 171917c commit ec415a7
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 37 deletions.
12 changes: 8 additions & 4 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,6 @@
<code>objectForTrait</code>
</DeprecatedMethod>
<MissingThrowsDocblock>
<code>Facade::deprecationsTriggeredByCurrentTest()</code>
<code>Facade::deprecationsTriggeredByCurrentTest()</code>
<code>Facade::deprecationsTriggeredByCurrentTest()</code>
<code>Facade::deprecationsTriggeredByCurrentTest()</code>
<code>getMethod</code>
</MissingThrowsDocblock>
<PropertyNotSetInConstructor>
Expand Down Expand Up @@ -571,6 +567,14 @@
<code>stop</code>
</PossiblyNullReference>
</file>
<file src="src/Runner/DeprecationCollector/Facade.php">
<MissingThrowsDocblock>
<code>self::collector()</code>
<code>self::collector()</code>
<code>self::collector()</code>
<code>self::collector()</code>
</MissingThrowsDocblock>
</file>
<file src="src/Runner/ErrorHandler.php">
<ArgumentTypeCoercion>
<code>$errorFile</code>
Expand Down
6 changes: 3 additions & 3 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
use PHPUnit\Metadata\Api\HookMethods;
use PHPUnit\Metadata\Api\Requirements;
use PHPUnit\Metadata\Parser\Registry as MetadataRegistry;
use PHPUnit\TestRunner\TestResult\Facade;
use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;
use PHPUnit\TestRunner\TestResult\PassedTests;
use PHPUnit\TextUI\Configuration\Registry as ConfigurationRegistry;
use PHPUnit\Util\Test as TestUtil;
Expand Down Expand Up @@ -1572,7 +1572,7 @@ private function verifyDeprecationExpectations(): void
if ($this->expectedUserDeprecationMessageRegularExpression !== null) {
$this->numberOfAssertionsPerformed++;

foreach (Facade::deprecationsTriggeredByCurrentTest() as $deprecation) {
foreach (DeprecationCollector::deprecations() as $deprecation) {
if (preg_match($this->expectedUserDeprecationMessageRegularExpression, $deprecation) > 0) {
return;
}
Expand All @@ -1587,7 +1587,7 @@ private function verifyDeprecationExpectations(): void
} elseif ($this->expectedUserDeprecationMessage !== null) {
$this->numberOfAssertionsPerformed++;

if (in_array($this->expectedUserDeprecationMessage, Facade::deprecationsTriggeredByCurrentTest(), true)) {
if (in_array($this->expectedUserDeprecationMessage, DeprecationCollector::deprecations(), true)) {
return;
}

Expand Down
77 changes: 77 additions & 0 deletions src/Runner/DeprecationCollector/Collector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

use PHPUnit\Event\EventFacadeIsSealedException;
use PHPUnit\Event\Facade;
use PHPUnit\Event\Test\DeprecationTriggered;
use PHPUnit\Event\Test\PhpunitDeprecationTriggered;
use PHPUnit\Event\UnknownSubscriberTypeException;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class Collector
{
/**
* @psalm-var list<non-empty-string>
*/
private array $deprecations = [];

/**
* @psalm-var list<non-empty-string>
*/
private array $phpunitDeprecations = [];

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public function __construct(Facade $facade)
{
$facade->registerSubscribers(
new TestPreparedSubscriber($this),
new TestTriggeredDeprecationSubscriber($this),
new TestTriggeredPhpunitDeprecationSubscriber($this),
);
}

/**
* @psalm-return list<non-empty-string>
*/
public function deprecations(): array
{
return $this->deprecations;
}

/**
* @psalm-return list<non-empty-string>
*/
public function phpunitDeprecations(): array

Check warning on line 57 in src/Runner/DeprecationCollector/Collector.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/DeprecationCollector/Collector.php#L57

Added line #L57 was not covered by tests
{
return $this->phpunitDeprecations;

Check warning on line 59 in src/Runner/DeprecationCollector/Collector.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/DeprecationCollector/Collector.php#L59

Added line #L59 was not covered by tests
}

public function testPrepared(): void
{
$this->deprecations = [];
$this->phpunitDeprecations = [];
}

public function testTriggeredDeprecation(DeprecationTriggered $event): void
{
$this->deprecations[] = $event->message();
}

public function testTriggeredPhpunitDeprecation(PhpunitDeprecationTriggered $event): void
{
$this->phpunitDeprecations[] = $event->message();
}
}
60 changes: 60 additions & 0 deletions src/Runner/DeprecationCollector/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

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

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class Facade
{
private static ?Collector $collector = null;

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public static function init(): void
{
self::collector();
}

/**
* @psalm-return list<non-empty-string>
*/
public static function deprecations(): array
{
return self::collector()->deprecations();
}

/**
* @psalm-return list<non-empty-string>
*/
public static function phpunitDeprecations(): array

Check warning on line 43 in src/Runner/DeprecationCollector/Facade.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/DeprecationCollector/Facade.php#L43

Added line #L43 was not covered by tests
{
return self::collector()->phpunitDeprecations();

Check warning on line 45 in src/Runner/DeprecationCollector/Facade.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/DeprecationCollector/Facade.php#L45

Added line #L45 was not covered by tests
}

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
private static function collector(): Collector
{
if (self::$collector === null) {
self::$collector = new Collector(EventFacade::instance());
}

return self::$collector;
}
}
28 changes: 28 additions & 0 deletions src/Runner/DeprecationCollector/Subscriber/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
abstract class Subscriber
{
private readonly Collector $collector;

public function __construct(Collector $collector)
{
$this->collector = $collector;
}

protected function collector(): Collector
{
return $this->collector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

use PHPUnit\Event\Test\Prepared;
use PHPUnit\Event\Test\PreparedSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestPreparedSubscriber extends Subscriber implements PreparedSubscriber
{
public function notify(Prepared $event): void
{
$this->collector()->testPrepared();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

use PHPUnit\Event\Test\DeprecationTriggered;
use PHPUnit\Event\Test\DeprecationTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestTriggeredDeprecationSubscriber extends Subscriber implements DeprecationTriggeredSubscriber
{
public function notify(DeprecationTriggered $event): void
{
$this->collector()->testTriggeredDeprecation($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

use PHPUnit\Event\Test\PhpunitDeprecationTriggered;
use PHPUnit\Event\Test\PhpunitDeprecationTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestTriggeredPhpunitDeprecationSubscriber extends Subscriber implements PhpunitDeprecationTriggeredSubscriber
{
public function notify(PhpunitDeprecationTriggered $event): void
{
$this->collector()->testTriggeredPhpunitDeprecation($event);
}
}
21 changes: 2 additions & 19 deletions src/Runner/TestResult/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ final class Collector
private bool $prepared = false;
private bool $currentTestSuiteForTestClassFailed = false;

/**
* @psalm-var list<non-empty-string>
*/
private array $deprecationsTriggeredByCurrentTest = [];

/**
* @psalm-var non-negative-int
*/
Expand Down Expand Up @@ -284,8 +279,7 @@ public function testSuiteFinished(TestSuiteFinished $event): void

public function testPrepared(): void
{
$this->prepared = true;
$this->deprecationsTriggeredByCurrentTest = [];
$this->prepared = true;
}

public function testFinished(Finished $event): void
Expand All @@ -294,8 +288,7 @@ public function testFinished(Finished $event): void

$this->numberOfTestsRun++;

$this->prepared = false;
$this->deprecationsTriggeredByCurrentTest = [];
$this->prepared = false;
}

public function beforeTestClassMethodErrored(BeforeFirstTestMethodErrored $event): void
Expand Down Expand Up @@ -355,8 +348,6 @@ public function testConsideredRisky(ConsideredRisky $event): void

public function testTriggeredDeprecation(DeprecationTriggered $event): void
{
$this->deprecationsTriggeredByCurrentTest[] = $event->message();

if ($event->ignoredByTest()) {
return;
}
Expand Down Expand Up @@ -661,14 +652,6 @@ public function hasWarnings(): bool
!empty($this->testRunnerTriggeredWarningEvents);
}

/**
* @psalm-return list<non-empty-string>
*/
public function deprecationsTriggeredByCurrentTest(): array
{
return $this->deprecationsTriggeredByCurrentTest;
}

/**
* @psalm-return non-empty-string
*/
Expand Down
11 changes: 0 additions & 11 deletions src/Runner/TestResult/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ public static function result(): TestResult
return self::collector()->result();
}

/**
* @psalm-return list<non-empty-string>
*
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public static function deprecationsTriggeredByCurrentTest(): array
{
return self::collector()->deprecationsTriggeredByCurrentTest();
}

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
Expand Down
2 changes: 2 additions & 0 deletions src/TextUI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PHPUnit\Runner\Baseline\Reader;
use PHPUnit\Runner\Baseline\Writer;
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;
use PHPUnit\Runner\ErrorHandler;
use PHPUnit\Runner\Extension\ExtensionBootstrapper;
use PHPUnit\Runner\Extension\Facade as ExtensionFacade;
Expand Down Expand Up @@ -154,6 +155,7 @@ public function run(array $argv): int
$testDoxResultCollector = $this->testDoxResultCollector($configuration);

TestResultFacade::init();
DeprecationCollector::init();

$resultCache = $this->initializeTestResultCache($configuration);

Expand Down

0 comments on commit ec415a7

Please sign in to comment.