783e9b27d850cad54623c9e38591c1125e15980f
[yaffs-website] / vendor / symfony / validator / Tests / Mapping / Loader / AnnotationLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Mapping\Loader;
13
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;
27
28 class AnnotationLoaderTest extends TestCase
29 {
30     public function testLoadClassMetadataReturnsTrueIfSuccessful()
31     {
32         $reader = new AnnotationReader();
33         $loader = new AnnotationLoader($reader);
34         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
35
36         $this->assertTrue($loader->loadClassMetadata($metadata));
37     }
38
39     public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
40     {
41         $loader = new AnnotationLoader(new AnnotationReader());
42         $metadata = new ClassMetadata('\stdClass');
43
44         $this->assertFalse($loader->loadClassMetadata($metadata));
45     }
46
47     public function testLoadClassMetadata()
48     {
49         $loader = new AnnotationLoader(new AnnotationReader());
50         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
51
52         $loader->loadClassMetadata($metadata);
53
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)),
67         ))));
68         $expected->addPropertyConstraint('firstName', new Choice(array(
69             'message' => 'Must be one of %choices%',
70             'choices' => array('A', 'B'),
71         )));
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());
77
78         // load reflection class so that the comparison passes
79         $expected->getReflectionClass();
80
81         $this->assertEquals($expected, $metadata);
82     }
83
84     /**
85      * Test MetaData merge with parent annotation.
86      */
87     public function testLoadParentClassMetadata()
88     {
89         $loader = new AnnotationLoader(new AnnotationReader());
90
91         // Load Parent MetaData
92         $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
93         $loader->loadClassMetadata($parent_metadata);
94
95         $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
96         $expected_parent->addPropertyConstraint('other', new NotNull());
97         $expected_parent->getReflectionClass();
98
99         $this->assertEquals($expected_parent, $parent_metadata);
100     }
101
102     /**
103      * Test MetaData merge with parent annotation.
104      */
105     public function testLoadClassMetadataAndMerge()
106     {
107         $loader = new AnnotationLoader(new AnnotationReader());
108
109         // Load Parent MetaData
110         $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
111         $loader->loadClassMetadata($parent_metadata);
112
113         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
114
115         // Merge parent metaData.
116         $metadata->mergeConstraints($parent_metadata);
117
118         $loader->loadClassMetadata($metadata);
119
120         $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
121         $expected_parent->addPropertyConstraint('other', new NotNull());
122         $expected_parent->getReflectionClass();
123
124         $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
125         $expected->mergeConstraints($expected_parent);
126
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)),
139         ))));
140         $expected->addPropertyConstraint('firstName', new Choice(array(
141             'message' => 'Must be one of %choices%',
142             'choices' => array('A', 'B'),
143         )));
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());
149
150         // load reflection class so that the comparison passes
151         $expected->getReflectionClass();
152
153         $this->assertEquals($expected, $metadata);
154     }
155
156     public function testLoadGroupSequenceProviderAnnotation()
157     {
158         $loader = new AnnotationLoader(new AnnotationReader());
159
160         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
161         $loader->loadClassMetadata($metadata);
162
163         $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
164         $expected->setGroupSequenceProvider(true);
165         $expected->getReflectionClass();
166
167         $this->assertEquals($expected, $metadata);
168     }
169 }