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';
27 const PROVIDERCHILDCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderChildEntity';
31 protected function setUp()
33 $this->metadata = new ClassMetadata(self::CLASSNAME);
36 protected function tearDown()
38 $this->metadata = null;
41 public function testAddConstraintDoesNotAcceptValid()
43 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
45 $this->metadata->addConstraint(new Valid());
48 public function testAddConstraintRequiresClassConstraints()
50 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
52 $this->metadata->addConstraint(new PropertyConstraint());
55 public function testAddPropertyConstraints()
57 $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
58 $this->metadata->addPropertyConstraint('lastName', new ConstraintB());
60 $this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
63 public function testAddMultiplePropertyConstraints()
65 $this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
68 new ConstraintA(array('groups' => array('Default', 'Entity'))),
69 new ConstraintB(array('groups' => array('Default', 'Entity'))),
72 $properties = $this->metadata->getPropertyMetadata('lastName');
74 $this->assertCount(1, $properties);
75 $this->assertEquals('lastName', $properties[0]->getName());
76 $this->assertEquals($constraints, $properties[0]->getConstraints());
79 public function testAddGetterConstraints()
81 $this->metadata->addGetterConstraint('lastName', new ConstraintA());
82 $this->metadata->addGetterConstraint('lastName', new ConstraintB());
85 new ConstraintA(array('groups' => array('Default', 'Entity'))),
86 new ConstraintB(array('groups' => array('Default', 'Entity'))),
89 $properties = $this->metadata->getPropertyMetadata('lastName');
91 $this->assertCount(1, $properties);
92 $this->assertEquals('getLastName', $properties[0]->getName());
93 $this->assertEquals($constraints, $properties[0]->getConstraints());
96 public function testAddMultipleGetterConstraints()
98 $this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
100 $constraints = array(
101 new ConstraintA(array('groups' => array('Default', 'Entity'))),
102 new ConstraintB(array('groups' => array('Default', 'Entity'))),
105 $properties = $this->metadata->getPropertyMetadata('lastName');
107 $this->assertCount(1, $properties);
108 $this->assertEquals('getLastName', $properties[0]->getName());
109 $this->assertEquals($constraints, $properties[0]->getConstraints());
112 public function testMergeConstraintsMergesClassConstraints()
114 $parent = new ClassMetadata(self::PARENTCLASS);
115 $parent->addConstraint(new ConstraintA());
117 $this->metadata->mergeConstraints($parent);
118 $this->metadata->addConstraint(new ConstraintA());
120 $constraints = array(
121 new ConstraintA(array('groups' => array(
126 new ConstraintA(array('groups' => array(
132 $this->assertEquals($constraints, $this->metadata->getConstraints());
135 public function testMergeConstraintsMergesMemberConstraints()
137 $parent = new ClassMetadata(self::PARENTCLASS);
138 $parent->addPropertyConstraint('firstName', new ConstraintA());
139 $parent->addPropertyConstraint('firstName', new ConstraintB(array('groups' => 'foo')));
141 $this->metadata->mergeConstraints($parent);
142 $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
144 $constraintA1 = new ConstraintA(array('groups' => array(
149 $constraintA2 = new ConstraintA(array('groups' => array(
153 $constraintB = new ConstraintB(array(
154 'groups' => array('foo'),
157 $constraints = array(
163 $constraintsByGroup = array(
168 'EntityParent' => array(
180 $members = $this->metadata->getPropertyMetadata('firstName');
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);
188 public function testMemberMetadatas()
190 $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
192 $this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
193 $this->assertFalse($this->metadata->hasPropertyMetadata('non_existent_field'));
196 public function testMergeConstraintsKeepsPrivateMembersSeparate()
198 $parent = new ClassMetadata(self::PARENTCLASS);
199 $parent->addPropertyConstraint('internal', new ConstraintA());
201 $this->metadata->mergeConstraints($parent);
202 $this->metadata->addPropertyConstraint('internal', new ConstraintA());
204 $parentConstraints = array(
205 new ConstraintA(array('groups' => array(
211 $constraints = array(
212 new ConstraintA(array('groups' => array(
218 $members = $this->metadata->getPropertyMetadata('internal');
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());
227 public function testGetReflectionClass()
229 $reflClass = new \ReflectionClass(self::CLASSNAME);
231 $this->assertEquals($reflClass, $this->metadata->getReflectionClass());
234 public function testSerialize()
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());
241 $metadata = unserialize(serialize($this->metadata));
243 $this->assertEquals($this->metadata, $metadata);
246 public function testGroupSequencesWorkIfContainingDefaultGroup()
248 $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
250 $this->assertInstanceOf('Symfony\Component\Validator\Constraints\GroupSequence', $this->metadata->getGroupSequence());
254 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
256 public function testGroupSequencesFailIfNotContainingDefaultGroup()
258 $this->metadata->setGroupSequence(array('Foo', 'Bar'));
262 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
264 public function testGroupSequencesFailIfContainingDefault()
266 $this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
270 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
272 public function testGroupSequenceFailsIfGroupSequenceProviderIsSet()
274 $metadata = new ClassMetadata(self::PROVIDERCLASS);
275 $metadata->setGroupSequenceProvider(true);
276 $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
280 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
282 public function testGroupSequenceProviderFailsIfGroupSequenceIsSet()
284 $metadata = new ClassMetadata(self::PROVIDERCLASS);
285 $metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
286 $metadata->setGroupSequenceProvider(true);
290 * @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
292 public function testGroupSequenceProviderFailsIfDomainClassIsInvalid()
294 $metadata = new ClassMetadata('stdClass');
295 $metadata->setGroupSequenceProvider(true);
298 public function testGroupSequenceProvider()
300 $metadata = new ClassMetadata(self::PROVIDERCLASS);
301 $metadata->setGroupSequenceProvider(true);
302 $this->assertTrue($metadata->isGroupSequenceProvider());
305 public function testMergeConstraintsMergesGroupSequenceProvider()
307 $parent = new ClassMetadata(self::PROVIDERCLASS);
308 $parent->setGroupSequenceProvider(true);
310 $metadata = new ClassMetadata(self::PROVIDERCHILDCLASS);
311 $metadata->mergeConstraints($parent);
313 $this->assertTrue($metadata->isGroupSequenceProvider());
317 * https://github.com/symfony/symfony/issues/11604.
319 public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata()
321 $this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');