0

Before updating my code via composer my forms unit test was working. It is now failing on the extension mock. Heres the error:

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

Heres my composer required section:

    "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"
   }

Heres the validation mock in the unit test setup method:

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();
}

Does anyone know what might be wrong with the mock? Any advice is greatly appreciated.

3
  • 1
    must be an instance of \Symfony\Component\Validator\ValidatorInterface" vs "\Symfony\Component\Validator\Validator\ValidatorInterface" You appear to have too many 'Validator' in the mock. Commented Jan 31, 2015 at 23:54
  • possible duplicate of Problems with ValidatorConstraint in Symfony 2.5 Commented Jan 31, 2015 at 23:58
  • @AlisterBulman Yes this was exactly it. Removing the second Validator worked. Im using Symphony ~2.3 and not 2.5 so the deprecation of Symfony\Component\Validator\Validator doesn't apply here. Commented Feb 1, 2015 at 8:01

2 Answers 2

2

From UPGRADE-2.5.md:

The validation engine in Symfony\Component\Validator\Validator was replaced by a new one in Symfony\Component\Validator\Validator\RecursiveValidator. With that change, several classes were deprecated that will be removed in Symfony 3.0. Also, the API of the validator was slightly changed. More details about that can be found in UPGRADE-3.0.

And here is similar problem: Problems with ValidatorConstraint in Symfony 2.5

Sign up to request clarification or add additional context in comments.

Comments

0

You have to escape the backslashes to use them in a string:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.