how to test the value of a textArea?

589 views Asked by At

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 ?

1

There are 1 answers

0
Pierrick Rambaud On BEST ANSWER

If you go to the definition of assertInputValueSame:

public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
    {
        self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
            new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
            new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
        ), $message);
    }

You see that the function is looking for a input delimiter which is not the case for textarea. Instead you can use :

$this->assertSelectorTextSame('#'.$form_name.'_message', '');