You are here

public function FeatureContext::fixStepArgument in Bear 7

Override MinkContext::fixStepArgument().

Make it possible to use [random]. If you want to use the previous random value [random:1]. Also, allow newlines in arguments.

File

tests/features/bootstrap/FeatureContext.php, line 61

Class

FeatureContext
Defines application features from the specific context.

Code

public function fixStepArgument($argument) {

  // Token replace the argument.
  static $random = array();
  for ($start = 0; ($start = strpos($argument, '[', $start)) !== FALSE;) {
    $end = strpos($argument, ']', $start);
    if ($end === FALSE) {
      break;
    }
    $random_generator = new Random();
    $name = substr($argument, $start + 1, $end - $start - 1);
    if ($name == 'random') {
      $this->vars[$name] = $random_generator
        ->name(8);
      $random[] = $this->vars[$name];
    }
    elseif (substr($name, 0, 7) == 'random:') {
      $num = substr($name, 7);
      if (is_numeric($num) && $num <= count($random)) {
        $this->vars[$name] = $random[count($random) - $num];
      }
    }
    if (isset($this->vars[$name])) {
      $argument = substr_replace($argument, $this->vars[$name], $start, $end - $start + 1);
      $start += strlen($this->vars[$name]);
    }
    else {
      $start = $end + 1;
    }
  }
  return $argument;
}