Symfony 具有集合的表单的单元测试
我几乎没有单元测试方面的经验。我阅读symfony烹饪书的这一章来测试表单类型 我的表格如下所示:Symfony 具有集合的表单的单元测试,symfony,Symfony,我几乎没有单元测试方面的经验。我阅读symfony烹饪书的这一章来测试表单类型 我的表格如下所示: public function __construct(SecurityContext $securityContext, \Doctrine\ORM\EntityManager $em) { $this->securityContext = $securityContext; $this->entityManager = $em; } public fu
public function __construct(SecurityContext $securityContext, \Doctrine\ORM\EntityManager $em)
{
$this->securityContext = $securityContext;
$this->entityManager = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text', array('label' => 'title', 'translation_domain' => 'messages', 'attr' => array('maxlength' => 255)))
->add('comments', 'collection', array(
'type' => new CommentType() ,
'allow_add' => false,
'allow_delete' => false,
'label' => false,
'options' => array(
'label' => false,
)
)
)
->add('translations', 'a2lix_translations', array(
'fields' => array(
'coverLetter' => array(
'label' => 'msg.coverLetter',
'field_type' => 'textarea',
'attr' => array('class' => 'rte')
)
)
));
}
现在我编写一个类来测试我的表单
class QuestionnaireControllerTest extends TypeTestCase
{
public function testAddQuestionnaire()
{
$kernel = new \AppKernel('dev', true);
$kernel->boot();
$container = $kernel->getContainer();
$securityContext = $container->get('security.context');
$entityManager = $container->get('doctrine.orm.entity_manager');
$formData = array('title' => 'Exp. title');
$type = new QuestionnaireType($securityContext, $entityManager);
$form = $this->factory->create($type);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$view = $form->createView();
$children = $view->children;
foreach (array_keys($formData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
}
但是我有一些问题要考
这是获取内核的正确方法吗?
如何测试表单项注释集合、翻译和Lix_翻译?
不幸的是,我没有找到关于这些问题的有用教程。当涉及到类型测试时,它可能会变得有点奇怪,因为它似乎不在循环中。我遇到了一些类似的问题,实际上做了以下工作: 1关于内核:扩展内核测试用例,在测试中包括TypeTestCase和超级逻辑,或者自己创建一个抽象。这样,您将有一个更加一致的内核初始化方式,并且除了表单初始化之外,TypeTestCase没有那么大。让这种可定制的功能在以后可能会有所帮助 2要将A2lix内容作为测试表单的扩展加载,您必须重写
protected function getExtensions()
并返回一个数组
return array(
new PreloadedExtension(..., array())
);
2里克斯。。。可能看起来像
$gedmoTranslationsType = new GedmoTranslationsType($this->container->get('a2lix_translation_form.gedmo.listener.translations'), $this->container->get('a2lix_translation_form.gedmo.service.translation'), $this->getLocales(), false);
$gedmoTranslationsLocalesType = new GedmoTranslationsLocalesType();
$translationsFields = new TranslationsFieldsType();
return array(
$gedmoTranslationsType->getName() => $gedmoTranslationsType,
$gedmoTranslationsLocalesType->getName() => $gedmoTranslationsLocalesType,
$translationsFields->getName() => $translationsFields,
);
我已经在方便的成员和函数中找到了容器和区域设置。你应该这样做
一般来说,扩展在测试实体类型时也会变得很重要。因此,概括这些东西可能会有所帮助
实际上,随着时间的推移,您将了解到更多需要进行类型测试的内容。我希望这能有所帮助
最好的