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

How to submit an invalid select option in WebTestCase with symfony 2.3

我正在尝试在 symfony 2.3 中测试具有 select 输入...以及文件上传 (enctype multipart/form-data)

的表单

select输入如下...

与 DomCrawler i select 表单

$form = $crawler->selectButton->('Update')->form()

然后尝试将 select 的值设置为

$form['select'] = null 或者 $form['select'] = ssjksjkajsdfj

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

InvalidArgumentException: Input "select" cannot take "ssjksjkajsdfj" as a value (possible values: 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 似乎不 know/care 形式,只是 return 一个 没有任何错误消息的常规表单,表单处理程序出于某种原因不验证表单。

这是控制器中的 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

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

PS:我可以在 chrome.

中使用 Postman 达到预期的效果

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

// 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)
);

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

希望对您有所帮助。

更新

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

由于您的表单正在使用 CSRF,您应该在请求中发送它,否则它会失败并将您重定向到 "empty" 表单。此外,请求必须以正确的格式发送数据。假设生成的表单代码以 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.
            ),
        ));