1111
1212namespace Symfony \Bridge \PhpUnit ;
1313
14+ use PHPUnit \Event \Code \Test ;
15+ use PHPUnit \Event \Code \TestMethod ;
1416use PHPUnit \Event \Test \BeforeTestMethodErrored ;
1517use PHPUnit \Event \Test \BeforeTestMethodErroredSubscriber ;
1618use PHPUnit \Event \Test \Errored ;
1921use PHPUnit \Event \Test \FinishedSubscriber ;
2022use PHPUnit \Event \Test \Skipped ;
2123use PHPUnit \Event \Test \SkippedSubscriber ;
24+ use PHPUnit \Metadata \Group ;
2225use PHPUnit \Runner \Extension \Extension ;
2326use PHPUnit \Runner \Extension \Facade ;
2427use PHPUnit \Runner \Extension \ParameterCollection ;
@@ -47,31 +50,36 @@ 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+ SymfonyExtension::disableClockMock ($ event -> test () );
54+ SymfonyExtension::disableDnsMock ($ event -> test () );
5255 }
5356 });
5457 $ facade ->registerSubscriber (new class implements FinishedSubscriber {
5558 public function notify (Finished $ event ): void
5659 {
57- SymfonyExtension::disableClockMock ();
58- SymfonyExtension::disableDnsMock ();
60+ SymfonyExtension::disableClockMock ($ event -> test () );
61+ SymfonyExtension::disableDnsMock ($ event -> test () );
5962 }
6063 });
6164 $ facade ->registerSubscriber (new class implements SkippedSubscriber {
6265 public function notify (Skipped $ event ): void
6366 {
64- SymfonyExtension::disableClockMock ();
65- SymfonyExtension::disableDnsMock ();
67+ SymfonyExtension::disableClockMock ($ event -> test () );
68+ SymfonyExtension::disableDnsMock ($ event -> test () );
6669 }
6770 });
6871
6972 if (interface_exists (BeforeTestMethodErroredSubscriber::class)) {
7073 $ facade ->registerSubscriber (new class implements BeforeTestMethodErroredSubscriber {
7174 public function notify (BeforeTestMethodErrored $ event ): void
7275 {
73- SymfonyExtension::disableClockMock ();
74- SymfonyExtension::disableDnsMock ();
76+ if (method_exists ($ event , 'test ' )) {
77+ SymfonyExtension::disableClockMock ($ event ->test ());
78+ SymfonyExtension::disableDnsMock ($ event ->test ());
79+ } else {
80+ ClockMock::withClockMock (false );
81+ DnsMock::withMockedHosts ([]);
82+ }
7583 }
7684 });
7785 }
@@ -88,16 +96,38 @@ public function notify(BeforeTestMethodErrored $event): void
8896 /**
8997 * @internal
9098 */
91- public static function disableClockMock (): void
99+ public static function disableClockMock (Test $ test ): void
92100 {
93- ClockMock::withClockMock (false );
101+ if (self ::hasGroup ($ test , 'time-sensitive ' )) {
102+ ClockMock::withClockMock (false );
103+ }
94104 }
95105
96106 /**
97107 * @internal
98108 */
99- public static function disableDnsMock (): void
109+ public static function disableDnsMock (Test $ test ): void
100110 {
101- DnsMock::withMockedHosts ([]);
111+ if (self ::hasGroup ($ test , 'dns-sensitive ' )) {
112+ DnsMock::withMockedHosts ([]);
113+ }
114+ }
115+
116+ /**
117+ * @internal
118+ */
119+ public static function hasGroup (Test $ test , string $ groupName ): bool
120+ {
121+ if (!$ test instanceof TestMethod) {
122+ return false ;
123+ }
124+
125+ foreach ($ test ->metadata () as $ metadata ) {
126+ if ($ metadata instanceof Group && $ groupName === $ metadata ->groupName ()) {
127+ return true ;
128+ }
129+ }
130+
131+ return false ;
102132 }
103133}
0 commit comments