使用构造测试表单symfony

时间:2017-07-29 20:18:22

标签: php forms symfony unit-testing

问题是PHPunit版本过旧 /////

现在我还有其他问题。我有测试:

class TodoTypeTest extends TypeTestCase
{
    private $em;
    protected function setUp()
    {
        $this->em = $this->createMock(EntityManager::class);
        parent::setUp();
    }

    protected function getExtensions()
    {
        return array(
            new PreloadedExtension([
                new TodoType($this->em)
            ], [])
        );
    }

    public function testTodoType()
    {
        $task = new Todo();
        $form = $this->factory->create(TodoType::class, $task, ['locale' => 'en']);
    }
 }

我遇到了这个问题:

  

错误:在null上调用成员函数getPrioritysInUserLocaleToForm()

问题在于TodoType类:

class TodoType扩展AbstractType {     / **      * @var EntityManagerInterface      * /     私人$ em;

/**
 * TodoType constructor.
 *
 * @param EntityManagerInterface $em
 */
public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', Type\TextType::class)
        ->add('content', Type\TextareaType::class)
        ->add('priority', Type\ChoiceType::class, [ 'choices' => $this->addChoicesInUserLocale($options['locale']) ])
        ->add('dueDate', Type\DateTimeType::class, [
            'widget' => 'single_text',
            'attr' => ['class' => 'js-datepicker'],
            'html5' => false,
        ]);
}

/**
 * Configure defaults options
 *
 * @param OptionsResolver $resolver
 */
public function configureOptions( OptionsResolver $resolver )
{
    $resolver->setDefaults( [
        'locale' => 'en',
    ] );
}
/**
 * Method adds array with choices to ChoiceType in builder
 *
 * @param string $locale User's locale
 *
 * @return array All priority in user _locale formatted as array e.g. ['1' => 'low', ...]
 */
private function addChoicesInUserLocale(string $locale): array
{
    return $this->em->getRepository('AppBundle:Priority')
            ->getPrioritysInUserLocaleToForm($locale);
}

}

我不知道为什么它不起作用:/

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,您正试图在代码的这一部分中调用null方法:

return $this->em->getRepository('AppBundle:Priority')
        ->getPrioritysInUserLocaleToForm($locale);

该消息告诉您,getRepository()返回null。

这是因为您使用的EntityManager是一个mock,除非另有说明,否则它将始终为所有方法返回null。您可以通过使其返回EntityRepository来解决此问题。您可以在测试方法中执行此操作:

public function testTodoType()
{
    $repoMock = $this->createMock(PriorityRepository::class);
    $repoMock->expects($this->any())
        ->method('getPrioritysInUserLocaleToForm')
        ->with('en')
        ->willReturn([]); // Whatever you want it to return

    $this->em->expects($this->any())
        ->method('getRepository')
        ->with('AppBundle:Priority')
        ->willReturn($repoMock);

    $task = new Todo();
    $form = $this->factory->create(TodoType::class, $task, ['locale' => 'en']);
}

这将使EntityManager返回您的Repository-mock,然后返回您想要的任何值。 expects()调用是断言,并且由于您的测试还没有,您可能需要检查使用$this->atLeastOnce()而不是$this->any()调用存储库方法。在任何情况下,为了使测试有用,你必须在某些时候做出断言。