19
19
use PHPUnit \Metadata \ExcludeStaticPropertyFromBackup ;
20
20
use PHPUnit \Metadata \Parser \Registry as MetadataRegistry ;
21
21
use PHPUnit \Metadata \PreserveGlobalState ;
22
+ use PHPUnit \Metadata \RunClassInSeparateProcess ;
23
+ use PHPUnit \Metadata \RunInSeparateProcess ;
24
+ use PHPUnit \Metadata \RunTestsInSeparateProcesses ;
22
25
use PHPUnit \TextUI \Configuration \Registry as ConfigurationRegistry ;
23
26
use ReflectionClass ;
24
27
@@ -51,6 +54,7 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
51
54
$ this ->shouldTestMethodBeRunInSeparateProcess ($ className , $ methodName ),
52
55
$ this ->shouldGlobalStateBePreserved ($ className , $ methodName ),
53
56
$ this ->shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess ($ className ),
57
+ $ this ->shouldForkIfPossible ($ className , $ methodName ),
54
58
$ this ->backupSettings ($ className , $ methodName ),
55
59
$ groups ,
56
60
);
@@ -64,6 +68,7 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
64
68
$ this ->shouldTestMethodBeRunInSeparateProcess ($ className , $ methodName ),
65
69
$ this ->shouldGlobalStateBePreserved ($ className , $ methodName ),
66
70
$ this ->shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess ($ className ),
71
+ $ this ->shouldForkIfPossible ($ className , $ methodName ),
67
72
$ this ->backupSettings ($ className , $ methodName ),
68
73
);
69
74
@@ -76,7 +81,7 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
76
81
* @psalm-param array{backupGlobals: ?bool, backupGlobalsExcludeList: list<string>, backupStaticProperties: ?bool, backupStaticPropertiesExcludeList: array<string,list<string>>} $backupSettings
77
82
* @psalm-param list<non-empty-string> $groups
78
83
*/
79
- private function buildDataProviderTestSuite (string $ methodName , string $ className , array $ data , bool $ runTestInSeparateProcess , ?bool $ preserveGlobalState , bool $ runClassInSeparateProcess , array $ backupSettings , array $ groups ): DataProviderTestSuite
84
+ private function buildDataProviderTestSuite (string $ methodName , string $ className , array $ data , bool $ runTestInSeparateProcess , ?bool $ preserveGlobalState , bool $ runClassInSeparateProcess , bool $ forkIfPossible , array $ backupSettings , array $ groups ): DataProviderTestSuite
80
85
{
81
86
$ dataProviderTestSuite = DataProviderTestSuite::empty (
82
87
$ className . ':: ' . $ methodName ,
@@ -98,6 +103,7 @@ private function buildDataProviderTestSuite(string $methodName, string $classNam
98
103
$ runTestInSeparateProcess ,
99
104
$ preserveGlobalState ,
100
105
$ runClassInSeparateProcess ,
106
+ $ forkIfPossible ,
101
107
$ backupSettings ,
102
108
);
103
109
@@ -110,7 +116,7 @@ private function buildDataProviderTestSuite(string $methodName, string $classNam
110
116
/**
111
117
* @psalm-param array{backupGlobals: ?bool, backupGlobalsExcludeList: list<string>, backupStaticProperties: ?bool, backupStaticPropertiesExcludeList: array<string,list<string>>} $backupSettings
112
118
*/
113
- private function configureTestCase (TestCase $ test , bool $ runTestInSeparateProcess , ?bool $ preserveGlobalState , bool $ runClassInSeparateProcess , array $ backupSettings ): void
119
+ private function configureTestCase (TestCase $ test , bool $ runTestInSeparateProcess , ?bool $ preserveGlobalState , bool $ runClassInSeparateProcess , bool $ forkIfPossible , array $ backupSettings ): void
114
120
{
115
121
if ($ runTestInSeparateProcess ) {
116
122
$ test ->setRunTestInSeparateProcess (true );
@@ -120,6 +126,10 @@ private function configureTestCase(TestCase $test, bool $runTestInSeparateProces
120
126
$ test ->setRunClassInSeparateProcess (true );
121
127
}
122
128
129
+ if ($ forkIfPossible ) {
130
+ $ test ->setForkIfPossible (true );
131
+ }
132
+
123
133
if ($ preserveGlobalState !== null ) {
124
134
$ test ->setPreserveGlobalState ($ preserveGlobalState );
125
135
}
@@ -272,4 +282,53 @@ private function shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess(str
272
282
{
273
283
return MetadataRegistry::parser ()->forClass ($ className )->isRunClassInSeparateProcess ()->isNotEmpty ();
274
284
}
285
+
286
+ /**
287
+ * @psalm-param class-string $className
288
+ * @psalm-param non-empty-string $methodName
289
+ */
290
+ private function shouldForkIfPossible (string $ className , string $ methodName ): bool
291
+ {
292
+ $ metadataForMethod = MetadataRegistry::parser ()->forMethod ($ className , $ methodName );
293
+
294
+ if ($ metadataForMethod ->isRunInSeparateProcess ()->isNotEmpty ()) {
295
+ $ metadata = $ metadataForMethod ->isRunInSeparateProcess ()->asArray ()[0 ];
296
+
297
+ assert ($ metadata instanceof RunInSeparateProcess);
298
+
299
+ $ forkIfPossible = $ metadata ->forkIfPossible ();
300
+
301
+ if ($ forkIfPossible !== null ) {
302
+ return $ forkIfPossible ;
303
+ }
304
+ }
305
+
306
+ $ metadataForClass = MetadataRegistry::parser ()->forClass ($ className );
307
+
308
+ if ($ metadataForClass ->isRunTestsInSeparateProcesses ()->isNotEmpty ()) {
309
+ $ metadata = $ metadataForClass ->isRunTestsInSeparateProcesses ()->asArray ()[0 ];
310
+
311
+ assert ($ metadata instanceof RunTestsInSeparateProcesses);
312
+
313
+ $ forkIfPossible = $ metadata ->forkIfPossible ();
314
+
315
+ if ($ forkIfPossible !== null ) {
316
+ return $ forkIfPossible ;
317
+ }
318
+ }
319
+
320
+ if ($ metadataForClass ->isRunClassInSeparateProcess ()->isNotEmpty ()) {
321
+ $ metadata = $ metadataForClass ->isRunClassInSeparateProcess ()->asArray ()[0 ];
322
+
323
+ assert ($ metadata instanceof RunClassInSeparateProcess);
324
+
325
+ $ forkIfPossible = $ metadata ->forkIfPossible ();
326
+
327
+ if ($ forkIfPossible !== null ) {
328
+ return $ forkIfPossible ;
329
+ }
330
+ }
331
+
332
+ return false ;
333
+ }
275
334
}
0 commit comments