@@ -101,16 +101,24 @@ public function __construct($uri = self::PLATFORM_URI, $user = null, $password =
101101
102102 /**
103103 * @BeforeScenario
104+ *
105+ * Default gather contexts method.
106+ * Uses doc comment type hint to get the contexts and set them
104107 */
105108 public function gatherContexts (BeforeScenarioScope $ scope )
106109 {
107110 $ refClass = new ReflectionClass ($ this );
108111 $ refProperties = $ refClass ->getProperties ();
109112 foreach ($ refProperties as $ refProperty ) {
110- preg_match_all ('#@(.*?)\n#s ' , $ refProperty ->getDocComment (), $ matches );
111- $ contexts = $ this ->parseAnnotations ($ matches [1 ]);
112- foreach ($ contexts as $ property => $ context ) {
113- $ this ->$ property = $ scope ->getEnvironment ()->getContext ($ context );
113+ $ propertyName = $ refProperty ->getName ();
114+ // get property type hint from doc comment
115+ preg_match_all ('#@var\s(.*?)\n#s ' , $ refProperty ->getDocComment (), $ matches );
116+ foreach ($ matches [1 ] as $ typeHint ) {
117+ // checks if the type hint implements Behat Context
118+ $ implements = @class_implements ($ typeHint );
119+ if (!empty ($ implements ) && in_array ('Behat\Behat\Context\Context ' , $ implements )) {
120+ $ this ->$ propertyName = $ scope ->getEnvironment ()->getContext ($ typeHint );
121+ }
114122 }
115123 }
116124 }
@@ -449,30 +457,4 @@ protected function closeEditView()
449457 } catch (\Exception $ e ) {
450458 }
451459 }
452-
453- /**
454- * Returns an array with the properties contexts,
455- * if the properties use the Context Annotation.
456- *
457- * @return array array of methods and their service dependencies
458- */
459- private function parseAnnotations ($ annotations )
460- {
461- // parse array from (numeric key => 'annotation <value>') to (annotation => value)
462- $ propertiesContexts = [];
463- foreach ($ annotations as $ annotation ) {
464- if (!preg_match ('/^(\w+)\s+\$(\w+)\s+([\w\. \\\\]+)/ ' , $ annotation , $ matches )) {
465- continue ;
466- }
467-
468- array_shift ($ matches );
469- $ tag = array_shift ($ matches );
470- if ($ tag == 'Context ' ) {
471- list ($ property , $ context ) = $ matches ;
472- $ propertiesContexts [$ property ] = $ context ;
473- }
474- }
475-
476- return $ propertiesContexts ;
477- }
478460}
0 commit comments