gpt4 book ai didi

php - 如何使用 symfony 2.3 在 WebTestCase 中提交无效的选择选项

转载 作者:行者123 更新时间:2023-12-05 05:26:06 25 4
gpt4 key购买 nike

我正在尝试在 symfony 2.3 中测试一个表单,该表单具有一个选择输入...以及一个文件上传(enctype multipart/form-data)

选择输入如下...

  • 这是必填字段。
  • 有 3 个选项 [1, 2, 3]

我用 DomCrawler 选择表单

$form = $crawler->selectButton->('更新')->form()

然后尝试设置select的值

$form['select'] = null要么$form['select'] = ssjksjkajsdfj

如果我这样做,DomCrawler 中有一个内部验证系统会返回以下错误。

InvalidArgumentException:输入“select”不能将“ssjksjkajsdfj”作为值(可能的值:1、2、3)。

在 symfony 2.4 及更高版本中,DomCrawler/Form 中有一个名为 disableValidation() 的神奇方法,它运行良好。不幸的是,由于某些依赖项需要 2.3,我无法升级

我也试过直接使用$client->Request()方法

$post_data = '------WebKitFormBoundaryi7fAxO2jrDFTmxef
Content-Disposition: form-data; name="select"

ssjksjkajsdfj
------WebKitFormBoundaryi7fAxO2jrDFTmxef--';

$client->request(
'POST',
$form->getUri(),
array(),
array(),
array(
'CONTENT_TYPE' => 'multipart/form-data; boundary=----WebKitFormBoundaryi7fAxO2jrDFTmxef',
),
$post_data
);

但是 symfony 似乎并不知道/不关心表单,只是返回一个没有任何错误消息的常规表单,表单处理程序出于某种原因不验证表单。

这是 Controller 中的updateAction

public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('CoyoteAdBundle:Ad')->find($id);

if (!$entity) {
throw $this->createNotFoundException('Unable to find Ad entity.');
}
$old_entity = $entity;

$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);

if ($editForm->isValid()) {

$this->archiveImage($old_entity);

$entity->upload();
$em->flush();
$this->addSuccessFlash('Ad has been updated successfully!');
return $this->redirect($this->generateUrl('ad_show', array('id' => $id)));
}

return $this->render('CoyoteAdBundle:Ad:edit.html.twig', array(
'entity' => $entity,
'form' => $editForm->createView(),
));
}

在这里我发现了一个确切的问题...... Select impossible value in select inputs with Symfony DomCrawler

但答案不是我要找的。我想测试并确保服务器将返回 200 以及一条消息,让用户知道他们想要做什么是不可能的。

PS:我可以在 Chrome 中使用 Postman 达到预期的效果。

最佳答案

我不知道 SF 2.3 中有任何方法可以用作 disableValidation。但是,我认为发送虚假请求应该可以解决问题。我还认为您使用请求方法的方式不正确。检查documentation对于 2.3 SF 版本,您有一个关于如何伪造请求的示例:

// Form submission with a file upload
use Symfony\Component\HttpFoundation\File\UploadedFile;

$photo = new UploadedFile(
'/path/to/photo.jpg',
'photo.jpg',
'image/jpeg',
123
);// Fake photo upload if you like, if not, just send the select value.

$client->request(
'POST',
'/submit',// valid route
array('select' => 'ssjksjkajsdfj'),// Substitute here the name of your select and the value you want to send
array('photo' => $photo)
);

它应该发送请求,如果一切正常, Controller 应该用 200 和错误进行应答。

希望对您有所帮助。

更新

好的,这里有几件事:CSRF 很重要,请求中的参数格式也很重要。

由于您的表单使用的是 CSRF,您应该在请求中发送它,否则它将失败并将您重定向到“空”表单。此外,请求必须以正确的格式发送数据。假设生成的表单代码以 my_form[name] 的形式命名输入,您应该相应地在请求中创建它们:

$csrfToken = $client->getContainer()->get('form.csrf_provider')->generateCsrfToken('my_form');// Generate CSRF for my_form
$crawler = $client->request(
'POST',
'/ajax/register', // valid route
array(
'my_form' => array(// form accepts params in form my_form[name]
'_token' => $csrfToken,
'select' => 'asdfasdf',// select I want to send invalid value
'photo' => $photo, // the uploaded photo
'whatever' => 'whatever', // rest of required parameters.
),
));

关于php - 如何使用 symfony 2.3 在 WebTestCase 中提交无效的选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com