Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3 FormTypeTest

Following the Symfony 3.0 manual on testing Form Types, I followed the example for 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'    => '[email protected]',
        '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, none given, called in /www/stevepop.com/symfony/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 85 and defined

The OfficeType is below;

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 am not sure what this means to be honest and will appreciate help from anyone who has done FormType unit tests in Symfony 2.8/3.0. Thank you.

like image 273
stevepop Avatar asked Jun 01 '16 12:06

stevepop


1 Answers

Try to 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...
}
like image 123
Jupeter Avatar answered Oct 13 '22 18:10

Jupeter