4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Mapping\Loader;
14 use Doctrine\Common\Annotations\AnnotationReader;
15 use PHPUnit\Framework\TestCase;
16 use Symfony\Component\Validator\Constraints\All;
17 use Symfony\Component\Validator\Constraints\Callback;
18 use Symfony\Component\Validator\Constraints\Choice;
19 use Symfony\Component\Validator\Constraints\Collection;
20 use Symfony\Component\Validator\Constraints\IsTrue;
21 use Symfony\Component\Validator\Constraints\NotNull;
22 use Symfony\Component\Validator\Constraints\Range;
23 use Symfony\Component\Validator\Constraints\Valid;
24 use Symfony\Component\Validator\Mapping\ClassMetadata;
25 use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
26 use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
28 class AnnotationLoaderTest extends TestCase
30 public function testLoadClassMetadataReturnsTrueIfSuccessful()
32 $reader = new AnnotationReader();
33 $loader = new AnnotationLoader($reader);
34 $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
36 $this->assertTrue($loader->loadClassMetadata($metadata));
39 public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
41 $loader = new AnnotationLoader(new AnnotationReader());
42 $metadata = new ClassMetadata('\stdClass');
44 $this->assertFalse($loader->loadClassMetadata($metadata));
47 public function testLoadClassMetadata()
49 $loader = new AnnotationLoader(new AnnotationReader());
50 $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
52 $loader->loadClassMetadata($metadata);
54 $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
55 $expected->setGroupSequence(array('Foo', 'Entity'));
56 $expected->addConstraint(new ConstraintA());
57 $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
58 $expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
59 $expected->addConstraint(new Callback('validateMeStatic'));
60 $expected->addPropertyConstraint('firstName', new NotNull());
61 $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
62 $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
63 $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
64 $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
65 'foo' => array(new NotNull(), new Range(array('min' => 3))),
66 'bar' => new Range(array('min' => 5)),
68 $expected->addPropertyConstraint('firstName', new Choice(array(
69 'message' => 'Must be one of %choices%',
70 'choices' => array('A', 'B'),
72 $expected->addPropertyConstraint('childA', new Valid());
73 $expected->addPropertyConstraint('childB', new Valid());
74 $expected->addGetterConstraint('lastName', new NotNull());
75 $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
76 $expected->addGetterConstraint('permissions', new IsTrue());
78 // load reflection class so that the comparison passes
79 $expected->getReflectionClass();
81 $this->assertEquals($expected, $metadata);
85 * Test MetaData merge with parent annotation.
87 public function testLoadParentClassMetadata()
89 $loader = new AnnotationLoader(new AnnotationReader());
91 // Load Parent MetaData
92 $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
93 $loader->loadClassMetadata($parent_metadata);
95 $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
96 $expected_parent->addPropertyConstraint('other', new NotNull());
97 $expected_parent->getReflectionClass();
99 $this->assertEquals($expected_parent, $parent_metadata);
103 * Test MetaData merge with parent annotation.
105 public function testLoadClassMetadataAndMerge()
107 $loader = new AnnotationLoader(new AnnotationReader());
109 // Load Parent MetaData
110 $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
111 $loader->loadClassMetadata($parent_metadata);
113 $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
115 // Merge parent metaData.
116 $metadata->mergeConstraints($parent_metadata);
118 $loader->loadClassMetadata($metadata);
120 $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
121 $expected_parent->addPropertyConstraint('other', new NotNull());
122 $expected_parent->getReflectionClass();
124 $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
125 $expected->mergeConstraints($expected_parent);
127 $expected->setGroupSequence(array('Foo', 'Entity'));
128 $expected->addConstraint(new ConstraintA());
129 $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
130 $expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
131 $expected->addConstraint(new Callback('validateMeStatic'));
132 $expected->addPropertyConstraint('firstName', new NotNull());
133 $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
134 $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
135 $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
136 $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
137 'foo' => array(new NotNull(), new Range(array('min' => 3))),
138 'bar' => new Range(array('min' => 5)),
140 $expected->addPropertyConstraint('firstName', new Choice(array(
141 'message' => 'Must be one of %choices%',
142 'choices' => array('A', 'B'),
144 $expected->addPropertyConstraint('childA', new Valid());
145 $expected->addPropertyConstraint('childB', new Valid());
146 $expected->addGetterConstraint('lastName', new NotNull());
147 $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
148 $expected->addGetterConstraint('permissions', new IsTrue());
150 // load reflection class so that the comparison passes
151 $expected->getReflectionClass();
153 $this->assertEquals($expected, $metadata);
156 public function testLoadGroupSequenceProviderAnnotation()
158 $loader = new AnnotationLoader(new AnnotationReader());
160 $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
161 $loader->loadClassMetadata($metadata);
163 $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
164 $expected->setGroupSequenceProvider(true);
165 $expected->getReflectionClass();
167 $this->assertEquals($expected, $metadata);