Ral*_*MPS 5 forms phpunit symfony
我目前正在使用 Symfony2 并使用 PHPUnit 测试我的项目。我想测试提交表单时参数错误或 URL 不完整时的异常。
我浏览了 Symfony2 和 PHPUnit 的文档,但没有找到任何类/方法来执行此操作。
如何更改表单操作的值?我想使用 PHPUnit,以便创建的报告是最新的,并且我可以看到我的代码的覆盖范围。
编辑:
为了澄清我的问题,一些新内容。如何在控制器中测试以“>”开头的行?( throw $this->createNotFoundException('Unable to find ParkingZone entity.');
)
当用户修改操作链接时,在控制器中,该过程将经历异常(或错误消息,如果选择此操作)。我该如何测试这个案例?
控制器
/**
* Edits an existing ParkingZone entity.
*
* @Route("/{id}/update", name="parkingzone_update")
* @Method("post")
* @Template("RatpGarageL1Bundle:ParkingZone:edit.html.twig")
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('RatpGarageL1Bundle:ParkingZone')->find($id);
if (!$entity) {
> throw $this->createNotFoundException('Unable to find ParkingZone entity.');
}
$editForm = $this->createForm(new ParkingZoneType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('parkingzone_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
Run Code Online (Sandbox Code Playgroud)
看法:
<form action="{{ path('parkingzone_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(form) }}>
<div class="control-group">
{{ form_label(form.name, 'Name', { 'attr': {'class': 'control-label'} } ) }}
<div class="controls error">
{{ form_widget(form.name, { 'attr': {'class': ''} } ) }}
<span class="help-inline">{{ form_errors(form.name) }}</span>
</div>
</div>
<div class="control-group">
{{ form_label(form.orderSequence, 'Rang', { 'attr': {'class': 'control-label'} } ) }}
<div class="controls error">
{{ form_widget(form.orderSequence, { 'attr': {'class': ''} } ) }}
<span class="help-inline">{{ form_errors(form.orderSequence) }}</span>
</div>
</div>
<div class="control-group">
{{ form_label(form.image, 'Image', { 'attr': {'class': 'control-label'} } ) }}
<div class="controls error">
{{ form_widget(form.image, { 'attr': {'class': ''} } ) }}
<span class="help-inline">{{ form_errors(form.image) }}</span>
</div>
</div>
{{ form_rest(form) }}
<div class="form-actions">
<button type="submit" class="btn btn-primary">Enregistrer</button>
<a href="{{ path('parkingzone') }}" class="btn">Annuler</a>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
Symfony 本身没有任何对象可以通过它来操纵在 html(twig 文件)中设置的表单操作。但是,twig 提供了动态更改 twig 文件中表单操作的功能。
基本方法是控制器通过渲染调用将参数传递到 twig 文件中。然后twig文件就可以使用这个参数来动态设置表单动作。如果控制器使用会话变量来确定该参数的值,则通过在测试程序中设置该会话变量的值,可以专门为测试设置表单操作。
例如在控制器中:
public function indexAction()
{
$session = $this->get('session');
$formAction = $session->get('formAction');
if (empty($formAction)) $formAction = '/my/normal/route';
...
return $this->render('MyBundle:XXX:index.html.twig', array(
'form' => $form->createView(), 'formAction' => $formAction)
);
}
Run Code Online (Sandbox Code Playgroud)
然后,在树枝文件中:
<form id="myForm" name="myForm" action="{{ formAction }}" method="post">
...
</form>
Run Code Online (Sandbox Code Playgroud)
然后,在测试程序中:
$client = static::createClient();
$session = $client->getContainer()->get('session');
$session->set('formAction', '/test/route');
$session->save();
// do the test
Run Code Online (Sandbox Code Playgroud)
这不是唯一的方法,还有多种可能性。例如,会话变量可以是 $testMode,如果设置了此变量,则表单会将 $testMode = true 传递到渲染调用中。然后,twig 文件可以根据 testMode 变量的值将表单操作设置为两个值之一。
归档时间: |
|
查看次数: |
7503 次 |
最近记录: |