I have an API built in Symfony using the FOSRestBundle.
I want to write a simple test to validate whether when I submit some JSON that the results get properly processed and the expected response gets returned.
Working test code
$user = $this->getResourceContent('userValid.json');
$this->client->request('POST', '/users', json_decode($user, true), [], $this->headers);
I can get the request to successfully submit by providing the 'user' as form POST values. However this is not the behavior that I am trying to test.
Failing test case
$user = $this->getResourceContent('userValid.json');
$this->client->request('POST', '/users', [], [], $this->headers, $user);
This is what I want to test providing the JSON as the request body so that it can be processed. However this does not work since the $form->isValid()
check fails since the form has not been submitted.
Controller action
public function postUserAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user, ['method' => 'POST']);
$form->handleRequest($request);
if ($form->isValid()) {
// TODO: Persist user
$view = $this->jsendView(['user' => $user], 201, Jsend::SUCCESS);
$view->getSerializationContext()->setGroups(['Default', 'private']);
return $view;
}
return $form;
}