数组作为Behat步骤中的参数

时间:2014-11-14 10:07:47

标签: arrays behat

是否可以在Behat步骤中将数组作为参数传递?

例如,想要这样的东西:

When I select <"Alex","Sergey"> in "users"

我知道在这种情况下我可以使用:

When I select "Alex" from "users"
And I additionally select "Sergey" from "users"

但问题是在这里使用数组。

2 个答案:

答案 0 :(得分:7)

这就是我想出来的

Given "foo" translations equal "[foo,bar,bazz]"

/**
 * @Transform /^\[(.*)\]$/
 */
public function castStringToArray($string)
{
    return explode(',', $string);
}

/**
 * @Given /^"([^"]*)" translations equal "([^"]*)"$/
 */
public function translationsEqual($phraseName, $translations)
{
    // we have an array now
    var_dump($translations);
}

答案 1 :(得分:1)

我找到了可能的解决方案。 可以制作step argument transformations。然后,您可以轻松地将逗号分隔的字符串转换为数组。

更新另一个选项是爆炸以逗号分隔的字符串:

Behat step

Given article "Test article" is published at "Foo, Bar"

步骤代码:

  /**
   * @Given /^article "([^"]*)" is published at "([^"]*)"$/
   */
  public function givenArticleIsPublishedAtMediums($title, $mediums){
    // Explode mediums from a string.
    foreach (explode(',', $mediums) as $medium) {
      // ...
    }
  }