public function AbstractValidatorTest::testValidateProperty in Plug 7
File
- lib/
Symfony/ validator/ Symfony/ Component/ Validator/ Tests/ Validator/ AbstractValidatorTest.php, line 790
Class
- AbstractValidatorTest
- @since 2.5 @author Bernhard Schussek <bschussek@gmail.com>
Namespace
Symfony\Component\Validator\Tests\ValidatorCode
public function testValidateProperty() {
$test = $this;
$entity = new Entity();
$entity->firstName = 'Bernhard';
$entity
->setLastName('Schussek');
$callback1 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
$propertyMetadatas = $test->metadata
->getPropertyMetadata('firstName');
$test
->assertSame($test::ENTITY_CLASS, $context
->getClassName());
$test
->assertSame('firstName', $context
->getPropertyName());
$test
->assertSame('firstName', $context
->getPropertyPath());
$test
->assertSame('Group', $context
->getGroup());
$test
->assertSame($propertyMetadatas[0], $context
->getMetadata());
$test
->assertSame($entity, $context
->getRoot());
$test
->assertSame('Bernhard', $context
->getValue());
$test
->assertSame('Bernhard', $value);
$context
->addViolation('Message %param%', array(
'%param%' => 'value',
));
};
$callback2 = function ($value, ExecutionContextInterface $context) {
$context
->addViolation('Other violation');
};
$this->metadata
->addPropertyConstraint('firstName', new Callback(array(
'callback' => $callback1,
'groups' => 'Group',
)));
$this->metadata
->addPropertyConstraint('lastName', new Callback(array(
'callback' => $callback2,
'groups' => 'Group',
)));
$violations = $this
->validateProperty($entity, 'firstName', 'Group');
/** @var ConstraintViolationInterface[] $violations */
$this
->assertCount(1, $violations);
$this
->assertSame('Message value', $violations[0]
->getMessage());
$this
->assertSame('Message %param%', $violations[0]
->getMessageTemplate());
$this
->assertSame(array(
'%param%' => 'value',
), $violations[0]
->getMessageParameters());
$this
->assertSame('firstName', $violations[0]
->getPropertyPath());
$this
->assertSame($entity, $violations[0]
->getRoot());
$this
->assertSame('Bernhard', $violations[0]
->getInvalidValue());
$this
->assertNull($violations[0]
->getMessagePluralization());
$this
->assertNull($violations[0]
->getCode());
}