【问题标题】:Testing Form symfony with construct使用构造测试表单 symfony
【发布时间】:2017-07-29 20:18:22
【问题描述】:

问题是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 类中:

类 TodoType 扩展 AbstractType { /** * @var 实体管理器接口 */ 私人 $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);
}

}

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

【问题讨论】:

  • 我发现了一个问题。我使用了错误版本的 phpunit
  • 所以添加这个作为答案并关闭这个问题。

标签: php forms symfony unit-testing


【解决方案1】:

正如错误消息告诉你的那样,你正试图在这部分代码中调用 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() 调用。无论如何,为了使测试有用,您必须在某个时候做出断言。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多