public function testLoadClassMetadata() { $loader = new XmlFileLoader(__DIR__ . '/constraint-mapping.xml'); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $expected->setGroupSequence(array('Foo', 'Entity')); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new ConstraintB()); $expected->addConstraint(new Callback('validateMe')); $expected->addConstraint(new Callback('validateMeStatic')); $expected->addConstraint(new Callback(array('Symfony\\Component\\Validator\\Tests\\Fixtures\\CallbackClass', 'callback'))); $expected->addConstraint(new Traverse(false)); $expected->addPropertyConstraint('firstName', new NotNull()); $expected->addPropertyConstraint('firstName', new Range(array('min' => 3))); $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B'))); $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3))))); $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3)))))); $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array('foo' => array(new NotNull(), new Range(array('min' => 3))), 'bar' => array(new Range(array('min' => 5))))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); $expected->addGetterConstraint('valid', new IsTrue()); $expected->addGetterConstraint('permissions', new IsTrue()); $this->assertEquals($expected, $metadata); }
public function testValidateCustomGroupWhenDefaultGroupWasReplaced() { $entity = new Entity(); $callback1 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in other group'); }; $callback2 = function ($value, ExecutionContextInterface $context) { $context->addViolation('Violation in group sequence'); }; $this->metadata->addConstraint(new Callback(array( 'callback' => $callback1, 'groups' => 'Other Group', ))); $this->metadata->addConstraint(new Callback(array( 'callback' => $callback2, 'groups' => 'Group 1', ))); $sequence = new GroupSequence(array('Group 1', 'Entity')); $this->metadata->setGroupSequence($sequence); $violations = $this->validate($entity, null, 'Other Group'); /* @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); $this->assertSame('Violation in other group', $violations[0]->getMessage()); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = Yaml::parse($this->file); // empty file if (null === $this->classes) { return false; } // not an array if (!is_array($this->classes)) { throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $this->file)); } if (isset($this->classes['namespaces'])) { foreach ($this->classes['namespaces'] as $alias => $namespace) { $this->addNamespaceAlias($alias, $namespace); } unset($this->classes['namespaces']); } } // TODO validation if (isset($this->classes[$metadata->getClassName()])) { $yaml = $this->classes[$metadata->getClassName()]; if (isset($yaml['group_sequence_provider'])) { $metadata->setGroupSequenceProvider((bool) $yaml['group_sequence_provider']); } if (isset($yaml['group_sequence'])) { $metadata->setGroupSequence($yaml['group_sequence']); } if (isset($yaml['constraints']) && is_array($yaml['constraints'])) { foreach ($this->parseNodes($yaml['constraints']) as $constraint) { $metadata->addConstraint($constraint); } } if (isset($yaml['properties']) && is_array($yaml['properties'])) { foreach ($yaml['properties'] as $property => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addPropertyConstraint($property, $constraint); } } } } if (isset($yaml['getters']) && is_array($yaml['getters'])) { foreach ($yaml['getters'] as $getter => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addGetterConstraint($getter, $constraint); } } } } return true; } return false; }
public function testValidateInOtherGroupTraversesNoGroupSequence() { $entity = new Entity(); $this->metadata->addPropertyConstraint('firstName', new FailingConstraint(array('groups' => 'First'))); $this->metadata->addGetterConstraint('lastName', new FailingConstraint(array('groups' => $this->metadata->getDefaultGroup()))); $this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup())); $this->visitor->validate($entity, $this->metadata->getDefaultGroup(), ''); // Only group "Second" was validated $violations = new ConstraintViolationList(array(new ConstraintViolation('Failed', 'Failed', array(), 'Root', 'lastName', ''))); $this->assertEquals($violations, $this->visitor->getViolations()); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $className = $reflClass->getName(); $loaded = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addConstraint($constraint); } } elseif ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); } $loaded = true; } foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } } elseif ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } $loaded = true; } } } foreach ($reflClass->getMethods() as $method) { if ($method->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getMethodAnnotations($method) as $constraint) { // TODO: clean this up $name = lcfirst(substr($method->getName(), 0, 3) == 'get' ? substr($method->getName(), 3) : substr($method->getName(), 2)); if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addGetterConstraint($name, $constraint); } } elseif ($constraint instanceof Constraint) { $metadata->addGetterConstraint($name, $constraint); } $loaded = true; } } } return $loaded; }
/** * {@inheritdoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $className = $reflClass->name; $success = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { if ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof GroupSequenceProvider) { $metadata->setGroupSequenceProvider(true); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); } $success = true; } foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->name == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { if ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->name, $constraint); } $success = true; } } } foreach ($reflClass->getMethods() as $method) { if ($method->getDeclaringClass()->name == $className) { foreach ($this->reader->getMethodAnnotations($method) as $constraint) { if ($constraint instanceof Callback) { $constraint->callback = $method->getName(); $constraint->methods = null; $metadata->addConstraint($constraint); } elseif ($constraint instanceof Constraint) { if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) { $metadata->addGetterConstraint(lcfirst($matches[2]), $constraint); } else { throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name)); } } $success = true; } } } return $success; }
public function testLoadClassMetadata() { $loader = new YamlFileLoader(__DIR__ . '/constraint-mapping.yml'); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $expected->setGroupSequence(array('Foo', 'Entity')); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new ConstraintB()); $expected->addPropertyConstraint('firstName', new NotNull()); $expected->addPropertyConstraint('firstName', new Min(3)); $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B'))); $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3)))); $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3))))); $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array('foo' => array(new NotNull(), new Min(3)), 'bar' => array(new Min(5)))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); $this->assertEquals($expected, $metadata); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { if (null === $this->classes) { $this->classes = array(); $xml = $this->parseFile($this->file); foreach ($xml->namespace as $namespace) { $this->addNamespaceAlias((string) $namespace['prefix'], trim((string) $namespace)); } foreach ($xml->class as $class) { $this->classes[(string) $class['name']] = $class; } } if (isset($this->classes[$metadata->getClassName()])) { $xml = $this->classes[$metadata->getClassName()]; foreach ($xml->{'group-sequence-provider'} as $provider) { $metadata->setGroupSequenceProvider(true); } foreach ($xml->{'group-sequence'} as $groupSequence) { if (count($groupSequence->value) > 0) { $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value)); } } foreach ($this->parseConstraints($xml->constraint) as $constraint) { $metadata->addConstraint($constraint); } foreach ($xml->property as $property) { foreach ($this->parseConstraints($property->constraint) as $constraint) { $metadata->addPropertyConstraint((string) $property['name'], $constraint); } } foreach ($xml->getter as $getter) { foreach ($this->parseConstraints($getter->constraint) as $constraint) { $metadata->addGetterConstraint((string) $getter['property'], $constraint); } } return true; } return false; }
/** * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException */ public function testGroupSequenceProviderFailsIfGroupSequenceIsSet() { $metadata = new ClassMetadata(self::PROVIDERCLASS); $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo')); $metadata->setGroupSequenceProvider(true); }
/** * Loads the validation metadata from the given XML class description. * * @param ClassMetadata $metadata The metadata to load * @param array $classDescription The XML class description */ private function loadClassMetadataFromXml(ClassMetadata $metadata, $classDescription) { if (count($classDescription->{'group-sequence-provider'}) > 0) { $metadata->setGroupSequenceProvider(true); } foreach ($classDescription->{'group-sequence'} as $groupSequence) { if (count($groupSequence->value) > 0) { $metadata->setGroupSequence($this->parseValues($groupSequence[0]->value)); } } foreach ($this->parseConstraints($classDescription->constraint) as $constraint) { $metadata->addConstraint($constraint); } foreach ($classDescription->property as $property) { foreach ($this->parseConstraints($property->constraint) as $constraint) { $metadata->addPropertyConstraint((string) $property['name'], $constraint); } } foreach ($classDescription->getter as $getter) { foreach ($this->parseConstraints($getter->constraint) as $constraint) { $metadata->addGetterConstraint((string) $getter['property'], $constraint); } } }
/** * Test MetaData merge with parent annotation. */ public function testLoadClassMetadataAndMerge() { $loader = new AnnotationLoader(new AnnotationReader()); // Load Parent MetaData $parent_metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\EntityParent'); $loader->loadClassMetadata($parent_metadata); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); // Merge parent metaData. $metadata->mergeConstraints($parent_metadata); $loader->loadClassMetadata($metadata); $expected_parent = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\EntityParent'); $expected_parent->addPropertyConstraint('other', new NotNull()); $expected_parent->getReflectionClass(); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\Entity'); $expected->mergeConstraints($expected_parent); $expected->setGroupSequence(array('Foo', 'Entity')); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new Callback(array('Symfony\\Component\\Validator\\Tests\\Fixtures\\CallbackClass', 'callback'))); $expected->addConstraint(new Callback('validateMe')); $expected->addConstraint(new Callback('validateMeStatic')); $expected->addPropertyConstraint('firstName', new NotNull()); $expected->addPropertyConstraint('firstName', new Range(array('min' => 3))); $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3))))); $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3)))))); $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array('foo' => array(new NotNull(), new Range(array('min' => 3))), 'bar' => new Range(array('min' => 5)))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); $expected->addGetterConstraint('valid', new IsTrue()); $expected->addGetterConstraint('permissions', new IsTrue()); // load reflection class so that the comparison passes $expected->getReflectionClass(); $this->assertEquals($expected, $metadata); }
/** * @param ClassMetadata $metadata */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('username', new Assert\NotBlank(array('message' => 'mautic.user.user.username.notblank'))); $metadata->addConstraint(new UniqueEntity(array('fields' => array('username'), 'message' => 'mautic.user.user.username.unique', 'repositoryMethod' => 'checkUniqueUsernameEmail'))); $metadata->addPropertyConstraint('firstName', new Assert\NotBlank(array('message' => 'mautic.user.user.firstname.notblank'))); $metadata->addPropertyConstraint('lastName', new Assert\NotBlank(array('message' => 'mautic.user.user.lastname.notblank'))); $metadata->addPropertyConstraint('email', new Assert\NotBlank(array('message' => 'mautic.user.user.email.valid'))); $metadata->addPropertyConstraint('email', new Assert\Email(array('message' => 'mautic.user.user.email.valid', 'groups' => array('SecondPass')))); $metadata->addConstraint(new UniqueEntity(array('fields' => array('email'), 'message' => 'mautic.user.user.email.unique', 'repositoryMethod' => 'checkUniqueUsernameEmail'))); $metadata->addPropertyConstraint('role', new Assert\NotBlank(array('message' => 'mautic.user.user.role.notblank'))); $metadata->addPropertyConstraint('plainPassword', new Assert\NotBlank(array('message' => 'mautic.user.user.password.notblank', 'groups' => array('CheckPassword')))); $metadata->addPropertyConstraint('plainPassword', new Assert\Length(array('min' => 6, 'minMessage' => 'mautic.user.user.password.minlength', 'groups' => array('CheckPassword')))); $metadata->setGroupSequence(array('User', 'SecondPass', 'CheckPassword')); }
/** * Loads the validation metadata from the given YAML class description. * * @param ClassMetadata $metadata The metadata to load * @param array $classDescription The YAML class description */ private function loadClassMetadataFromYaml(ClassMetadata $metadata, array $classDescription) { if (isset($classDescription['group_sequence_provider'])) { $metadata->setGroupSequenceProvider((bool) $classDescription['group_sequence_provider']); } if (isset($classDescription['group_sequence'])) { $metadata->setGroupSequence($classDescription['group_sequence']); } if (isset($classDescription['constraints']) && is_array($classDescription['constraints'])) { foreach ($this->parseNodes($classDescription['constraints']) as $constraint) { $metadata->addConstraint($constraint); } } if (isset($classDescription['properties']) && is_array($classDescription['properties'])) { foreach ($classDescription['properties'] as $property => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addPropertyConstraint($property, $constraint); } } } } if (isset($classDescription['getters']) && is_array($classDescription['getters'])) { foreach ($classDescription['getters'] as $getter => $constraints) { if (null !== $constraints) { foreach ($this->parseNodes($constraints) as $constraint) { $metadata->addGetterConstraint($getter, $constraint); } } } } }