ba7099201158e9b23d2f854cf5d47ad6c6a4cdb2
[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\NotNull;
21 use Symfony\Component\Validator\Constraints\Range;
22 use Symfony\Component\Validator\Constraints\IsTrue;
23 use Symfony\Component\Validator\Mapping\ClassMetadata;
24 use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
25 use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
26
27 class AnnotationLoaderTest extends TestCase
28 {
29     public function testLoadClassMetadataReturnsTrueIfSuccessful()
30     {
31         $reader = new AnnotationReader();
32         $loader = new AnnotationLoader($reader);
33         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
34
35         $this->assertTrue($loader->loadClassMetadata($metadata));
36     }
37
38     public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
39     {
40         $loader = new AnnotationLoader(new AnnotationReader());
41         $metadata = new ClassMetadata('\stdClass');
42
43         $this->assertFalse($loader->loadClassMetadata($metadata));
44     }
45
46     public function testLoadClassMetadata()
47     {
48         $loader = new AnnotationLoader(new AnnotationReader());
49         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
50
51         $loader->loadClassMetadata($metadata);
52
53         $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
54         $expected->setGroupSequence(array('Foo', 'Entity'));
55         $expected->addConstraint(new ConstraintA());
56         $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
57         $expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
58         $expected->addConstraint(new Callback('validateMeStatic'));
59         $expected->addPropertyConstraint('firstName', new NotNull());
60         $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
61         $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
62         $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
63         $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
64             'foo' => array(new NotNull(), new Range(array('min' => 3))),
65             'bar' => new Range(array('min' => 5)),
66         ))));
67         $expected->addPropertyConstraint('firstName', new Choice(array(
68             'message' => 'Must be one of %choices%',
69             'choices' => array('A', 'B'),
70         )));
71         $expected->addGetterConstraint('lastName', new NotNull());
72         $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
73         $expected->addGetterConstraint('permissions', new IsTrue());
74
75         // load reflection class so that the comparison passes
76         $expected->getReflectionClass();
77
78         $this->assertEquals($expected, $metadata);
79     }
80
81     /**
82      * Test MetaData merge with parent annotation.
83      */
84     public function testLoadParentClassMetadata()
85     {
86         $loader = new AnnotationLoader(new AnnotationReader());
87
88         // Load Parent MetaData
89         $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
90         $loader->loadClassMetadata($parent_metadata);
91
92         $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
93         $expected_parent->addPropertyConstraint('other', new NotNull());
94         $expected_parent->getReflectionClass();
95
96         $this->assertEquals($expected_parent, $parent_metadata);
97     }
98
99     /**
100      * Test MetaData merge with parent annotation.
101      */
102     public function testLoadClassMetadataAndMerge()
103     {
104         $loader = new AnnotationLoader(new AnnotationReader());
105
106         // Load Parent MetaData
107         $parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
108         $loader->loadClassMetadata($parent_metadata);
109
110         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
111
112         // Merge parent metaData.
113         $metadata->mergeConstraints($parent_metadata);
114
115         $loader->loadClassMetadata($metadata);
116
117         $expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
118         $expected_parent->addPropertyConstraint('other', new NotNull());
119         $expected_parent->getReflectionClass();
120
121         $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
122         $expected->mergeConstraints($expected_parent);
123
124         $expected->setGroupSequence(array('Foo', 'Entity'));
125         $expected->addConstraint(new ConstraintA());
126         $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
127         $expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
128         $expected->addConstraint(new Callback('validateMeStatic'));
129         $expected->addPropertyConstraint('firstName', new NotNull());
130         $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
131         $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
132         $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
133         $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
134             'foo' => array(new NotNull(), new Range(array('min' => 3))),
135             'bar' => new Range(array('min' => 5)),
136         ))));
137         $expected->addPropertyConstraint('firstName', new Choice(array(
138             'message' => 'Must be one of %choices%',
139             'choices' => array('A', 'B'),
140         )));
141         $expected->addGetterConstraint('lastName', new NotNull());
142         $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
143         $expected->addGetterConstraint('permissions', new IsTrue());
144
145         // load reflection class so that the comparison passes
146         $expected->getReflectionClass();
147
148         $this->assertEquals($expected, $metadata);
149     }
150
151     public function testLoadGroupSequenceProviderAnnotation()
152     {
153         $loader = new AnnotationLoader(new AnnotationReader());
154
155         $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
156         $loader->loadClassMetadata($metadata);
157
158         $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
159         $expected->setGroupSequenceProvider(true);
160         $expected->getReflectionClass();
161
162         $this->assertEquals($expected, $metadata);
163     }
164 }