Is it possible to pass an array as argument in Behat step?
For example want something like this:
When I select <"Alex","Sergey"> in "users"
I know that for this situation I can use:
When I select "Alex" from "users"
And I additionally select "Sergey" from "users"
But the question is about using arrays here.
This is what I came up with
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);
}
                        Option 1
It's possible to make step argument transformations. Then you can easily convert comma-separated string to an array. An Example:
Behat step
Given article "Test article" is published at "Foo, Bar"
Step code:
<?php
use Behat\Behat\Context\BehatContext;
class FeatureContext extends BehatContext
{
    /**
     * @Transform "([^"]*)"
     */
    public function castStringToNumber($value)
    {
        return explode(',' $value);
    }
    /**
     * @Given /^article "([^"]*)" is published at "([^"]*)"$/
     */
    public function givenArticleIsPublishedAtPages($title, $pages){
      foreach ($pages as $page) {
      // ...
    }
  }
Option 2
Another option is to explode a comma-separated string:
Behat step
Given article "Test article" is published at "Foo, Bar"
Step code:
  /**
   * @Given /^article "([^"]*)" is published at "([^"]*)"$/
   */
  public function givenArticleIsPublishedAtMediums($title, $mediums){
    // Explode mediums from a string.
    foreach (explode(',', $mediums) as $medium) {
      // ...
    }
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With