Skip to content
Advertisement

How to test form submission with wrong values using Symfony crawler component and PHPUnit?

When you’re using the app through the browser, you send a bad value, the system checks for errors in the form, and if something goes wrong (it does in this case), it redirects with a default error message written below the incriminated field.

This is the behaviour I am trying to assert with my test case, but I came accross an InvalidArgumentException I was not expecting.

I am using the symfony/phpunit-bridge with phpunit/phpunit v8.5.23 and symfony/dom-crawler v5.3.7. Here’s a sample of what it looks like :

JavaScript

Here’s the exception message I get :

JavaScript

The ColorChoiceType tested here is pretty standard :

JavaScript

What I can do, is to wrap in a try-catch block, the line where it sets the wrong value. And it would indeed submit the form and proceed to the next assertion. The issue here is that the form was considered submitted and valid, it forced an appropriate value for the color field (the first choice of the enum set). This is not what I get when I try this in my browser (cf. the intro).

JavaScript

How can I mimic the browser behaviour in my test case and make asserts on it ?

Advertisement

Answer

It seems that you can disable validation on the DomCrawlerForm component. Based on the official documentation here.

So doing this, now works as expected :

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement