Symfony单元测试表扩展模拟

时间:2015-01-31 21:44:17

标签: php unit-testing symfony composer-php

在通过composer更新我的代码之前,我的表单单元测试正在运行。它现在在扩展模拟失败了。 下面是错误:

Argument 1 passed to Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension::__construct() must be an instance of Symfony\Component\Validator\ValidatorInterface, instance of Mock_ValidatorInterface_14b95ba0 given

继承我的作曲家所需部分:

    "require": {
    "php": ">=5.3.3",
    "symfony/symfony": "~2.3",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.3",
    "symfony/monolog-bridge": "~2.4",
    "sensio/distribution-bundle": "~2.3",
    "sensio/framework-extra-bundle": "~2.3"
   }

单元测试设置方法中的验证模拟:

use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Form\Extension\Core\CoreExtension;

class TestedTypeTest extends TypeTestCase
{

protected function setUp()
{
    parent::setUp();

    $validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface');
    $validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
    $formTypeExtension = new FormTypeValidatorExtension($validator);
    $coreExtension = new CoreExtension();

    $this->factory = Forms::createFormFactoryBuilder()
            ->addExtensions($this->getExtensions())
            ->addExtension($coreExtension)
            ->addTypeExtension($formTypeExtension)
            ->getFormFactory();
}

有谁知道模拟可能有什么问题?非常感谢任何建议。

2 个答案:

答案 0 :(得分:2)

从UPGRADE-2.5.md:

  

Symfony\Component\Validator\Validator中的验证引擎是   取而代之的是新的   Symfony\Component\Validator\Validator\RecursiveValidator。接着就,随即   更改时,将删除几个将被删除的类   Symfony 3.0。此外,验证器的API略有变化。更多   有关详细信息,请参阅UPGRADE-3.0。

这是类似的问题: Problems with ValidatorConstraint in Symfony 2.5

答案 1 :(得分:0)

你必须转义反斜杠才能在字符串中使用它们:

$validator = $this->getMock('\\Symfony\\Component\\Validator\\Validator\\ValidatorInterface');