33
44namespace Zalas \PHPUnit \Globals ;
55
6+ use PHPUnit \Event \Code \Test ;
67use PHPUnit \Event \Code \TestMethod ;
78use PHPUnit \Event \Test \PreparationStarted ;
89use PHPUnit \Event \Test \PreparationStartedSubscriber ;
10+ use ReflectionAttribute ;
11+ use ReflectionClass ;
12+ use ReflectionException ;
13+ use ReflectionMethod ;
14+ use RuntimeException ;
915use Zalas \PHPUnit \Globals \Attribute \Env ;
1016use Zalas \PHPUnit \Globals \Attribute \Putenv ;
1117use Zalas \PHPUnit \Globals \Attribute \Server ;
@@ -17,7 +23,7 @@ public function notify(PreparationStarted $event): void
1723 $ this ->readGlobalAttributes ($ event ->test ());
1824 }
1925
20- private function readGlobalAttributes (TestMethod $ method ): void
26+ private function readGlobalAttributes (Test $ method ): void
2127 {
2228 $ attributes = $ this ->parseTestMethodAttributes ($ method );
2329 $ setVars = $ this ->findSetVarAttributes ($ attributes );
@@ -84,27 +90,32 @@ private function findUnsetVarAttributes(array $attributes): array
8490 /**
8591 * @return array<Env|Putenv|Server>
8692 */
87- private function parseTestMethodAttributes (TestMethod $ method ): array
93+ private function parseTestMethodAttributes (Test $ method ): array
8894 {
95+ if (!$ method instanceof TestMethod) {
96+ return [];
97+ }
98+
8999 $ className = $ method ->className ();
90100 $ methodName = $ method ->methodName ();
91101
92- $ methodAttributes = null ;
93-
94- if (null !== $ methodName ) {
102+ try {
95103 $ methodAttributes = $ this ->collectGlobalsFromAttributes (
96- (new \ ReflectionMethod ($ className , $ methodName ))->getAttributes ()
104+ (new ReflectionMethod ($ className , $ methodName ))->getAttributes ()
97105 );
98- }
99106
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+ }
104115 }
105116
106117 /**
107- * @param array<\ ReflectionAttribute> $attributes
118+ * @param array<ReflectionAttribute> $attributes
108119 * @return array<Env|Putenv|Server>
109120 */
110121 private function collectGlobalsFromAttributes (array $ attributes ): array
0 commit comments