Does anyone have a good way to unit test an entity\'s validation constraints in Symfony2?
Ideally I want to have access to the Dependency Injection Container within
I liked Kasheens answer, but it doesn't work for Symfony 2.3 anymore. There are little changes:
use Symfony\Component\Validator\Validation;
and
$validator = Validation::createValidatorBuilder()->getValidator();
If you want to validate Annotations for instance, use enableAnnotationMapping() like below:
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
the rest stays the same:
$errors = $validator->validate($entity);
$this->assertEquals(0, count($errors));