Unit testing validator constraints with symfony2.3
Check the type of your element: in the validator class you use the comparator between two DateTime object but in the test you pass a string to the validator.
This is my test class:
namespace Acme\DemoBundle\Tests\Form;use Acme\DemoBundle\Validator\Constraints\Age18;use Acme\DemoBundle\Validator\Constraints\Age18Validator;class Age18ValidatorTest extends \PHPUnit_Framework_TestCase{ private $constraint; private $context; public function setUp() { $this->constraint = new Age18(); $this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')->disableOriginalConstructor()->getMock(); } public function testValidate() { /*ConstraintValidator*/ $validator = new Age18Validator(); $validator->initialize( $this->context); $this->context->expects($this->once()) ->method('addViolation') ->with($this->constraint->message,array()); $validator->validate(\Datetime::createFromFormat("d/m/Y","10/10/2000"), $this->constraint); } public function tearDown() { $this->constraint = null; }}
Hope this help