单元测试表与存储库方法

时间:2015-05-02 05:05:42

标签: forms unit-testing symfony

我正在尝试对我的一个表单进行单元测试,其中包含一个实体表单类型。我想测试完整的表单,但我继续遇到错误消息 - 类型" Doctrine \ ORM \ QueryBuilder"," NULL"的预期参数给定

这很明显,我需要以某种方式模拟Doctrine \ ORM \ QueryBuilder作为实体表单类型的返回类型。我并不确定如何做到这一点。

以下是表格的代码 -

<?php

namespace ICS\BackEnd\BoardBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BoardCollectionType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', 'text', array(
                'disabled' => TRUE
            ))
            ->add('member', 'entity', array(
                'class' => 'MemberBundle:Members',
                'property' => 'fullName',
                'query_builder' => function(EntityRepository $er) {
                    return $er->findAllActiveMembers();
                },
            ))
        ;
    }

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'ICS\BackEnd\BoardBundle\Entity\Board'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'ics_boardbundle_board';
}
}

这是我正在运行的测试 -

<?php

namespace ICS\BackEnd\BoardBundle\Tests\Form;

use Doctrine\ORM\QueryBuilder;
use ICS\BackEnd\BoardBundle\Entity\Board;
use ICS\BackEnd\BoardBundle\Form\BoardCollectionType;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;

class BoardCollectionTypeTest extends TypeTestCase {

protected $repository;

protected function setUp()
{
    parent::setUp();

    $this->factory = Forms::createFormFactoryBuilder()
        ->addExtensions($this->getExtensions())
        ->getFormFactory();
}

public function testSubmittedValueData()
{
    $formData = array(
        'member' => NULL,
    );

    $type = new BoardCollectionType();
    $form = $this->factory->create($type);

    $object = new Board();
    $object->createFromArray($formData);

    // submit the data to the form directly
    $form->submit($formData);

    $this->assertTrue($form->isSynchronized());
    $this->assertEquals($object, $form->getData());

    $view = $form->createView();
    $children = $view->children;

    foreach (array_keys($formData) as $key) {
        $this->assertArrayHasKey($key, $children);
    }
}

protected function getExtensions()
{

    $this->repository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
        ->disableOriginalConstructor()
        ->getMock();
    $mockEntityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
        ->disableOriginalConstructor()
        ->getMock();
    $mockEntityManager->expects($this->any())
        ->method('getRepository')
        ->will($this->returnValue($this->repository));
    $classMetadata = $this->getMockBuilder('\Doctrine\Common\Persistence\Mapping\ClassMetadata')
        ->disableOriginalConstructor()
        ->getMock();
    $mockEntityManager->expects($this->any())
        ->method('getClassMetadata')
        ->will($this->returnValue($classMetadata));
    $mockRegistry = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry')
        ->disableOriginalConstructor()
        ->setMethods(array('getManagerForClass'))
        ->getMock();
    $mockRegistry->expects($this->any())
        ->method('getManagerForClass')
        ->will($this->returnValue($mockEntityManager));
    $mockEntityType = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType')
        ->setMethods(array('getName'))
        ->setConstructorArgs(array($mockRegistry))
        ->getMock();
    $mockEntityType->expects($this->any())
        ->method('getName')
        ->will($this->returnValue('entity'));

    $this->assertQueryBuilderCalled();

    return array(new PreloadedExtension(array(
        $mockEntityType->getName() => $mockEntityType,
    ), array()));
}

protected function assertQueryBuilderCalled()
{
    $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
        ->disableOriginalConstructor()->getMock();
    $repo = $this->getMockBuilder('ICS\BackEnd\MemberBundle\Entity\Repository\MembersRepository')
        ->disableOriginalConstructor()->getMock();
    $repo->expects($this->once())->method('findAllActiveMembers')
        ->will($this->returnValue(new QueryBuilder($em)));

    /*$qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
        ->disableOriginalConstructor()
        ->getMock();

    $query = $this->getMockBuilder('Doctrine\ORM\AbstractQuery')
        ->disableOriginalConstructor()
        ->setMethods(array('execute'))
        ->getMockForAbstractClass();
    $query->expects($this->any())
        ->method('execute')
        ->will($this->returnValue(array()));
    $qb->expects($this->any())
        ->method('getQuery')
        ->will($this->returnValue($query));

    $this->repository->expects($this->any())
        ->method('findAllActiveMembers')
        ->will($this->returnValue($query));

    $this->repository->expects($this->any())
        ->method('createQueryBuilder')
        ->will($this->returnValue($qb));*/
}
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我通过将实体表单类型更改为选项来解决此问题。它使测试更容易阅读和做。我注入了&#34;选择&#34;进入表单类型的选项。