3
3
4
4
namespace Zalas \PHPUnit \Globals ;
5
5
6
+ use PHPUnit \Event \Code \Test ;
6
7
use PHPUnit \Event \Code \TestMethod ;
7
8
use PHPUnit \Event \Test \PreparationStarted ;
8
9
use PHPUnit \Event \Test \PreparationStartedSubscriber ;
10
+ use ReflectionAttribute ;
11
+ use ReflectionClass ;
12
+ use ReflectionException ;
13
+ use ReflectionMethod ;
14
+ use RuntimeException ;
9
15
use Zalas \PHPUnit \Globals \Attribute \Env ;
10
16
use Zalas \PHPUnit \Globals \Attribute \Putenv ;
11
17
use Zalas \PHPUnit \Globals \Attribute \Server ;
@@ -17,7 +23,7 @@ public function notify(PreparationStarted $event): void
17
23
$ this ->readGlobalAttributes ($ event ->test ());
18
24
}
19
25
20
- private function readGlobalAttributes (TestMethod $ method ): void
26
+ private function readGlobalAttributes (Test $ method ): void
21
27
{
22
28
$ attributes = $ this ->parseTestMethodAttributes ($ method );
23
29
$ setVars = $ this ->findSetVarAttributes ($ attributes );
@@ -84,27 +90,32 @@ private function findUnsetVarAttributes(array $attributes): array
84
90
/**
85
91
* @return array<Env|Putenv|Server>
86
92
*/
87
- private function parseTestMethodAttributes (TestMethod $ method ): array
93
+ private function parseTestMethodAttributes (Test $ method ): array
88
94
{
95
+ if (!$ method instanceof TestMethod) {
96
+ return [];
97
+ }
98
+
89
99
$ className = $ method ->className ();
90
100
$ methodName = $ method ->methodName ();
91
101
92
- $ methodAttributes = null ;
93
-
94
- if (null !== $ methodName ) {
102
+ try {
95
103
$ methodAttributes = $ this ->collectGlobalsFromAttributes (
96
- (new \ ReflectionMethod ($ className , $ methodName ))->getAttributes ()
104
+ (new ReflectionMethod ($ className , $ methodName ))->getAttributes ()
97
105
);
98
- }
99
106
100
- return \array_merge (
101
- $ methodAttributes ,
102
- $ this ->collectGlobalsFromAttributes ((new \ReflectionClass ($ className ))->getAttributes ())
103
- );
107
+ return \array_merge (
108
+ $ methodAttributes ,
109
+ $ this ->collectGlobalsFromAttributes ((new ReflectionClass ($ className ))->getAttributes ())
110
+ );
111
+ } catch (ReflectionException $ e ) {
112
+ // There would need to be a bug in PHPUnit for the ReflectionException to be thrown.
113
+ throw new RuntimeException ("Failed to parse test method $ className:: $ methodName " , 0 , $ e );
114
+ }
104
115
}
105
116
106
117
/**
107
- * @param array<\ ReflectionAttribute> $attributes
118
+ * @param array<ReflectionAttribute> $attributes
108
119
* @return array<Env|Putenv|Server>
109
120
*/
110
121
private function collectGlobalsFromAttributes (array $ attributes ): array
0 commit comments