As I'm not aware if behat can inject my Behat FeatureContext function parameters with anything but strings, I'd like to know if I could split strings in such a way that I am left with an array of json_objects.
I've managed to do this with json_decode
and json_encode
, but that feels a bit repetitive as I first decode the string of objects,
only to encode it back into a single object.
Per example have the following Behat feature:
Feature: Provide a consistent standard JSON API endpoint
In order to build interchangeable front ends
As a JSON API developer
I need to allow Create, Read, Update, and Delete functionality
Background:
Given there are Albums with the following details:
"""
[{
"artist":"Pink Floyd",
"title":"The Dark Side of the Moon",
"songs":[
{"title":"Speak to Me", "length":254}
{"title":"Breathe", "length":192}
{"title":"On the Run", "length":188}
]
},
{
"artist":"AC/DC",
"title":"Back to Black",
"songs":[
{"title":"Hells Bells", "length":205}
{"title":"Shoot to Thrill", "length":302}
{"title":"What Do You Do for Money Honey", "length":244}
]
}]
"""
And the "Content-Type" request header is "application/json"
and the following function in FeatureContext.php:
...
public function thereAreAlbumsWithTheFollowingDetails(string $jsonString) {
$albums = json_decode($jsonString, true);
foreach ($albums as $album) {
$albumJson = json_encode($album);
$this->apiContext->setRequestBody($albumJson);
$this->apiContext->requestPath("/api/album", "POST");
}
}
...