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