Я пытаюсь сделать веб-тест для моего приложения restfull API.
Я использую FosRestBundle, и моя проблема в том, что я не знаю, как создать свой запрос.
Это моя конфигурация fos-rest:
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
Это мой веб-тест:
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 a 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();
}
И мой метод postAction
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);
}
}
Проблема в том, что условие $form->isValid() возвращает false, потому что все атрибуты моего объекта равны нулю.
Поэтому я думаю, что пропустил сборку моего запроса, потому что $form->handleRequest($request) не устанавливает атрибуты, которые я отправляю своему объекту.
У вас есть идеи, почему? Вы знаете, как построить мой запрос?
Спасибо, AlexL