I'm building a REST API using Symfony2. I am already using Liip bundle for my functional tests together with Alice and Faker to genereate all the fixtures. However, I have little trouble when I want to directly test POST calls themselves as long JSON are included in the POST data, which made my functions quite long, ugly and unreadable.
I decided to move the fake JSON out of the class, converting them to YAML files and then loading them using Symfony's parser:
private function loadYaml($resource){
$data = Yaml::parse(file_get_contents('src/AppBundle/DataFixtures/YAML/' . $resource . '.yml'));
return $data;
}
This seems to work quite well, since I can easily convert them back to JSON objects and then use it in the call:
$postData = json_encode($this->loadYaml('newapplication'));
$this->client->request(
'POST',
'/api/application/save/',
array('data' => $postData), // The Request parameters
array(), // Files
array(),
'mybody', // Raw Body Data
true
);
My first question is: is this a right approach? Is there any bundle that I have missed which will make my life much easier?
My second question is wheter it will be possible to use Faker within this YAML constructions. On my fixtures, I call Faker functions (e.g. < firstName() >) that when fixtures are loaded automatically fill my entities with random but meaningful values. Would it be possible to use them in these YAML constructions?
Thanks a lot! ;)