Skip to content

Commit

Permalink
implemented named arguments feature
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Apr 2, 2011
1 parent f4bae48 commit 6e0f861
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Behat/Behat/Definition/Definition.php
Expand Up @@ -104,6 +104,16 @@ public function getRegex()
return $this->regex; return $this->regex;
} }


/**
* Returns callback reflection for definition matcher.
*
* @return ReflectionFunction
*/
public function getCallbackReflection()
{
return new \ReflectionFunction($this->callback);
}

/** /**
* Saves matched step text to definition. * Saves matched step text to definition.
* *
Expand Down
34 changes: 32 additions & 2 deletions src/Behat/Behat/Definition/DefinitionDispatcher.php
Expand Up @@ -175,7 +175,7 @@ public function findDefinition(StepNode $step)
} }


$text = $step->getText(); $text = $step->getText();
$args = $step->getArguments(); $multiline = $step->getArguments();
$matches = array(); $matches = array();


// find step to match // find step to match
Expand All @@ -184,7 +184,10 @@ public function findDefinition(StepNode $step)


if (preg_match($origRegex, $text, $arguments) if (preg_match($origRegex, $text, $arguments)
|| ($origRegex !== $transRegex && preg_match($transRegex, $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 // transform arguments
foreach ($arguments as $num => $argument) { foreach ($arguments as $num => $argument) {
Expand Down Expand Up @@ -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;
}
} }

0 comments on commit 6e0f861

Please sign in to comment.