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.

pfwd
  • 1,088
  • 1
  • 11
  • 22
  • 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. – Alister Bulman Jan 31 '15 at 23:54
  • possible duplicate of [Problems with ValidatorConstraint in Symfony 2.5](http://stackoverflow.com/questions/24009719/problems-with-validatorconstraint-in-symfony-2-5) – Brejk Jan 31 '15 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. – pfwd Feb 01 '15 at 08:01

2 Answers2

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

Community
  • 1
  • 1
Brejk
  • 454
  • 3
  • 12
0

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

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

Adam Chysky
  • 386
  • 1
  • 9