Symfony 3 FormTypeTest

6

按照Symfony 3.0手册上有关测试表单类型的内容,我按照以下方式测试了具有依赖项的表单:

<?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());
}

运行测试时,我遇到了以下错误:

Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct()的第一个参数必须是Doctrine\Common\Persistence\ManagerRegistry的实例,但未提供任何参数,在/www/stevepop.com/symfony/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php的第85行调用并定义

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.


OfficeType 是什么样子? - Jakub Matczak
谢谢@dragoste,我更新了我的帖子以展示OfficeType的外观。 - stevepop
上面发布了OfficeType。 - stevepop
1个回答

4
试着在你的测试中添加以下内容:
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...
}

甚至更好的方法是使用 $manager = \PHPUnit_Framework_TestCase::createMock('Doctrine\Common\Persistence\ManagerRegistry');$this->getMock()已经被弃用了。 - Stephan Vierkant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接