i want to test my form but I'm getting error Class "doctrine.orm.validator.unique" not found, this is about #[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
in my Entity. Can i add this in easy way like validator ?
Test:
<?php
declare(strict_types=1);
namespace App\Tests\Form;
use App\Form\RegistrationFormType;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\Validation;
class RegistrationFormTypeTest extends TypeTestCase
{
protected function getExtensions(): array
{
$validator = Validation::createValidator();
// or if you also need to read constraints from annotations
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->addDefaultDoctrineAnnotationReader()
->getValidator();
return [
new ValidatorExtension($validator),
];
}
public function testSubmitValidData()
{
$formData = [
'email' => '[email protected]',
'agreeTerms' => true
];
$form = $this->factory->create(RegistrationFormType::class);
$form->submit($formData);
}
}
CodePudding user response:
protected function setUp(): void
{
parent::setUp();
$validator = $this->createMock('\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();
}