Search code examples
phpautomationbehat

Behat Scenario Outline Float values


Consider the following scenario for API testing

Given I am using the API Service
When  I send the <request> as <method> to <endpoint> endpoint with <key> having value <value>
Then  The response status code is 200

Examples:
  | request    | method | endpoint       | key            | value          |
  | "All Keys" | "POST" | "endpointName" | "numericField" | 15             |
  | "All Keys" | "POST" | "endpointName" | "numericField" | 15.12345       |

The above example creates a request with the specified parameters.

My problem is that while the integer (15) value is passed to the function accordingly, the float (15.12345) is converted into a string ("15.12345"). This happens straight as the function is called; it is not modified later on during another step.

Is there a way to keep the float value from turning into a string?

As requested, the send request step method is:

    $data = $this->fulfilmentOptions->getDataValue($request);
    $uri = $this->getMinkParameter('base_url') . $this->setEndpoint($endpoint);

    array_walk_recursive($data, function(&$item, $originalKey) use ($key, $value) {
        if ($originalKey === $key) {
            $item = $value;
        }
    });

    try {
        $this->response = $this->client->request($method, $uri, [
            'headers' => $this->fulfilmentOptions->getDataValue('CreateOrder API Headers'),
            'body' => json_encode($data)
        ]);
    } catch (\GuzzleHttp\Exception\RequestException $e) {
        $this->response = $e->getResponse();
    }

    $this->responseContent = $this->response->getBody()->getContents();

Solution

  • One way of fixing the issue is to use floatval method for 'value' key to make sure you get the right type.