0

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! ;)

MarcSitges
  • 282
  • 3
  • 11

1 Answers1

0

For your question about bundle, WebTestCase from Symfony\Bundle\FrameworkBundle\Test\WebTestCase is really nice to do test on REST API in Symfony project.

In POST, data are in body and not has parameter. (How are parameters sent in an HTTP POST request?)

Try

$this->client->request(
    'POST', 
    '/api/application/save/',
    array(), // The Request parameters
    array(), // Files
    array(),
    $postData, // Raw Body Data
    true
);
Community
  • 1
  • 1
François Dupont
  • 375
  • 3
  • 18
  • In our application, data is sent within the request parameters. I am using WebTestCase from Liip Bundle, which adds a few interesting features. – MarcSitges Apr 15 '16 at 10:21