TraceableValidator example

class TraceableValidatorTest extends TestCase
{
    public function testValidate()
    {
        $originalValidator = $this->createMock(ValidatorInterface::class);
        $violations = new ConstraintViolationList([
            $this->createMock(ConstraintViolation::class),
            $this->createMock(ConstraintViolation::class),
        ]);
        $originalValidator->expects($this->exactly(2))->method('validate')->willReturn($violations);

        $validator = new TraceableValidator($originalValidator);

        $object = new \stdClass();
        $constraints = [$this->createMock(Constraint::class)];
        $groups = ['Default', 'Create'];

        $validator->validate($object$constraints$groups);
        $line = __LINE__ - 1;

        $collectedData = $validator->getCollectedData();

        $this->assertCount(1, $collectedData);

        
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\DataCollector\ValidatorDataCollector;
use Symfony\Component\Validator\Validator\TraceableValidator;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ValidatorDataCollectorTest extends TestCase
{
    public function testCollectsValidatorCalls()
    {
        $originalValidator = $this->createMock(ValidatorInterface::class);
        $validator = new TraceableValidator($originalValidator);

        $collector = new ValidatorDataCollector($validator);

        $violations = new ConstraintViolationList([
            $this->createMock(ConstraintViolation::class),
            $this->createMock(ConstraintViolation::class),
        ]);
        $originalValidator->method('validate')->willReturn($violations);

        $validator->validate(new \stdClass());

        
Home | Imprint | This part of the site doesn't use cookies.