MemberMetadataTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Mapping;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraints\Valid;
  13. use Symfony\Component\Validator\Mapping\MemberMetadata;
  14. use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
  15. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  16. use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
  17. class MemberMetadataTest extends TestCase
  18. {
  19. protected $metadata;
  20. protected function setUp()
  21. {
  22. $this->metadata = new TestMemberMetadata(
  23. 'Symfony\Component\Validator\Tests\Fixtures\Entity',
  24. 'getLastName',
  25. 'lastName'
  26. );
  27. }
  28. protected function tearDown()
  29. {
  30. $this->metadata = null;
  31. }
  32. /**
  33. * @group legacy
  34. */
  35. public function testLegacyAddValidSetsMemberToCascaded()
  36. {
  37. $result = $this->metadata->addConstraint(new Valid());
  38. $this->assertEquals(array(), $this->metadata->getConstraints());
  39. $this->assertEquals($result, $this->metadata);
  40. $this->assertTrue($this->metadata->isCascaded());
  41. }
  42. /**
  43. * @group legacy
  44. */
  45. public function testLegacyAddOtherConstraintDoesNotSetMemberToCascaded()
  46. {
  47. $result = $this->metadata->addConstraint($constraint = new ConstraintA());
  48. $this->assertEquals(array($constraint), $this->metadata->getConstraints());
  49. $this->assertEquals($result, $this->metadata);
  50. $this->assertFalse($this->metadata->isCascaded());
  51. }
  52. public function testAddConstraintRequiresClassConstraints()
  53. {
  54. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  55. $this->metadata->addConstraint(new ClassConstraint());
  56. }
  57. public function testSerialize()
  58. {
  59. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  60. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  61. $metadata = unserialize(serialize($this->metadata));
  62. $this->assertEquals($this->metadata, $metadata);
  63. }
  64. public function testSerializeCollectionCascaded()
  65. {
  66. $this->metadata->addConstraint(new Valid(array('traverse' => true)));
  67. $metadata = unserialize(serialize($this->metadata));
  68. $this->assertEquals($this->metadata, $metadata);
  69. }
  70. /**
  71. * @group legacy
  72. */
  73. public function testLegacySerializeCollectionCascadedDeeply()
  74. {
  75. $this->metadata->addConstraint(new Valid(array('traverse' => true)));
  76. $metadata = unserialize(serialize($this->metadata));
  77. $this->assertEquals($this->metadata, $metadata);
  78. }
  79. public function testSerializeCollectionNotCascaded()
  80. {
  81. $this->metadata->addConstraint(new Valid(array('traverse' => false)));
  82. $metadata = unserialize(serialize($this->metadata));
  83. $this->assertEquals($this->metadata, $metadata);
  84. }
  85. }
  86. class TestMemberMetadata extends MemberMetadata
  87. {
  88. public function getPropertyValue($object)
  89. {
  90. }
  91. protected function newReflectionMember($object)
  92. {
  93. }
  94. }