Skip to content

[PhpUnitBridge] Fix cleaning up mocked features with attributes #60500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/phpunit-bridge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
php-version: "7.2"

- name: Lint
run: find ./src/Symfony/Bridge/PhpUnit -name '*.php' | grep -v -e /Tests/ -e /Attribute/ -e /Extension/ -e /Metadata/ -e ForV7 -e ForV8 -e ForV9 -e ConstraintLogicTrait | parallel -j 4 php -l {}
run: find ./src/Symfony/Bridge/PhpUnit -name '*.php' | grep -v -e /Tests/ -e /Attribute/ -e /Extension/ -e /Metadata/ -e ForV7 -e ForV8 -e ForV9 -e ConstraintLogicTrait -e SymfonyExtension | parallel -j 4 php -l {}
54 changes: 36 additions & 18 deletions src/Symfony/Bridge/PhpUnit/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
use Symfony\Bridge\PhpUnit\Attribute\DnsSensitive;
use Symfony\Bridge\PhpUnit\Attribute\TimeSensitive;
use Symfony\Bridge\PhpUnit\Extension\EnableClockMockSubscriber;
use Symfony\Bridge\PhpUnit\Extension\RegisterClockMockSubscriber;
use Symfony\Bridge\PhpUnit\Extension\RegisterDnsMockSubscriber;
Expand All @@ -50,35 +52,51 @@ public function bootstrap(Configuration $configuration, Facade $facade, Paramete

$facade->registerSubscriber(new RegisterClockMockSubscriber($reader));
$facade->registerSubscriber(new EnableClockMockSubscriber($reader));
$facade->registerSubscriber(new class implements ErroredSubscriber {
$facade->registerSubscriber(new class($reader) implements ErroredSubscriber {
public function __construct(private AttributeReader $reader)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I wanted to use the same instance of the reader was to leverage its internal cache. Perhaps moving the disableClockMock, disableDnsMock, and hasGroup methods to an abstract subscriber class would be better?

{
}

public function notify(Errored $event): void
{
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
SymfonyExtension::disableClockMock($event->test(), $this->reader);
SymfonyExtension::disableDnsMock($event->test(), $this->reader);
}
});
$facade->registerSubscriber(new class implements FinishedSubscriber {
$facade->registerSubscriber(new class($reader) implements FinishedSubscriber {
public function __construct(private AttributeReader $reader)
{
}

public function notify(Finished $event): void
{
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
SymfonyExtension::disableClockMock($event->test(), $this->reader);
SymfonyExtension::disableDnsMock($event->test(), $this->reader);
}
});
$facade->registerSubscriber(new class implements SkippedSubscriber {
$facade->registerSubscriber(new class($reader) implements SkippedSubscriber {
public function __construct(private AttributeReader $reader)
{
}

public function notify(Skipped $event): void
{
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
SymfonyExtension::disableClockMock($event->test(), $this->reader);
SymfonyExtension::disableDnsMock($event->test(), $this->reader);
}
});

if (interface_exists(BeforeTestMethodErroredSubscriber::class)) {
$facade->registerSubscriber(new class implements BeforeTestMethodErroredSubscriber {
$facade->registerSubscriber(new class($reader) implements BeforeTestMethodErroredSubscriber {
public function __construct(private AttributeReader $reader)
{
}

public function notify(BeforeTestMethodErrored $event): void
{
if (method_exists($event, 'test')) {
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
SymfonyExtension::disableClockMock($event->test(), $this->reader);
SymfonyExtension::disableDnsMock($event->test(), $this->reader);
} else {
ClockMock::withClockMock(false);
DnsMock::withMockedHosts([]);
Expand All @@ -99,27 +117,27 @@ public function notify(BeforeTestMethodErrored $event): void
/**
* @internal
*/
public static function disableClockMock(Test $test): void
public static function disableClockMock(Test $test, AttributeReader $reader): void
{
if (self::hasGroup($test, 'time-sensitive')) {
if (self::hasGroup($test, 'time-sensitive', $reader, TimeSensitive::class)) {
ClockMock::withClockMock(false);
}
}

/**
* @internal
*/
public static function disableDnsMock(Test $test): void
public static function disableDnsMock(Test $test, AttributeReader $reader): void
{
if (self::hasGroup($test, 'dns-sensitive')) {
if (self::hasGroup($test, 'dns-sensitive', $reader, DnsSensitive::class)) {
DnsMock::withMockedHosts([]);
}
}

/**
* @internal
*/
public static function hasGroup(Test $test, string $groupName): bool
public static function hasGroup(Test $test, string $groupName, AttributeReader $reader, string $attribute): bool
{
if (!$test instanceof TestMethod) {
return false;
Expand All @@ -131,6 +149,6 @@ public static function hasGroup(Test $test, string $groupName): bool
}
}

return false;
return [] !== $reader->forClassAndMethod($test->className(), $test->methodName(), $attribute);
}
}
Loading