Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I already got the same problem when testing some of my customized Types.</p> <p>Here's the way I figure it out (by mocking EntityType),</p> <p><strong>First, make sure your test class extends <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Test/TypeTestCase.php" rel="noreferrer">TypeTestCase</a>,</strong></p> <pre><code>class MyTypeTest extends TypeTestCase { // ... } </code></pre> <p><strong>Then, add a <a href="https://github.com/symfony/Form/blob/master/PreloadedExtension.php" rel="noreferrer">preloaded extension</a> to your <a href="http://api.symfony.com/2.3/Symfony/Component/Form/FormFactory.html" rel="noreferrer">form factory</a> in order to take into account the <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php" rel="noreferrer">EntityType</a></strong></p> <pre><code>protected function setUp() { parent::setUp(); $this-&gt;factory = Forms::createFormFactoryBuilder() -&gt;addExtensions($this-&gt;getExtensions()) -&gt;getFormFactory(); } // Where this-&gt;getExtensions() returns the EntityType preloaded extension // (see the last step) } </code></pre> <p><strong>And finally, add an <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php" rel="noreferrer">Entity Type</a> mock to your <a href="https://github.com/symfony/Form/blob/master/PreloadedExtension.php" rel="noreferrer">preloaded extension</a>.</strong></p> <pre><code>protected function getExtensions() { $mockEntityType = $this-&gt;getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType') -&gt;disableOriginalConstructor() -&gt;getMock(); $mockEntityType-&gt;expects($this-&gt;any())-&gt;method('getName') -&gt;will($this-&gt;returnValue('entity')); return array(new PreloadedExtension(array( $mockEntityType-&gt;getName() =&gt; $mockEntityType, ), array())); } </code></pre> <p><strong>But, you may need to ...</strong> </p> <p>Mock the <a href="http://api.symfony.com/2.0/Symfony/Bundle/DoctrineBundle/Registry.html" rel="noreferrer">registry</a> that <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php" rel="noreferrer">DoctrineType</a> takes as parameter when calling its default constructor because it's used by <code>setDefaultOptions()</code> (Keep in mind that <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php" rel="noreferrer">EntityType</a> extends <a href="https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php" rel="noreferrer">DoctrineType</a>) to take into account <a href="http://symfony.com/doc/current/reference/forms/types/entity.html#class" rel="noreferrer">class</a> and <a href="http://symfony.com/doc/current/reference/forms/types/entity.html#property" rel="noreferrer">property</a> options of your <a href="http://symfony.com/doc/current/reference/forms/types/entity.html" rel="noreferrer">Entity field</a>.</p> <p>Your may then need to mock the entityType as follow:</p> <pre><code>$mockEntityManager = $this-&gt;getMockBuilder('\Doctrine\ORM\EntityManager')-&gt;getMock(); $mockRegistry = $this-&gt;getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry') -&gt;disableOriginalConstructor() -&gt;setMethods(array('getManagerForClass')) -&gt;getMock(); $mockRegistry-&gt;expects($this-&gt;any())-&gt;method('getManagerForClass') -&gt;will($this-&gt;returnValue($mockEntityManager)); $mockEntityType = $this-&gt;getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType') -&gt;setMethods(array('getName')) -&gt;setConstructorArgs(array($mockRegistry)) -&gt;getMock(); $mockEntityType-&gt;expects($this-&gt;any())-&gt;method('getName') -&gt;will($this-&gt;returnValue('entity')); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload