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
@@ -50,6 +53,7 @@ public function build(ReflectionClass $theClass, string $methodName, array $grou
50
53
$ this ->shouldTestMethodBeRunInSeparateProcess ($ className , $ methodName ),
51
54
$ this ->shouldGlobalStateBePreserved ($ className , $ methodName ),
52
55
$ this ->shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess ($ className ),
56
+ $ this ->shouldForkIfPossible ($ className , $ methodName ),
53
57
$ this ->backupSettings ($ className , $ methodName ),
54
58
$ groups ,
55
59
);
@@ -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 ,
@@ -99,6 +104,7 @@ private function buildDataProviderTestSuite(string $methodName, string $classNam
99
104
$ runTestInSeparateProcess ,
100
105
$ preserveGlobalState ,
101
106
$ runClassInSeparateProcess ,
107
+ $ forkIfPossible ,
102
108
$ backupSettings ,
103
109
);
104
110
@@ -111,7 +117,7 @@ private function buildDataProviderTestSuite(string $methodName, string $classNam
111
117
/**
112
118
* @psalm-param array{backupGlobals: ?bool, backupGlobalsExcludeList: list<string>, backupStaticProperties: ?bool, backupStaticPropertiesExcludeList: array<string,list<string>>} $backupSettings
113
119
*/
114
- private function configureTestCase (TestCase $ test , bool $ runTestInSeparateProcess , ?bool $ preserveGlobalState , bool $ runClassInSeparateProcess , array $ backupSettings ): void
120
+ private function configureTestCase (TestCase $ test , bool $ runTestInSeparateProcess , ?bool $ preserveGlobalState , bool $ runClassInSeparateProcess , bool $ forkIfPossible , array $ backupSettings ): void
115
121
{
116
122
if ($ runTestInSeparateProcess ) {
117
123
$ test ->setRunTestInSeparateProcess (true );
@@ -121,6 +127,10 @@ private function configureTestCase(TestCase $test, bool $runTestInSeparateProces
121
127
$ test ->setRunClassInSeparateProcess (true );
122
128
}
123
129
130
+ if ($ forkIfPossible ) {
131
+ $ test ->setForkIfPossible (true );
132
+ }
133
+
124
134
if ($ preserveGlobalState !== null ) {
125
135
$ test ->setPreserveGlobalState ($ preserveGlobalState );
126
136
}
@@ -273,4 +283,53 @@ private function shouldAllTestMethodsOfTestClassBeRunInSingleSeparateProcess(str
273
283
{
274
284
return MetadataRegistry::parser ()->forClass ($ className )->isRunClassInSeparateProcess ()->isNotEmpty ();
275
285
}
286
+
287
+ /**
288
+ * @psalm-param class-string $className
289
+ * @psalm-param non-empty-string $methodName
290
+ */
291
+ private function shouldForkIfPossible (string $ className , string $ methodName ): bool
292
+ {
293
+ $ metadataForMethod = MetadataRegistry::parser ()->forMethod ($ className , $ methodName );
294
+
295
+ if ($ metadataForMethod ->isRunInSeparateProcess ()->isNotEmpty ()) {
296
+ $ metadata = $ metadataForMethod ->isRunInSeparateProcess ()->asArray ()[0 ];
297
+
298
+ assert ($ metadata instanceof RunInSeparateProcess);
299
+
300
+ $ forkIfPossible = $ metadata ->forkIfPossible ();
301
+
302
+ if ($ forkIfPossible !== null ) {
303
+ return $ forkIfPossible ;
304
+ }
305
+ }
306
+
307
+ $ metadataForClass = MetadataRegistry::parser ()->forClass ($ className );
308
+
309
+ if ($ metadataForClass ->isRunTestsInSeparateProcesses ()->isNotEmpty ()) {
310
+ $ metadata = $ metadataForClass ->isRunTestsInSeparateProcesses ()->asArray ()[0 ];
311
+
312
+ assert ($ metadata instanceof RunTestsInSeparateProcesses);
313
+
314
+ $ forkIfPossible = $ metadata ->forkIfPossible ();
315
+
316
+ if ($ forkIfPossible !== null ) {
317
+ return $ forkIfPossible ;
318
+ }
319
+ }
320
+
321
+ if ($ metadataForClass ->isRunClassInSeparateProcess ()->isNotEmpty ()) {
322
+ $ metadata = $ metadataForClass ->isRunClassInSeparateProcess ()->asArray ()[0 ];
323
+
324
+ assert ($ metadata instanceof RunClassInSeparateProcess);
325
+
326
+ $ forkIfPossible = $ metadata ->forkIfPossible ();
327
+
328
+ if ($ forkIfPossible !== null ) {
329
+ return $ forkIfPossible ;
330
+ }
331
+ }
332
+
333
+ return false ;
334
+ }
276
335
}
0 commit comments