2017-07-29 38 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()調用存儲庫方法。在任何情況下,爲了使測試有用,你必須在某個時候做出斷言。