2017-07-29 68 views
0

的问题是太旧版本的PHPUnit 的/////测试表格symfony中,以构建

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

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']); 
    } 
} 

我得到这个问题:

Error: Call to a member function getPrioritysInUserLocaleToForm() on null

问题是在这里TodoType类:

类TodoType扩展文摘actType { /** * @var EntityManagerInterface */ private $ 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); 
} 

}

我不知道为什么它不工作:/

+0

我发现了一个问题。我使用的是phpunit的错误版本 – demotywatorking

+0

因此,添加这个答案并关闭这个问题。 – Daniel

回答

0

随着错误消息告诉您,您要呼吁无效的方法在这部分代码:

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

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

发生这种情况是因为您使用的EntityManager是一个模拟,除非另行指定,否则它将始终为所有方法返回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的回报您的存储库,模拟,然后又返回任何你想要的值。 expects()调用是断言,由于您的测试还没有,您可能需要检查是否使用$this->atLeastOnce()而不是$this->any()调用存储库方法。在任何情况下,为了使测试有用,你必须在某个时候做出断言。