How to test a symfony form type with constructor
<p>i'm currently struggle with the TypeTestCase from Symfony testing a form type with an constructor. While the offical <a href="http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html#creating-your-field-type-as-a-service" rel="nofollow">solution</a> registring a form as a service works well in the application, the same form type cannot be tested with TypeTestCase. Since TypeTestCase extends from FormIntegrationTestCase and extends from \PHPUnit_Framework_TestCase instead from KernelTestCase. The FormFactory in TypeTestCase doesn't/can't look for forms registered as services and throw an error calling phpunit:</p>

<pre><code>Tests\DemoBundle\Form\Type\DemoTypeTest::testSubmitValidData
Missing argument 1 for DemoBundle\Form\Type\DemoType::__construct(), called in /var/www/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 90 and defined
</code></pre>

<p>The only way i've currently see is not serving the dependency via constructor using the optionsResolver in the configureOptions set an required element with the setRequired method for the dependency.</p>

<p>Here the relevant parts from the code:</p>

<p>DemoType.php</p>

<pre><code>[...]
class DemoType extends AbstractType
{
    /**
     * @var EntityManager
     */
    protected $entityManager;

    public function __construct($entityManager)
    {
        $this-&gt;entityManager = $entityManager;
    }
[...]
</code></pre>

<p>services.yml</p>

<pre><code>app.form.type.demo:
    class: DemoBundle\Form\Type\DemoType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }
</code></pre>

<p>DemoTypeTest.php</p>

<pre><code>[...]
class DemoTypeTest extends TypeTestCase
{

    protected function getExtensions()
    {
        $classMetadata = $this-&gt;getMock(ClassMetadata::class, [], [], '', false);
        /* @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject $validator */
        $validator = $this-&gt;getMock(ValidatorInterface::class);
        $validator-&gt;method('validate')-&gt;will($this-&gt;returnValue(new ConstraintViolationList()));
        $validator-&gt;method('getMetadataFor')-&gt;will($this-&gt;returnValue($classMetadata));
        return [
            new ValidatorExtension($validator),
        ];
    }

    public function testSubmitValidData()
    {
        $formData = [
            'some' =&gt; 'data',
        ];

        $form = $this-&gt;factory-&gt;create(DemoType::class);

[...]
</code></pre>