In my Symfony App, I'm currently writting the test of my forms following the documentation. In a contact form, when the data are validated I empty the form for further usage. I want to check this behaviour in my tests.
I have 4 fields that contains several constrains that I didn't represent here :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('subject', TextType::class)
->add('email', EmailType::class)
->add('message', TextareaType::class)
;
}
in my tests I use the following function in a class that extends WebTestCase
:
public function testContactForm(string $subject, string $name, string $email, string $message, int $nbErrors)
{
$form_name = 'contact';
//do some test, submit etc...
//check that the form is cleaned if valid
if (!$nbErrors) {
$this->assertInputValueSame($form_name . '[subject]', '');
$this->assertInputValueSame($form_name.'[name]', '');
$this->assertInputValueSame($form_name . '[email]', '');
$this->assertInputValueSame($form_name . '[message]', '');
}
}
of course $this->assertInputValueSame($form_name . '[message]', '');
doesn't work because message
is a textArea
. I thus tried:
$this->assertSelectorTextContains('#'.$name.'_message', '');
but obtained the following error
mb_strpos(): Empty delimiter
So what is the good way to test that a textArea input is empty in Symfony 4 ?
If you go to the definition of
assertInputValueSame
:You see that the function is looking for a
input
delimiter which is not the case fortextarea
. Instead you can use :