title: How to unit test Symfony 4 Form with EntityType field
body: <h1>How to unit test Symfony 4 Form with EntityType field</h1>
  
  <p>When I run my test:</p>
  
  <blockquote>
    <p>$ ./vendor/bin/simple-phpunit tests/Unit/Form/ProductFormTest.php</p>
  </blockquote>
  
  <p>This is the output in my terminal: </p>
  
  <blockquote>
    <p>PHPUnit 6.5.8 by Sebastian Bergmann</p>
    
    <p>and contributors.</p>
    
    <p>Runtime:       PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 with Xdebug
    2.7.0alpha2-dev Configuration: /var/www/project/phpunit.xml.dist</p>
    
    <p>Testing App\Tests\Unit\Form\ProductFormTest E<br>
    1 / 1 (100%)</p>
    
    <p>Time: 551 ms, Memory: 6.00MB</p>
    
    <p>There was 1 error:</p>
    
    <p>1) App\Tests\Unit\Form\ProductFormTest::formSubmitsValidData
    Symfony\Component\Form\Exception\RuntimeException: Class
    "App\Entity\Supplier" seems not to be a managed Doctrine entity. Did
    you forget to map it?</p>
    
    <p>/var/www/project/vendor/symfony/doctrine-bridge/Form/Type/DoctrineType.php:205
    /var/www/project/vendor/symfony/options-resolver/OptionsResolver.php:858
    /var/www/project/vendor/symfony/doctrine-bridge/Form/Type/DoctrineType.php:130
    /var/www/project/vendor/symfony/options-resolver/OptionsResolver.php:766
    /var/www/project/vendor/symfony/options-resolver/OptionsResolver.php:698
    /var/www/project/vendor/symfony/form/ResolvedFormType.php:95
    /var/www/project/vendor/symfony/form/FormFactory.php:76
    /var/www/project/vendor/symfony/form/FormBuilder.php:97
    /var/www/project/vendor/symfony/form/FormBuilder.php:256
    /var/www/project/vendor/symfony/form/FormBuilder.php:206
    /var/www/project/vendor/symfony/form/FormFactory.php:30
    /var/www/project/tests/Unit/Form/ProductFormTest.php:86</p>
    
    <p>ERRORS! Tests: 1, Assertions: 0, Errors: 1.</p>
  </blockquote>
  
  <p>This error started after mocking the ManagerRegistry class. It seems that in this unit test there is no mapping for doctrine entities present. </p>
  
  <p>Is there a clean way to test a form with "Symfony\Bridge\Doctrine\Form\Type\EntityType" fields? </p>
  
  <p><em>src\App\Entity\Product.php</em> </p>
  
  <pre><code>namespace App\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use App\Entity\Supplier;
  
  /**
   * Product Entity
   * 
   * @ORM\Entity(repositoryClass = "App\Repository\ProductRepository")
   * @ORM\Table(name = "product")
   */
  class Product
  {
      /**
       * Constructor
       */
      public function __construct()
      {
          parent::__construct();
  
          $this-&gt;setType(AbstractProduct::TYPE_PARENT);
      }
  
      /**
       * To String
       * 
       * @return string
       */
      public function __toString()
      {
          return "[" . $this-&gt;id . "] Product: " . $this-&gt;ean . " | " . $this-&gt;name;
      }
  
      /**
       * ID
       *
       * @var integer
       *
       * @ORM\Id
       * @ORM\Column(name = "product_id", type = "integer")
       * @ORM\GeneratedValue(strategy = "AUTO")
       */
      protected $id;
  
      /**
       * EAN (European Article Number)
       *
       * @var string
       *
       * @ORM\Column(name = "product_ean", type = "string", length = 13)
       */
      protected $ean;
  
      /**
       * Name
       *
       * @var string
       *
       * @ORM\Column(name = "product_name", type = "string", length = 128)
       */
      protected $name;
  
      /**
       * Description
       *
       * @var string
       *
       * @ORM\Column(name = "product_description", type = "text", nullable = true)
       */
      protected $description;
  
      /**
       * Supplier
       *
       * Many Products have one Supplier
       *
       * @var Supplier
       *
       * @ORM\ManyToOne(targetEntity = "Supplier", inversedBy = "products")
       * @ORM\JoinColumn(name = "supplier_id", referencedColumnName = "supplier_id")
       */
      protected $supplier;
  
      /**
       * Get id
       *
       * @return integer
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set ean
       *
       * @param string $ean
       *
       * @return AbstractProduct
       */
      public function setEan($ean)
      {
          $this-&gt;ean = $ean;
  
          return $this;
      }
  
      /**
       * Get ean
       *
       * @return string
       */
      public function getEan()
      {
          return $this-&gt;ean;
      }
  
      /**
       * Set name
       *
       * @param string $name
       *
       * @return AbstractProduct
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set description
       *
       * @param string $description
       *
       * @return AbstractProduct
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Set supplier
       *
       * @param \App\Entity\Supplier $supplier
       *
       * @return Product
       */
      public function setSupplier(Supplier $supplier = null)
      {
          $this-&gt;supplier = $supplier;
  
          return $this;
      }
  
      /**
       * Get supplier
       *
       * @return \App\Entity\Supplier
       */
      public function getSupplier()
      {
          return $this-&gt;supplier;
      }
  }
  </code></pre>
  
  <p><em>src\App\Form\ProductForm.php</em></p>
  
  <pre><code>namespace App\Form;
  
  use App\Entity\Supplier;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormBuilderInterface;
  
  class ProductForm extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $translationDomain = "product";
  
          /*
           * Card
           */
          $builder-&gt;add("ean", TextType::class, [
              "label"              =&gt; "product.ean",
              "required"           =&gt; true,
              "translation_domain" =&gt; $translationDomain,
          ]);
  
          $builder-&gt;add("name", TextType::class, [
              "label"              =&gt; "product.name",
              "required"           =&gt; true,
              "translation_domain" =&gt; $translationDomain,
          ]);
  
          $builder-&gt;add("supplier", EntityType::class, [
              "class"              =&gt; Supplier::class,
              "choice_label"       =&gt; "name",
              "label"              =&gt; "supplier.name",
              "required"           =&gt; false,
              "translation_domain" =&gt; "supplier",
          ]);
      }
  }
  </code></pre>
  
  <p><em>tests\Unit\Form\ProductFormTest.php</em></p>
  
  <pre><code>namespace App\Tests\Unit\Form;
  
  use App\Entity\Product;
  use App\Form\ProductForm;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Bridge\Doctrine\ManagerRegistry;
  use Symfony\Component\Form\PreloadedExtension;
  use Symfony\Component\Form\Test\TypeTestCase;
  
  class ProductFormTest extends TypeTestCase
  {
      /**
       * @var ManagerRegistry
       */
      private $_managerRegistry;
  
      /**
       * {@inheritdoc}
       */
      protected function setUp()
      {
          $this-&gt;_managerRegistry = $this-&gt;createMock(ManagerRegistry::class);
  
          parent::setUp();
      }
  
      /**
       * {@inheritdoc}
       */
      protected function tearDown()
      {
          $this-&gt;_managerRegistry = null;
  
          parent::tearDown();
      }
  
      /**
       * {@inheritdoc}
       */
      protected function getExtensions()
      {
          $entityType = new EntityType($this-&gt;_managerRegistry);
  
          return [
              new PreloadedExtension([$entityType], [])
          ];
      }
  
      /**
       * @test
       */
      public function formSubmitsValidData()
      {
          $createdAt = new \DateTime();
  
          $formData = [
              "ean"         =&gt; "8718923400440",
              "name"        =&gt; "Plumbus",
              "description" =&gt; "This is a household device so common it does not need an introduction",
          ];
  
          $productComparedToForm = new Product();
          $productComparedToForm
              -&gt;setEan($formData["ean"])
              -&gt;setName($formData["name"])
          ;
  
          $productHandledByForm = new Product();
  
          $form = $this-&gt;factory-&gt;create(ProductForm::class, $productHandledByForm);
  
          $form-&gt;submit($formData);
  
          static::assertTrue($form-&gt;isSynchronized());
          static::assertEquals($productComparedToForm, $productHandledByForm);
  
          $view = $form-&gt;createView();
  
          foreach (array_keys($formData) as $key) {
              static::assertArrayHasKey($key, $view-&gt;children);
          }
      }
  }
  </code></pre>