api - Build request for postAction FosRestBundle WebTestCase -
i'm trying make web test case restfull api app.
i'm using fosrestbundle , problems don't know how build request.
this fos-rest config:
fos_rest: disable_csrf_role: role_api param_fetcher_listener: true body_listener: true format_listener: true view: view_response_listener: 'force' formats: xml: true json : true templating_formats: html: true force_redirects: html: true failed_validation: http_bad_request default_engine: twig routing_loader: default_format: json
this web test case :
public function test_postaction() { $client = static::createclient(); $parameters = [ 'contenttext' => 'lorem ipsum dolor sit amet, consectetur adipiscing elit. mauris lobortis sapien ac magna hendrerit tincidunt. nunc mi dui, rhoncus nec justo et, rutrum lobortis sem. fusce venenatis orci nec sodales convallis. nunc in quam nibh semper convallis viverra ac augue.', 'tasks' => [], 'title' => 'lorem ipsum dolor sit amet', ]; $url = $client ->getcontainer() ->get('router') ->generate('post_note', [ '_format' => 'json', ]) ; $client->request( 'post', $url, $parameters ); $response = $client->getresponse(); }
and postaction method
public function postaction(request $request) { try { $note = new note(); /** @var $form form*/ $form = $this->createform(new notetype(), $note); $this->removeextrafields($request, $form); $form->handlerequest($request); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($note); $em->flush(); return fosview::create($note, codes::http_ok); } return fosview::create(array('errors' => $form->geterrors()), codes::http_internal_server_error); } catch (\exception $e) { return fosview::create($e->getmessage(), codes::http_internal_server_error); } }
the problem condition $form->isvalid() return false because attributes of object null.
so think miss build request because $form->handlerequest($request) dosen't set attributes i've send object.
do have idea why ? know how build request ?
thanks, alexl
this might , snippet test have 100% working.
$crawler = $this->client->request( 'post' , '/api/validate' , [] , [] , [ 'content_type' => 'application/json' ] , json_encode($data) );
$data being in case $parameters. shouldn't need json_encode bit should work either way believe.
Comments
Post a Comment