Minor dependency updates
[yaffs-website] / vendor / symfony / validator / Tests / Mapping / ClassMetadataTest.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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\Constraints\Valid;
17 use Symfony\Component\Validator\Mapping\ClassMetadata;
18 use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
19 use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
20 use Symfony\Component\Validator\Tests\Fixtures\PropertyConstraint;
21
22 class ClassMetadataTest extends TestCase
23 {
24     const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
25     const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
26     const PROVIDERCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity';
27
28     protected $metadata;
29
30     protected function setUp()
31     {
32         $this->metadata = new ClassMetadata(self::CLASSNAME);
33     }
34
35     protected function tearDown()
36     {
37         $this->metadata = null;
38     }
39
40     public function testAddConstraintDoesNotAcceptValid()
41     {
42         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
43
44         $this->metadata->addConstraint(new Valid());
45     }
46
47     public function testAddConstraintRequiresClassConstraints()
48     {
49         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
50
51         $this->metadata->addConstraint(new PropertyConstraint());
52     }
53
54     public function testAddPropertyConstraints()
55     {
56         $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
57         $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
58
59         $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
60     }
61
62     public function testAddMultiplePropertyConstraints()
63     {
64         $this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
65
66         $constraints = array(
67             new ConstraintA(array('groups' => array('Default', 'Entity'))),
68             new ConstraintB(array('groups' => array('Default', 'Entity'))),
69         );
70
71         $properties = $this->metadata->getPropertyMetadata('lastName');
72
73         $this->assertCount(1, $properties);
74         $this->assertEquals('lastName', $properties[0]->getName());
75         $this->assertEquals($constraints, $properties[0]->getConstraints());
76     }
77
78     public function testAddGetterConstraints()
79     {
80         $this->metadata->addGetterConstraint('lastName', new ConstraintA());
81         $this->metadata->addGetterConstraint('lastName', new ConstraintB());
82
83         $constraints = array(
84             new ConstraintA(array('groups' => array('Default', 'Entity'))),
85             new ConstraintB(array('groups' => array('Default', 'Entity'))),
86         );
87
88         $properties = $this->metadata->getPropertyMetadata('lastName');
89
90         $this->assertCount(1, $properties);
91         $this->assertEquals('getLastName', $properties[0]->getName());
92         $this->assertEquals($constraints, $properties[0]->getConstraints());
93     }
94
95     public function testAddMultipleGetterConstraints()
96     {
97         $this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
98
99         $constraints = array(
100             new ConstraintA(array('groups' => array('Default', 'Entity'))),
101             new ConstraintB(array('groups' => array('Default', 'Entity'))),
102         );
103
104         $properties = $this->metadata->getPropertyMetadata('lastName');
105
106         $this->assertCount(1, $properties);
107         $this->assertEquals('getLastName', $properties[0]->getName());
108         $this->assertEquals($constraints, $properties[0]->getConstraints());
109     }
110
111     public function testMergeConstraintsMergesClassConstraints()
112     {
113         $parent = new ClassMetadata(self::PARENTCLASS);
114         $parent->addConstraint(new ConstraintA());
115
116         $this->metadata->mergeConstraints($parent);
117         $this->metadata->addConstraint(new ConstraintA());
118
119         $constraints = array(
120             new ConstraintA(array('groups' => array(
121                 'Default',
122                 'EntityParent',
123                 'Entity',
124             ))),
125             new ConstraintA(array('groups' => array(
126                 'Default',
127                 'Entity',
128             ))),
129         );
130
131         $this->assertEquals($constraints, $this->metadata->getConstraints());
132     }
133
134     public function testMergeConstraintsMergesMemberConstraints()
135     {
136         $parent = new ClassMetadata(self::PARENTCLASS);
137         $parent->addPropertyConstraint('firstName', new ConstraintA());
138         $parent->addPropertyConstraint('firstName', new ConstraintB(array('groups' => 'foo')));
139
140         $this->metadata->mergeConstraints($parent);
141         $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
142
143         $constraintA1 = new ConstraintA(array('groups' => array(
144             'Default',
145             'EntityParent',
146             'Entity',
147         )));
148         $constraintA2 = new ConstraintA(array('groups' => array(
149             'Default',
150             'Entity',
151         )));
152         $constraintB = new ConstraintB(array(
153             'groups' => array('foo'),
154         ));
155
156         $constraints = array(
157             $constraintA1,
158             $constraintB,
159             $constraintA2,
160         );
161
162         $constraintsByGroup = array(
163             'Default' => array(
164                 $constraintA1,
165                 $constraintA2,
166             ),
167             'EntityParent' => array(
168                 $constraintA1,
169             ),
170             'Entity' => array(
171                 $constraintA1,
172                 $constraintA2,
173             ),
174             'foo' => array(
175                 $constraintB,
176             ),
177         );
178
179         $members = $this->metadata->getPropertyMetadata('firstName');
180
181         $this->assertCount(1, $members);
182         $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
183         $this->assertEquals($constraints, $members[0]->getConstraints());
184         $this->assertEquals($constraintsByGroup, $members[0]->constraintsByGroup);
185     }
186
187     public function testMemberMetadatas()
188     {
189         $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
190
191         $this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
192         $this->assertFalse($this->metadata->hasPropertyMetadata('non_existent_field'));
193     }
194
195     public function testMergeConstraintsKeepsPrivateMembersSeparate()
196     {
197         $parent = new ClassMetadata(self::PARENTCLASS);
198         $parent->addPropertyConstraint('internal', new ConstraintA());
199
200         $this->metadata->mergeConstraints($parent);
201         $this->metadata->addPropertyConstraint('internal', new ConstraintA());
202
203         $parentConstraints = array(
204             new ConstraintA(array('groups' => array(
205                 'Default',
206                 'EntityParent',
207                 'Entity',
208             ))),
209         );
210         $constraints = array(
211             new ConstraintA(array('groups' => array(
212                 'Default',
213                 'Entity',
214             ))),
215         );
216
217         $members = $this->metadata->getPropertyMetadata('internal');
218
219         $this->assertCount(2, $members);
220         $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
221         $this->assertEquals($parentConstraints, $members[0]->getConstraints());
222         $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
223         $this->assertEquals($constraints, $members[1]->getConstraints());
224     }
225
226     public function testGetReflectionClass()
227     {
228         $reflClass = new \ReflectionClass(self::CLASSNAME);
229
230         $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
231     }
232
233     public function testSerialize()
234     {
235         $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
236         $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
237         $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
238         $this->metadata->addGetterConstraint('lastName', new ConstraintB());
239
240         $metadata = unserialize(serialize($this->metadata));
241
242         $this->assertEquals($this->metadata, $metadata);
243     }
244
245     public function testGroupSequencesWorkIfContainingDefaultGroup()
246     {
247         $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
248
249         $this->assertInstanceOf('Symfony\Component\Validator\Constraints\GroupSequence', $this->metadata->getGroupSequence());
250     }
251
252     /**
253      * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
254      */
255     public function testGroupSequencesFailIfNotContainingDefaultGroup()
256     {
257         $this->metadata->setGroupSequence(array('Foo', 'Bar'));
258     }
259
260     /**
261      * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
262      */
263     public function testGroupSequencesFailIfContainingDefault()
264     {
265         $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
266     }
267
268     /**
269      * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
270      */
271     public function testGroupSequenceFailsIfGroupSequenceProviderIsSet()
272     {
273         $metadata = new ClassMetadata(self::PROVIDERCLASS);
274         $metadata->setGroupSequenceProvider(true);
275         $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
276     }
277
278     /**
279      * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
280      */
281     public function testGroupSequenceProviderFailsIfGroupSequenceIsSet()
282     {
283         $metadata = new ClassMetadata(self::PROVIDERCLASS);
284         $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
285         $metadata->setGroupSequenceProvider(true);
286     }
287
288     /**
289      * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
290      */
291     public function testGroupSequenceProviderFailsIfDomainClassIsInvalid()
292     {
293         $metadata = new ClassMetadata('stdClass');
294         $metadata->setGroupSequenceProvider(true);
295     }
296
297     public function testGroupSequenceProvider()
298     {
299         $metadata = new ClassMetadata(self::PROVIDERCLASS);
300         $metadata->setGroupSequenceProvider(true);
301         $this->assertTrue($metadata->isGroupSequenceProvider());
302     }
303
304     /**
305      * https://github.com/symfony/symfony/issues/11604.
306      */
307     public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata()
308     {
309         $this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
310     }
311 }
312
313 class ParentClass
314 {
315     public $example = 0;
316 }
317
318 class ChildClass extends ParentClass
319 {
320     public $example = 1;    // overrides parent property of same name
321 }