Skip to content

[PhpUnitBridge] Clean up mocked features only when @group is present #60484

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 20, 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
54 changes: 42 additions & 12 deletions src/Symfony/Bridge/PhpUnit/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bridge\PhpUnit;

use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\BeforeTestMethodErrored;
use PHPUnit\Event\Test\BeforeTestMethodErroredSubscriber;
use PHPUnit\Event\Test\Errored;
Expand All @@ -19,6 +21,7 @@
use PHPUnit\Event\Test\FinishedSubscriber;
use PHPUnit\Event\Test\Skipped;
use PHPUnit\Event\Test\SkippedSubscriber;
use PHPUnit\Metadata\Group;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
Expand Down Expand Up @@ -47,31 +50,36 @@ public function bootstrap(Configuration $configuration, Facade $facade, Paramete
$facade->registerSubscriber(new class implements ErroredSubscriber {
public function notify(Errored $event): void
{
SymfonyExtension::disableClockMock();
SymfonyExtension::disableDnsMock();
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
}
});
$facade->registerSubscriber(new class implements FinishedSubscriber {
public function notify(Finished $event): void
{
SymfonyExtension::disableClockMock();
SymfonyExtension::disableDnsMock();
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
}
});
$facade->registerSubscriber(new class implements SkippedSubscriber {
public function notify(Skipped $event): void
{
SymfonyExtension::disableClockMock();
SymfonyExtension::disableDnsMock();
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
}
});

if (interface_exists(BeforeTestMethodErroredSubscriber::class)) {
$facade->registerSubscriber(new class implements BeforeTestMethodErroredSubscriber {
public function notify(BeforeTestMethodErrored $event): void
{
SymfonyExtension::disableClockMock();
SymfonyExtension::disableDnsMock();
if (method_exists($event, 'test')) {
SymfonyExtension::disableClockMock($event->test());
SymfonyExtension::disableDnsMock($event->test());
} else {
ClockMock::withClockMock(false);
DnsMock::withMockedHosts([]);
}
}
});
}
Expand All @@ -88,16 +96,38 @@ public function notify(BeforeTestMethodErrored $event): void
/**
* @internal
*/
public static function disableClockMock(): void
public static function disableClockMock(Test $test): void
{
ClockMock::withClockMock(false);
if (self::hasGroup($test, 'time-sensitive')) {
ClockMock::withClockMock(false);
}
}

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

/**
* @internal
*/
public static function hasGroup(Test $test, string $groupName): bool
{
if (!$test instanceof TestMethod) {
return false;
}

foreach ($test->metadata() as $metadata) {
if ($metadata instanceof Group && $groupName === $metadata->groupName()) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\DnsMock;

class SymfonyExtensionWithManualRegister extends TestCase
{
public static function setUpBeforeClass(): void
{
ClockMock::register(self::class);
ClockMock::withClockMock(strtotime('2024-05-20 15:30:00'));

DnsMock::register(self::class);
DnsMock::withMockedHosts([
'example.com' => [
['type' => 'A', 'ip' => '1.2.3.4'],
],
]);
}

public static function tearDownAfterClass(): void
{
ClockMock::withClockMock(false);
DnsMock::withMockedHosts([]);
}

public function testDate()
{
self::assertSame('2024-05-20 15:30:00', date('Y-m-d H:i:s'));
}

public function testGetHostByName()
{
self::assertSame('1.2.3.4', gethostbyname('example.com'));
}

public function testTime()
{
self::assertSame(1716219000, time());
}

public function testDnsGetRecord()
{
self::assertSame([[
'host' => 'example.com',
'class' => 'IN',
'ttl' => 1,
'type' => 'A',
'ip' => '1.2.3.4',
]], dns_get_record('example.com'));
}
}
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/symfonyextension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ if (!getenv('SYMFONY_PHPUNIT_VERSION') || version_compare(getenv('SYMFONY_PHPUNI
--FILE--
<?php
passthru(\sprintf('NO_COLOR=1 php %s/simple-phpunit.php -c %s/Fixtures/symfonyextension/phpunit-with-extension.xml.dist %s/SymfonyExtension.php', getenv('SYMFONY_SIMPLE_PHPUNIT_BIN_DIR'), __DIR__, __DIR__));
echo PHP_EOL;
passthru(\sprintf('NO_COLOR=1 php %s/simple-phpunit.php -c %s/Fixtures/symfonyextension/phpunit-with-extension.xml.dist %s/SymfonyExtensionWithManualRegister.php', getenv('SYMFONY_SIMPLE_PHPUNIT_BIN_DIR'), __DIR__, __DIR__));
--EXPECTF--
PHPUnit %s

Expand All @@ -17,3 +19,14 @@ Time: %s, Memory: %s

OK, but there were issues!
Tests: 46, Assertions: 46, Deprecations: 1.

PHPUnit %s

Runtime: PHP %s
Configuration: %s/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/symfonyextension/phpunit-with-extension.xml.dist

.... 4 / 4 (100%)

Time: %s, Memory: %s

OK (4 tests, 4 assertions)
Loading