validateProperty example

$this->collectedData[] = [
            'caller' => compact('name', 'file', 'line'),
            'context' => compact('value', 'constraints', 'groups'),
            'violations' => iterator_to_array($violations),
        ];

        return $violations;
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->validator->validateProperty($object$propertyName$groups);
    }

    public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
    }

    public function startContext(): ContextualValidatorInterface
    {
        return $this->validator->startContext();
    }

    
$contextualValidatorMockBuilder->onlyMethods($contextualValidatorMethods);
        $contextualValidator = $contextualValidatorMockBuilder->getMock();
        $contextualValidator->expects($this->any())
            ->method('atPath')
            ->willReturnCallback(fn ($path) => $contextualValidator->doAtPath($path));
        $contextualValidator->expects($this->any())
            ->method('validate')
            ->willReturnCallback(fn ($value$constraints = null, $groups = null) => $contextualValidator->doValidate($value$constraints$groups));
        $contextualValidator->expects($this->any())
            ->method('validateProperty')
            ->willReturnCallback(fn ($object$propertyName$groups = null) => $contextualValidator->validateProperty($object$propertyName$groups));
        $contextualValidator->expects($this->any())
            ->method('validatePropertyValue')
            ->willReturnCallback(fn ($objectOrClass$propertyName$value$groups = null) => $contextualValidator->doValidatePropertyValue($objectOrClass$propertyName$value$groups));
        $contextualValidator->expects($this->any())
            ->method('getViolations')
            ->willReturnCallback(fn () => $contextualValidator->doGetViolations());
        $validator->expects($this->any())
            ->method('inContext')
            ->with($context)
            ->willReturn($contextualValidator);

        
public function validate(mixed $value, Constraint|array $constraints = null, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($value)
            ->validate($value$constraints$groups)
            ->getViolations();
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($object)
            ->validateProperty($object$propertyName$groups)
            ->getViolations();
    }

    public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        // If a class name is passed, take $value as root         return $this->startContext(\is_object($objectOrClass) ? $objectOrClass : $value)
            ->validatePropertyValue($objectOrClass$propertyName$value$groups)
            ->getViolations();
    }
}
public function validate($value$constraints = NULL, $groups = NULL): ConstraintViolationListInterface {
    return $this->startContext($value)
      ->validate($value$constraints$groups)
      ->getViolations();
  }

  /** * {@inheritdoc} */
  public function validateProperty($object$propertyName$groups = NULL): ConstraintViolationListInterface {
    return $this->startContext($object)
      ->validateProperty($object$propertyName$groups)
      ->getViolations();
  }

  /** * {@inheritdoc} */
  public function validatePropertyValue($objectOrClass$propertyName$value$groups = NULL): ConstraintViolationListInterface {
    // Just passing a class name is not supported.     if (!is_object($objectOrClass)) {
      throw new \LogicException('Typed data validation does not support passing the class name only.');
    }
    
$contextualValidatorMockBuilder->onlyMethods($contextualValidatorMethods);
        $contextualValidator = $contextualValidatorMockBuilder->getMock();
        $contextualValidator->expects($this->any())
            ->method('atPath')
            ->willReturnCallback(fn ($path) => $contextualValidator->doAtPath($path));
        $contextualValidator->expects($this->any())
            ->method('validate')
            ->willReturnCallback(fn ($value$constraints = null, $groups = null) => $contextualValidator->doValidate($value$constraints$groups));
        $contextualValidator->expects($this->any())
            ->method('validateProperty')
            ->willReturnCallback(fn ($object$propertyName$groups = null) => $contextualValidator->validateProperty($object$propertyName$groups));
        $contextualValidator->expects($this->any())
            ->method('validatePropertyValue')
            ->willReturnCallback(fn ($objectOrClass$propertyName$value$groups = null) => $contextualValidator->doValidatePropertyValue($objectOrClass$propertyName$value$groups));
        $contextualValidator->expects($this->any())
            ->method('getViolations')
            ->willReturnCallback(fn () => $contextualValidator->doGetViolations());
        $validator->expects($this->any())
            ->method('inContext')
            ->with($context)
            ->willReturn($contextualValidator);

        
$expects('inContext')->willReturn($expected = $this->createMock(ContextualValidatorInterface::class));
        $this->assertSame($expected$validator->inContext($this->createMock(ExecutionContextInterface::class)), 'returns original validator inContext() result');

        $expects('startContext')->willReturn($expected = $this->createMock(ContextualValidatorInterface::class));
        $this->assertSame($expected$validator->startContext(), 'returns original validator startContext() result');

        $expects('validate')->willReturn($expected = new ConstraintViolationList());
        $this->assertSame($expected$validator->validate('value'), 'returns original validator validate() result');

        $expects('validateProperty')->willReturn($expected = new ConstraintViolationList());
        $this->assertSame($expected$validator->validateProperty(new \stdClass(), 'property'), 'returns original validator validateProperty() result');

        $expects('validatePropertyValue')->willReturn($expected = new ConstraintViolationList());
        $this->assertSame($expected$validator->validatePropertyValue(new \stdClass(), 'property', 'value'), 'returns original validator validatePropertyValue() result');
    }
}
$this->validator = $this->createValidator($this->metadataFactory);
    }

    protected function validate($value$constraints = null, $groups = null)
    {
        return $this->validator->validate($value$constraints$groups);
    }

    protected function validateProperty($object$propertyName$groups = null)
    {
        return $this->validator->validateProperty($object$propertyName$groups);
    }

    protected function validatePropertyValue($object$propertyName$value$groups = null)
    {
        return $this->validator->validatePropertyValue($object$propertyName$value$groups);
    }

    public function testValidate()
    {
        $callback = function D$value, ExecutionContextInterface $context) {
            $this->assertNull($context->getClassName());
            
public function validate(mixed $value, Constraint|array $constraints = null, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($value)
            ->validate($value$constraints$groups)
            ->getViolations();
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->startContext($object)
            ->validateProperty($object$propertyName$groups)
            ->getViolations();
    }

    public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        // If a class name is passed, take $value as root         return $this->startContext(\is_object($objectOrClass) ? $objectOrClass : $value)
            ->validatePropertyValue($objectOrClass$propertyName$value$groups)
            ->getViolations();
    }
}
$this->collectedData[] = [
            'caller' => compact('name', 'file', 'line'),
            'context' => compact('value', 'constraints', 'groups'),
            'violations' => iterator_to_array($violations),
        ];

        return $violations;
    }

    public function validateProperty(object $object, string $propertyName, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->validator->validateProperty($object$propertyName$groups);
    }

    public function validatePropertyValue(object|string $objectOrClass, string $propertyName, mixed $value, string|GroupSequence|array $groups = null): ConstraintViolationListInterface
    {
        return $this->validator->validatePropertyValue($objectOrClass$propertyName$value$groups);
    }

    public function startContext(): ContextualValidatorInterface
    {
        return $this->validator->startContext();
    }

    

  public function testValidatePropertyWithCustomGroup() {
    $tree = [
      'value' => [],
      'properties' => [
        'key1' => ['value' => 'value1'],
      ],
    ];
    $typed_data = $this->setupTypedData($tree, 'test_name');
    $this->expectException(\LogicException::class);
    $this->recursiveValidator->validateProperty($typed_data, 'key1', 'test group');
  }

  /** * @covers ::validateProperty * * @dataProvider providerTestValidatePropertyWithInvalidObjects */
  public function testValidatePropertyWithInvalidObjects($object) {
    $this->expectException(\InvalidArgumentException::class);
    $this->recursiveValidator->validateProperty($object, 'key1', NULL);
  }

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