【问题标题】:Symfony 3 FormTypeTestSymfony 3 FormTypeTest
【发布时间】:2016-09-30 19:17:29
【问题描述】:

按照 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() 的参数 1 必须是 Doctrine\Common\Persistence\ManagerRegistry 的一个实例,没有给出,在 /www/stevepop.com/symfony/ 中调用vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php 在第 85 行并定义

OfficeType 如下;

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",
    ));
 }

说实话,我不确定这意味着什么,我会感谢任何在 Symfony 2.8/3.0 中做过 FormType 单元测试的人的帮助。 谢谢。

【问题讨论】:

  • OfficeType 长什么样子?
  • Thanjks @dragoste,我更新了我的帖子以显示 OfficeType 的样子。
  • OfficeType 贴在上面。

标签: php unit-testing phpunit symfony


【解决方案1】:

尝试将此添加到您的测试中:

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-&gt;getMock() 已弃用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多