的問題是太舊版本的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);
}
}
我不知道爲什麼它不工作:/
我發現了一個問題。我使用的是phpunit的錯誤版本 – demotywatorking
因此,添加這個答案並關閉這個問題。 – Daniel