【发布时间】:2016-09-30 19:17:29
【问题描述】:
按照 Symfony 3.0 测试表单类型的手册,我按照以下示例测试具有依赖关系的表单;
<?php
namespace Tests\AppBundle\Form;
use AppBundle\Form\Type\Database\OfficeAssignType;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
class OfficeTypeTest extends TypeTestCase
{
private $entityManager;
protected function setUp()
{
// mock any dependencies
$this->entityManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
parent::setUp();
}
protected function getExtensions()
{
// create a type instance with the mocked dependencies
$office = new OfficeType($this->entityManager);
return array(
// register the type instances with the PreloadedExtension
new PreloadedExtension(array($office), array()),
);
}
public function testSubmitValid()
{
$formData = array(
'name' => 'new test office',
'address1' => 'Test new office address',
'city' => 'Test office city',
'phone' => 'testoffice@stevepop.com',
'country' => '235',
'currrency' => '1',
);
// Instead of creating a new instance, the one created in
// getExtensions() will be used
$form = $this->factory->create(OfficeType::class);
// submit data to form
//$form->submit($formData);
//$this->assertTrue($form->isSynchronized());
}
}
当我运行测试时,我收到以下错误
传递给 Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct() 的参数 1 必须是 Doctrine\Common\Persistence\ManagerRegistry 的一个实例,没有给出,在 /www/stevepop.com/symfony/ 中调用vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php 在第 85 行并定义
OfficeType 如下;
namespace AppBundle\Form\Type\Database;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class OfficeType extends AbstractType {
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class, array(
'required' => true,
));
$builder->add('address1', TextType::class, array(
'required' => true,
));
$builder->add('country', EntityType::class, array(
'class' => 'AppBundle:Country',
'attr' => array(
'class' => 'input-sm',
),
'required' => true,
'placeholder' => "Non selected",
));
$builder->add('currency', EntityType::class, array(
'class' => 'AppBundle:Currency',
'attr' => array(
'class' => 'input-sm',
),
'required' => true,
'placeholder' => "Non selected",
));
}
说实话,我不确定这意味着什么,我会感谢任何在 Symfony 2.8/3.0 中做过 FormType 单元测试的人的帮助。 谢谢。
【问题讨论】:
-
OfficeType长什么样子? -
Thanjks @dragoste,我更新了我的帖子以显示 OfficeType 的样子。
-
OfficeType 贴在上面。
标签: php unit-testing phpunit symfony