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

谁都会走 提交于 2019-12-24 04:09:11

问题


I am trying to test a form in symfony 2.3 that has a select input... along with a file upload (enctype multipart/form-data)

The select input is as follows...

  • It is a required field.
  • Has 3 options [1, 2, 3]

with the DomCrawler i select the form

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

then try to set the value of the select with

$form['select'] = null or $form['select'] = ssjksjkajsdfj

There is an internal validation system in the DomCrawler that returns the following error if i do that.

InvalidArgumentException: Input "select" cannot take "ssjksjkajsdfj" as a value (possible values: 1, 2, 3).

In symfony 2.4 and up there is a magic method in DomCrawler/Form called disableValidation() and that works very well. Unfotunately because of some dependencies requiring 2.3 I cannot upgrade

i also tried to use the $client->Request() method directly

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

But symfony does not seem to know/care about the form and just returns a regular form without any of the error messages, the form handler doesn't validate the form for some reason.

Here's is the updateAction in the controller

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(),
    ));
}

Here i found an exact question... Select impossible value in select inputs with Symfony DomCrawler

but the answer is not what i am looking for. I want to test and make sure that the server will return a 200 along with a message letting the user know what they are tying to do is not possible.

PS: I can achieve desired results with Postman in chrome.


回答1:


I don't know of any method in SF 2.3 that works as disableValidation. However, I think sending a fake request should do the trick. I also think the way you're using the request method is not correct. Check the documentation for the 2.3 SF version, you have an example on how to fake a request:

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

it should send the request and the controller should answer with the 200 and the error if everything's allright.

Hope it helps.

UPDATE

Ok, a couple of things here: CSRF is important and the format of the parameters in the request too.

Since your form is using CSRF, you should send it along in the request or it will fail and redirect you to the "empty" form. Also, the request must send the data in the proper format. Supposing the generated code of the form names the inputs in the form my_form[name] you should create them in the request accordingly:

$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.
            ),
        ));


来源:https://stackoverflow.com/questions/28075552/how-to-submit-an-invalid-select-option-in-webtestcase-with-symfony-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!