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\Constraints\Valid;
16 use Symfony\Component\Validator\Mapping\MemberMetadata;
17 use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
18 use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
19 use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
21 class MemberMetadataTest extends TestCase
25 protected function setUp()
27 $this->metadata = new TestMemberMetadata(
28 'Symfony\Component\Validator\Tests\Fixtures\Entity',
34 protected function tearDown()
36 $this->metadata = null;
42 public function testLegacyAddValidSetsMemberToCascaded()
44 $result = $this->metadata->addConstraint(new Valid());
46 $this->assertEquals(array(), $this->metadata->getConstraints());
47 $this->assertEquals($result, $this->metadata);
48 $this->assertTrue($this->metadata->isCascaded());
54 public function testLegacyAddOtherConstraintDoesNotSetMemberToCascaded()
56 $result = $this->metadata->addConstraint($constraint = new ConstraintA());
58 $this->assertEquals(array($constraint), $this->metadata->getConstraints());
59 $this->assertEquals($result, $this->metadata);
60 $this->assertFalse($this->metadata->isCascaded());
63 public function testAddConstraintRequiresClassConstraints()
65 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
67 $this->metadata->addConstraint(new ClassConstraint());
70 public function testSerialize()
72 $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
73 $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
75 $metadata = unserialize(serialize($this->metadata));
77 $this->assertEquals($this->metadata, $metadata);
80 public function testSerializeCollectionCascaded()
82 $this->metadata->addConstraint(new Valid(array('traverse' => true)));
84 $metadata = unserialize(serialize($this->metadata));
86 $this->assertEquals($this->metadata, $metadata);
92 public function testLegacySerializeCollectionCascadedDeeply()
94 $this->metadata->addConstraint(new Valid(array('traverse' => true)));
96 $metadata = unserialize(serialize($this->metadata));
98 $this->assertEquals($this->metadata, $metadata);
101 public function testSerializeCollectionNotCascaded()
103 $this->metadata->addConstraint(new Valid(array('traverse' => false)));
105 $metadata = unserialize(serialize($this->metadata));
107 $this->assertEquals($this->metadata, $metadata);
111 class TestMemberMetadata extends MemberMetadata
113 public function getPropertyValue($object)
117 protected function newReflectionMember($object)