Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implemented named arguments feature

  • Loading branch information...
commit 6e0f861f668161804f29a38e528d42eadd324734 1 parent f4bae48
@everzet everzet authored
View
10 src/Behat/Behat/Definition/Definition.php
@@ -105,6 +105,16 @@ public function getRegex()
}
/**
+ * Returns callback reflection for definition matcher.
+ *
+ * @return ReflectionFunction
+ */
+ public function getCallbackReflection()
+ {
+ return new \ReflectionFunction($this->callback);
+ }
+
+ /**
* Saves matched step text to definition.
*
* @param string $text step text (description)
View
34 src/Behat/Behat/Definition/DefinitionDispatcher.php
@@ -175,7 +175,7 @@ public function findDefinition(StepNode $step)
}
$text = $step->getText();
- $args = $step->getArguments();
+ $multiline = $step->getArguments();
$matches = array();
// find step to match
@@ -184,7 +184,10 @@ public function findDefinition(StepNode $step)
if (preg_match($origRegex, $text, $arguments)
|| ($origRegex !== $transRegex && preg_match($transRegex, $text, $arguments))) {
- $arguments = array_merge(array_slice($arguments, 1), $args);
+ // prepare callback arguments
+ $arguments = $this->prepareCallbackArguments(
+ $definition->getCallbackReflection(), array_slice($arguments, 1), $multiline
+ );
// transform arguments
foreach ($arguments as $num => $argument) {
@@ -259,4 +262,31 @@ protected function loadDefinitions()
}
}
}
+
+ /**
+ * Merges found arguments with multiliners and maps them to the function callback signature.
+ *
+ * @param ReflectionFunction $refl callback reflection
+ * @param array $arguments found arguments
+ * @param array $multiline multiline arguments of the step
+ *
+ * @return array
+ */
+ private function prepareCallbackArguments(\ReflectionFunction $refl, array $arguments, array $multiline)
+ {
+ $resulting = array();
+ foreach (array_slice($refl->getParameters(), 1) as $num => $parameterRefl) {
+ if (isset($arguments[$parameterRefl->getName()])) {
+ $resulting[] = $arguments[$parameterRefl->getName()];
+ } elseif (isset($arguments[$num])) {
+ $resulting[] = $arguments[$num];
+ }
+ }
+
+ foreach ($multiline as $argument) {
+ $resulting[] = $argument;
+ }
+
+ return $resulting;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.