How to Pass an Array as an Argument From a Behat Step?

While Behat, by default, does not support arrays as an argument in a step, there are ways in which we can convert a comma separated string, for example, into an array of strings. In this article, we're going to look at ways to make this possible.

For the purpose of this article, let's assume we have the following scenario step with a comma separated string of numbers:

...
When I select a random number from "1,2,3,4"
...

Using Step Argument Transformations

A step argument transformation can be used to transform a normal string argument into a more specific data type or object.

We can define a transformation method by using the @Transform annotation followed by a regular expression to specify our target. Consider, for example, the following where we target comma separated string of numbers and transform it into an array:

/**
 * @Transform "^([\d](?:,[\d])*)$"
 */
public function toArray(string $value): array
{
    return explode(',' $value);
}

Depending on your use case, however, you would of course have a different regular expression to capture/target the right string.

It might be a good idea to distinguish between the strings you wish to transform. For example, by formatting them in a unique way, such as by wrapping them in special characters (for e.g., <1,2,3,4>). Accordingly, you could have a regular expression that targets that string specifically. This will help you avoid transforming arguments by mistake.

Splitting a Comma Separated String

We could also simply pass the comma separated string as the argument to a step definition method and split it there. This would help avoid unnecessary complexity such as transforming arguments by mistake, writing complex regular expressions to selectively match, or having to format the arguments in a distinguishable way. Consider, for example, the following:

/**
 * @When /^I select a random number from "([^"]*)"$/
 */
public function iSelectARandomNumber(string $numbers)
{
    $numbersList = explode(',' $value);

    foreach ($numbersList as $number) {
        // ...
    }
}

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.