/** * @expectedException \Symfony\Component\Validator\Exception\ValidatorException */ public function testValidateValueRejectsValid() { $entity = new Entity(); $metadata = new ClassMetadata(get_class($entity)); $this->metadataFactory->addMetadata($metadata); $this->validator->validateValue($entity, new Valid()); }
public function testReplaceDefaultGroupWithArrayFromGroupSequenceProvider() { $sequence = array('Group 1', 'Group 2', 'Group 3', 'Entity'); $entity = new GroupSequenceProviderEntity($sequence); $callback1 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in Group 2'); }; $callback2 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in Group 3'); }; $metadata = new ClassMetadata(get_class($entity)); $metadata->addConstraint(new Callback(array( 'callback' => function () {}, 'groups' => 'Group 1', ))); $metadata->addConstraint(new Callback(array( 'callback' => $callback1, 'groups' => 'Group 2', ))); $metadata->addConstraint(new Callback(array( 'callback' => $callback2, 'groups' => 'Group 3', ))); $metadata->setGroupSequenceProvider(true); $this->metadataFactory->addMetadata($metadata); $violations = $this->validate($entity, null, 'Default'); /* @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); $this->assertSame('Violation in Group 2', $violations[0]->getMessage()); }
public function testValidateCascadedPropertyDoesNotRecurseByDefault() { $entity = new Entity(); $entity->reference = new \ArrayIterator(array('key' => new \ArrayIterator(array('nested' => new Entity())))); $this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator')); // add a constraint for the entity that always fails $this->metadata->addConstraint(new FailingConstraint()); // validate iterator when validating the property "reference" $this->metadata->addPropertyConstraint('reference', new Valid()); $this->visitor->validate($entity, 'Default', ''); $violations = new ConstraintViolationList(array(new ConstraintViolation('Failed', 'Failed', array(), 'Root', '', $entity))); $this->assertEquals($violations, $this->visitor->getViolations()); }
public function testWalkCascadedPropertyRecursesIfDeepIsSet() { $entity = new Entity(); $entityMetadata = new ClassMetadata(get_class($entity)); $this->metadataFactory->addMetadata($entityMetadata); $this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator')); // add a constraint for the entity that always fails $entityMetadata->addConstraint(new FailingConstraint()); // validate iterator when validating the property "reference" $this->metadata->addPropertyConstraint('reference', new Valid(array('deep' => true))); $this->walker->walkPropertyValue($this->metadata, 'reference', new \ArrayIterator(array('key' => new \ArrayIterator(array('nested' => $entity)))), 'Default', 'path'); $violations = new ConstraintViolationList(); $violations->add(new ConstraintViolation('Failed', 'Failed', array(), 'Root', 'path[key][nested]', $entity)); $this->assertEquals($violations, $this->walker->getViolations()); }
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy', $ignoreNull = true) { if (!$validateClass) { $validateClass = 'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures\\SingleIntIdEntity'; } if (!$uniqueFields) { $uniqueFields = array('name'); } $registry = $this->createRegistryMock($entityManagerName, $em); $uniqueValidator = new UniqueEntityValidator($registry); $metadata = new ClassMetadata($validateClass); $constraint = new UniqueEntity(array('fields' => $uniqueFields, 'em' => $entityManagerName, 'errorPath' => $errorPath, 'repositoryMethod' => $repositoryMethod, 'ignoreNull' => $ignoreNull)); $metadata->addConstraint($constraint); $metadataFactory = new FakeMetadataFactory(); $metadataFactory->addMetadata($metadata); $validatorFactory = $this->createValidatorFactory($uniqueValidator); return new Validator($metadataFactory, $validatorFactory, new DefaultTranslator()); }
/** * @dataProvider getTestReplaceDefaultGroup */ public function testReplaceDefaultGroup($sequence, array $assertViolations) { $entity = new GroupSequenceProviderEntity($sequence); $callback1 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in Group 2'); }; $callback2 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in Group 3'); }; $metadata = new ClassMetadata(get_class($entity)); $metadata->addConstraint(new Callback(array('callback' => function () { }, 'groups' => 'Group 1'))); $metadata->addConstraint(new Callback(array('callback' => $callback1, 'groups' => 'Group 2'))); $metadata->addConstraint(new Callback(array('callback' => $callback2, 'groups' => 'Group 3'))); $metadata->setGroupSequenceProvider(true); $this->metadataFactory->addMetadata($metadata); $violations = $this->validate($entity, null, 'Default'); /* @var ConstraintViolationInterface[] $violations */ $this->assertCount(count($assertViolations), $violations); foreach ($assertViolations as $key => $message) { $this->assertSame($message, $violations[$key]->getMessage()); } }