Symfony 3 FormTypeTest

Following the Symfony 3.0 tutorial on testing form forms, I followed the example of testing forms with dependencies as follows:

<?php namespace Tests\AppBundle\Form; use AppBundle\Form\Type\Database\OfficeAssignType; use Symfony\Component\Form\PreloadedExtension; use Symfony\Component\Form\Test\TypeTestCase; class OfficeTypeTest extends TypeTestCase { private $entityManager; protected function setUp() { // mock any dependencies $this->entityManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager'); parent::setUp(); } protected function getExtensions() { // create a type instance with the mocked dependencies $office = new OfficeType($this->entityManager); return array( // register the type instances with the PreloadedExtension new PreloadedExtension(array($office), array()), ); } public function testSubmitValid() { $formData = array( 'name' => 'new test office', 'address1' => 'Test new office address', 'city' => 'Test office city', 'phone' => ' testoffice@stevepop.com ', 'country' => '235', 'currrency' => '1', ); // Instead of creating a new instance, the one created in // getExtensions() will be used $form = $this->factory->create(OfficeType::class); // submit data to form //$form->submit($formData); //$this->assertTrue($form->isSynchronized()); } 

}

When I run the tests, I get the following error

Argument 1 passed to Symfony \ Bridge \ Doctrine \ Form \ Type \ DoctrineType :: __ construct () must be an instance of Doctrine \ Common \ Persistence \ ManagerRegistry that is not specified, called in / www / stevepop.com / symfony / provider / symfony /symfony/src/Symfony/Component/Form/FormRegistry.php on line 85 and is defined

The following is an OfficeType type;

 namespace AppBundle\Form\Type\Database; 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 OfficeType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name', TextType::class, array( 'required' => true, )); $builder->add('address1', TextType::class, array( 'required' => true, )); $builder->add('country', EntityType::class, array( 'class' => 'AppBundle:Country', 'attr' => array( 'class' => 'input-sm', ), 'required' => true, 'placeholder' => "Non selected", )); $builder->add('currency', EntityType::class, array( 'class' => 'AppBundle:Currency', 'attr' => array( 'class' => 'input-sm', ), 'required' => true, 'placeholder' => "Non selected", )); } 

I'm not sure what it means to be honest and appreciate the help of those who passed the FormType unit tests in Symfony 2.8 / 3.0. Thanks.

+6
source share
1 answer

Try adding this to your tests:

 use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; use Symfony\Component\Form\Extension\Core\CoreExtension; class OfficeType extends AbstractType { /** * @var \Doctrine\ORM\EntityManager */ private $em; public function setUp() { $this->em = DoctrineTestHelper::createTestEntityManager(); parent::setUp(); } protected function getExtensions() { $manager = $this->createMock('Doctrine\Common\Persistence\ManagerRegistry'); $manager->expects($this->any()) ->method('getManager') ->will($this->returnValue($this->em)); $manager->expects($this->any()) ->method('getManagerForClass') ->will($this->returnValue($this->em)); return array( new CoreExtension(), new DoctrineOrmExtension($manager), ); } // your code... } 
+4
source

All Articles