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");
}
}
...
I was following a tutorial that added the first test objects via REST, but that doesn't have to happen.
I have also learned that nested tables for Behat are a bad idea.
https://joebuschmann.com/specflow-nested-tables-a-bad-idea/
behat.yml
default:
suites:
default:
contexts:
- App\Features\Bootstrap\FeatureContext:
container: '@service_container'
entityManager: "@doctrine.orm.default_entity_manager"
- Imbo\BehatApiExtension\Context\ApiContext
extensions:
Imbo\BehatApiExtension:
apiClient:
base_uri: http://127.0.0.1:8000
Behat\Symfony2Extension:
kernel:
bootstrap: config\behat\bootstrap.php
class: App\Kernel
Code below is not exactly correct, but it's close enough.
FeatureContext.php
...
/**
* @Given there are Albums with the following details:
*/
public function thereAreAlbumsWithTheFollowingDetails(TableNode $table) {
foreach ($table->getColumnsHash() as $row) {
$album = new Album();
$album->setTitle($row['title']);
$album->setReleaseDate(new \DateTime($row['release_date']));
array_push($this->entities, $album);
}
$this->em->flush();
}
/**
* @Given albums with the following Songs:
*/
public function albumsWithTheFollowingSongs(TableNode $table) {
$i = 0;
foreach ($table->getColumnsHash() as $row) {
$song = new Song($row['title']);
$this->entities[$i]->setSong($song);
$i = $i + 1;
}
}
/**
* @When they are saved into the database
*/
public function theyAreSavedIntoTheDatabase() {
foreach ($this->entities as $entity) {
$this->em->persist($entity);
}
$this->em->flush();
}
...
For what I understand you want to add some data to set up the scenario, in that case I would move away from json as that's an implementation detail:
Given there are Albums with the following details:
| artist | title | songs |
| Pink Floyd | The Dark Side of the Moon | Speak to Me, Breathe, On the Run |
| AC/DC | Back to Black | Hells Bells, Shoot to Thrill, What Do You Do for Money Honey |
...
Then on FeatureContext transform the data to json if you need that, but personally, if you've done it correctly, I would just inject the same service that should be used in /api/album controller to create the Albums.