title: How to mock a translator service to unit test a form component that depends on it?
body: <p>In my Symfony app I use a form to send emails to the administrator: </p>
  
  <pre class="lang-php prettyprint-override"><code>class ContactType extends AbstractType
  {
      private $translator;
  
      public function __construct(TranslatorInterface $translator)
      {
          $this-&gt;translator = $translator;
      }
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name', TextType::class, [
                  'label' =&gt; false,
                  'attr' =&gt; ['placeholder' =&gt; $this-&gt;translator-&gt;trans('contact.name.placeholder')],
                  'constraints' =&gt; [
                      new NotBlank(["message" =&gt; $this-&gt;translator-&gt;trans('contact.name.not-blank')])
                  ]
              ])
              //some other email fields 
          ;
      }
  
      public function setDefaultOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'error_bubbling' =&gt; true
          ));
      }
  
      public function getName()
      {
          return 'contact_form';
      }
  }
  </code></pre>
  
  <p>As you can see this class is dependant on the <code>translator</code> service which automatically done with <code>autowired</code> set to <code>true</code> in my config. </p>
  
  <p>Now I need to unit test this <code>contactType</code> so following the <a href="https://symfony.com/doc/current/form/unit_testing.html" rel="nofollow noreferrer">documentation</a> I created this little test:</p>
  
  <pre class="lang-php prettyprint-override"><code>class ContactTypeTest extends TypeTestCase
  {
      public function testSubmitValidData()
      {
          $formData = [
              'subject'   =&gt; 'Subject',
              'name'      =&gt; 'Pierrick',
              'email'     =&gt; 'pierrick@domaine.com',
              'message'   =&gt; 'Team-Manager Rocks !'
          ];
  
          $form = $this-&gt;factory-&gt;create(ContactType::class, null);
  
          // submit the data to the form directly
          $form-&gt;submit($formData);
  
          $this-&gt;assertTrue($form-&gt;isSynchronized());
  
          $view = $form-&gt;createView();
          $children = $view-&gt;children;
  
          foreach (array_keys($formData) as $key) {
              $this-&gt;assertArrayHasKey($key, $children);
          }
      }
  }
  </code></pre>
  
  <p>But when executing <code>./vendor/bin/simple-phpunit</code>I get the following error:</p>
  
  <blockquote>
    <p>There was 1 error:</p>
    
    <p>1) App\tests\Form\ContactTypeTest::testSubmitValidData
    ArgumentCountError: Too few arguments to function App\Form\ContactType::__construct(), 0 passed in /Users/pierrickrambaud/Sites/team-manager/vendor/symfony/form/FormRegistry.php on line 92 and exactly 1 expected</p>
  </blockquote>
  
  <p>Is it possible to mock the service or alternatively is it possible to use <code>$this-&gt;factory-&gt;create(MyFormType::class, $object)</code> and give parameter to the <code>Type</code> constructor?</p>