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;
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;
22 class ClassMetadataTest extends TestCase
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';
30 protected function setUp()
32 $this->metadata = new ClassMetadata(self::CLASSNAME);
35 protected function tearDown()
37 $this->metadata = null;
40 public function testAddConstraintDoesNotAcceptValid()
42 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
44 $this->metadata->addConstraint(new Valid());
47 public function testAddConstraintRequiresClassConstraints()
49 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
51 $this->metadata->addConstraint(new PropertyConstraint());
54 public function testAddPropertyConstraints()
56 $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
57 $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
59 $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
62 public function testAddMultiplePropertyConstraints()
64 $this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
67 new ConstraintA(array('groups' => array('Default', 'Entity'))),
68 new ConstraintB(array('groups' => array('Default', 'Entity'))),
71 $properties = $this->metadata->getPropertyMetadata('lastName');
73 $this->assertCount(1, $properties);
74 $this->assertEquals('lastName', $properties[0]->getName());
75 $this->assertEquals($constraints, $properties[0]->getConstraints());
78 public function testAddGetterConstraints()
80 $this->metadata->addGetterConstraint('lastName', new ConstraintA());
81 $this->metadata->addGetterConstraint('lastName', new ConstraintB());
84 new ConstraintA(array('groups' => array('Default', 'Entity'))),
85 new ConstraintB(array('groups' => array('Default', 'Entity'))),
88 $properties = $this->metadata->getPropertyMetadata('lastName');
90 $this->assertCount(1, $properties);
91 $this->assertEquals('getLastName', $properties[0]->getName());
92 $this->assertEquals($constraints, $properties[0]->getConstraints());
95 public function testAddMultipleGetterConstraints()
97 $this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
100 new ConstraintA(array('groups' => array('Default', 'Entity'))),
101 new ConstraintB(array('groups' => array('Default', 'Entity'))),
104 $properties = $this->metadata->getPropertyMetadata('lastName');
106 $this->assertCount(1, $properties);
107 $this->assertEquals('getLastName', $properties[0]->getName());
108 $this->assertEquals($constraints, $properties[0]->getConstraints());
111 public function testMergeConstraintsMergesClassConstraints()
113 $parent = new ClassMetadata(self::PARENTCLASS);
114 $parent->addConstraint(new ConstraintA());
116 $this->metadata->mergeConstraints($parent);
117 $this->metadata->addConstraint(new ConstraintA());
119 $constraints = array(
120 new ConstraintA(array('groups' => array(
125 new ConstraintA(array('groups' => array(
131 $this->assertEquals($constraints, $this->metadata->getConstraints());
134 public function testMergeConstraintsMergesMemberConstraints()
136 $parent = new ClassMetadata(self::PARENTCLASS);
137 $parent->addPropertyConstraint('firstName', new ConstraintA());
138 $parent->addPropertyConstraint('firstName', new ConstraintB(array('groups' => 'foo')));
140 $this->metadata->mergeConstraints($parent);
141 $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
143 $constraintA1 = new ConstraintA(array('groups' => array(
148 $constraintA2 = new ConstraintA(array('groups' => array(
152 $constraintB = new ConstraintB(array(
153 'groups' => array('foo'),
156 $constraints = array(
162 $constraintsByGroup = array(
167 'EntityParent' => array(
179 $members = $this->metadata->getPropertyMetadata('firstName');
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);
187 public function testMemberMetadatas()
189 $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
191 $this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
192 $this->assertFalse($this->metadata->hasPropertyMetadata('non_existent_field'));
195 public function testMergeConstraintsKeepsPrivateMembersSeparate()
197 $parent = new ClassMetadata(self::PARENTCLASS);
198 $parent->addPropertyConstraint('internal', new ConstraintA());
200 $this->metadata->mergeConstraints($parent);
201 $this->metadata->addPropertyConstraint('internal', new ConstraintA());
203 $parentConstraints = array(
204 new ConstraintA(array('groups' => array(
210 $constraints = array(
211 new ConstraintA(array('groups' => array(
217 $members = $this->metadata->getPropertyMetadata('internal');
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());
226 public function testGetReflectionClass()
228 $reflClass = new \ReflectionClass(self::CLASSNAME);
230 $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
233 public function testSerialize()
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());
240 $metadata = unserialize(serialize($this->metadata));
242 $this->assertEquals($this->metadata, $metadata);
245 public function testGroupSequencesWorkIfContainingDefaultGroup()
247 $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
249 $this->assertInstanceOf('Symfony\Component\Validator\Constraints\GroupSequence', $this->metadata->getGroupSequence());
253 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
255 public function testGroupSequencesFailIfNotContainingDefaultGroup()
257 $this->metadata->setGroupSequence(array('Foo', 'Bar'));
261 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
263 public function testGroupSequencesFailIfContainingDefault()
265 $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
269 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
271 public function testGroupSequenceFailsIfGroupSequenceProviderIsSet()
273 $metadata = new ClassMetadata(self::PROVIDERCLASS);
274 $metadata->setGroupSequenceProvider(true);
275 $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
279 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
281 public function testGroupSequenceProviderFailsIfGroupSequenceIsSet()
283 $metadata = new ClassMetadata(self::PROVIDERCLASS);
284 $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
285 $metadata->setGroupSequenceProvider(true);
289 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
291 public function testGroupSequenceProviderFailsIfDomainClassIsInvalid()
293 $metadata = new ClassMetadata('stdClass');
294 $metadata->setGroupSequenceProvider(true);
297 public function testGroupSequenceProvider()
299 $metadata = new ClassMetadata(self::PROVIDERCLASS);
300 $metadata->setGroupSequenceProvider(true);
301 $this->assertTrue($metadata->isGroupSequenceProvider());
305 * https://github.com/symfony/symfony/issues/11604.
307 public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata()
309 $this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
318 class ChildClass extends ParentClass
320 public $example = 1; // overrides parent property of same name