Skip to content

Commit cc14427

Browse files
committed
[PhpUnitBridge] Clean up mocked features only when group is present
1 parent 1084655 commit cc14427

File tree

3 files changed

+135
-8
lines changed

3 files changed

+135
-8
lines changed

src/Symfony/Bridge/PhpUnit/SymfonyExtension.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\PhpUnit;
1313

14+
use PHPUnit\Event\Code\Test;
15+
use PHPUnit\Event\Code\TestMethod;
1416
use PHPUnit\Event\Test\BeforeTestMethodErrored;
1517
use PHPUnit\Event\Test\BeforeTestMethodErroredSubscriber;
1618
use PHPUnit\Event\Test\Errored;
@@ -19,6 +21,7 @@
1921
use PHPUnit\Event\Test\FinishedSubscriber;
2022
use PHPUnit\Event\Test\Skipped;
2123
use PHPUnit\Event\Test\SkippedSubscriber;
24+
use PHPUnit\Metadata\Group;
2225
use PHPUnit\Runner\Extension\Extension;
2326
use PHPUnit\Runner\Extension\Facade;
2427
use PHPUnit\Runner\Extension\ParameterCollection;
@@ -47,31 +50,60 @@ public function bootstrap(Configuration $configuration, Facade $facade, Paramete
4750
$facade->registerSubscriber(new class implements ErroredSubscriber {
4851
public function notify(Errored $event): void
4952
{
50-
SymfonyExtension::disableClockMock();
51-
SymfonyExtension::disableDnsMock();
53+
$test = $event->test();
54+
55+
if (SymfonyExtension::hasGroup($test, 'time-sensitive')) {
56+
SymfonyExtension::disableClockMock();
57+
}
58+
if (SymfonyExtension::hasGroup($test, 'dns-sensitive')) {
59+
SymfonyExtension::disableDnsMock();
60+
}
5261
}
5362
});
5463
$facade->registerSubscriber(new class implements FinishedSubscriber {
5564
public function notify(Finished $event): void
5665
{
57-
SymfonyExtension::disableClockMock();
58-
SymfonyExtension::disableDnsMock();
66+
$test = $event->test();
67+
68+
if (SymfonyExtension::hasGroup($test, 'time-sensitive')) {
69+
SymfonyExtension::disableClockMock();
70+
}
71+
if (SymfonyExtension::hasGroup($test, 'dns-sensitive')) {
72+
SymfonyExtension::disableDnsMock();
73+
}
5974
}
6075
});
6176
$facade->registerSubscriber(new class implements SkippedSubscriber {
6277
public function notify(Skipped $event): void
6378
{
64-
SymfonyExtension::disableClockMock();
65-
SymfonyExtension::disableDnsMock();
79+
$test = $event->test();
80+
81+
if (SymfonyExtension::hasGroup($test, 'time-sensitive')) {
82+
SymfonyExtension::disableClockMock();
83+
}
84+
if (SymfonyExtension::hasGroup($test, 'dns-sensitive')) {
85+
SymfonyExtension::disableDnsMock();
86+
}
6687
}
6788
});
6889

6990
if (interface_exists(BeforeTestMethodErroredSubscriber::class)) {
7091
$facade->registerSubscriber(new class implements BeforeTestMethodErroredSubscriber {
7192
public function notify(BeforeTestMethodErrored $event): void
7293
{
73-
SymfonyExtension::disableClockMock();
74-
SymfonyExtension::disableDnsMock();
94+
if (method_exists($event, 'test')) {
95+
$test = $event->test();
96+
97+
if (SymfonyExtension::hasGroup($test, 'time-sensitive')) {
98+
SymfonyExtension::disableClockMock();
99+
}
100+
if (SymfonyExtension::hasGroup($test, 'dns-sensitive')) {
101+
SymfonyExtension::disableDnsMock();
102+
}
103+
} else {
104+
SymfonyExtension::disableClockMock();
105+
SymfonyExtension::disableDnsMock();
106+
}
75107
}
76108
});
77109
}
@@ -100,4 +132,22 @@ public static function disableDnsMock(): void
100132
{
101133
DnsMock::withMockedHosts([]);
102134
}
135+
136+
/**
137+
* @internal
138+
*/
139+
public static function hasGroup(Test $test, string $groupName): bool
140+
{
141+
if (!$test instanceof TestMethod) {
142+
return false;
143+
}
144+
145+
foreach ($test->metadata() as $metadata) {
146+
if ($metadata instanceof Group && $groupName === $metadata->groupName()) {
147+
return true;
148+
}
149+
}
150+
151+
return false;
152+
}
103153
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ClockMock;
16+
use Symfony\Bridge\PhpUnit\DnsMock;
17+
18+
class SymfonyExtensionWithManualRegister extends TestCase
19+
{
20+
public static function setUpBeforeClass(): void
21+
{
22+
ClockMock::register(self::class);
23+
ClockMock::withClockMock(strtotime('2024-05-20 15:30:00'));
24+
25+
DnsMock::register(self::class);
26+
DnsMock::withMockedHosts([
27+
'example.com' => [
28+
['type' => 'A', 'ip' => '1.2.3.4'],
29+
],
30+
]);
31+
}
32+
33+
public static function tearDownAfterClass(): void
34+
{
35+
ClockMock::withClockMock(false);
36+
DnsMock::withMockedHosts([]);
37+
}
38+
39+
public function testDate()
40+
{
41+
self::assertSame('2024-05-20 15:30:00', date('Y-m-d H:i:s'));
42+
}
43+
44+
public function testGetHostByName()
45+
{
46+
self::assertSame('1.2.3.4', gethostbyname('example.com'));
47+
}
48+
49+
public function testTime()
50+
{
51+
self::assertSame(1716219000, time());
52+
}
53+
54+
public function testDnsGetRecord()
55+
{
56+
self::assertSame([[
57+
'host' => 'example.com',
58+
'class' => 'IN',
59+
'ttl' => 1,
60+
'type' => 'A',
61+
'ip' => '1.2.3.4',
62+
]], dns_get_record('example.com'));
63+
}
64+
}

src/Symfony/Bridge/PhpUnit/Tests/symfonyextension.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ if (!getenv('SYMFONY_PHPUNIT_VERSION') || version_compare(getenv('SYMFONY_PHPUNI
55
--FILE--
66
<?php
77
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__));
8+
echo PHP_EOL;
9+
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__));
810
--EXPECTF--
911
PHPUnit %s
1012

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

1820
OK, but there were issues!
1921
Tests: 46, Assertions: 46, Deprecations: 1.
22+
23+
PHPUnit %s
24+
25+
Runtime: PHP %s
26+
Configuration: %s/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/symfonyextension/phpunit-with-extension.xml.dist
27+
28+
.... 4 / 4 (100%)
29+
30+
Time: %s, Memory: %s
31+
32+
OK (4 tests, 4 assertions)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy