我目前正在使用Symfony的TypeTestCase来测试带有构造函数的表单类型。虽然作为服务的表单的官方solution注册表在应用程序中运行良好,但是使用TypeTestCase无法测试相同的表单类型。由于TypeTestCase从FormIntegrationTestCase扩展而来自\ PHPUnit_Framework_TestCase而不是从KernelTestCase扩展。 TypeTestCase中的FormFactory没有/不能查找注册为服务的表单并且在调用phpunit时抛出错误:
Tests\DemoBundle\Form\Type\DemoTypeTest::testSubmitValidData
Missing argument 1 for DemoBundle\Form\Type\DemoType::__construct(), called in /var/www/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 90 and defined
我目前看到的唯一方法是不使用configureOptions中的optionsResolver通过构造函数提供依赖项,为依赖项设置一个必需元素和setRequired方法。
这里是代码中的相关部分:
DemoType.php
[...]
class DemoType extends AbstractType
{
/**
* @var EntityManager
*/
protected $entityManager;
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
[...]
services.yml
app.form.type.demo:
class: DemoBundle\Form\Type\DemoType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type }
DemoTypeTest.php
[...]
class DemoTypeTest extends TypeTestCase
{
protected function getExtensions()
{
$classMetadata = $this->getMock(ClassMetadata::class, [], [], '', false);
/* @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject $validator */
$validator = $this->getMock(ValidatorInterface::class);
$validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
$validator->method('getMetadataFor')->will($this->returnValue($classMetadata));
return [
new ValidatorExtension($validator),
];
}
public function testSubmitValidData()
{
$formData = [
'some' => 'data',
];
$form = $this->factory->create(DemoType::class);
[...]
答案 0 :(得分:1)
您可以实例化FormType并将其传递给表单工厂,如doc中所述。
您可以模拟Doctrine Entity Manager,例如:
WITH
CTE
AS
(
SELECT TOP 4
AVG(rating) AS ratingstars,
COUNT(id_module) AS countmodules
FROM [db]
WHERE (rating > 3)
GROUP BY id_module
ORDER BY ratingstars DESC, countmodules DESC
)
SELECT
ratingstars
,countmodules
,SUM(countmodules) OVER () AS SumCountModules
FROM CTE
ORDER BY ratingstars DESC, countmodules DESC
;
关于模拟Doctrine对象堆栈以模拟repo方法的示例,您可以查看this answer或只是询问:)
希望这个帮助