Version

Documentation » Core

« 11. Command

12. Testing

12.1. Test Widgets

You can write unit tests for Twig form rendering with the following code:

use Sonata\Form\Test\AbstractWidgetTestCase;

class CustomTest extends AbstractWidgetTestCase
{
    public function testAcmeWidget()
    {
        $options = [
            'foo' => 'bar',
        ];

        $form     = $this->factory->create('Acme\Form\CustomType', null, $options);
        $html     = $this->renderWidget($form->createView());
        $expected = '<input foo="bar"/>';

        $this->assertContains($expected, $this->cleanHtmlWhitespace($html));
    }
}

Found a typo? Something is wrong in this documentation? Just fork and edit it!