MemberMetadataTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Tests\Component\Validator\Mapping;
  11. require_once __DIR__.'/../Fixtures/ConstraintA.php';
  12. require_once __DIR__.'/../Fixtures/ConstraintB.php';
  13. require_once __DIR__.'/../Fixtures/ClassConstraint.php';
  14. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  15. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  16. use Symfony\Tests\Component\Validator\Fixtures\ClassConstraint;
  17. use Symfony\Component\Validator\Constraints\Valid;
  18. use Symfony\Component\Validator\Mapping\MemberMetadata;
  19. class MemberMetadataTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected $metadata;
  22. protected function setUp()
  23. {
  24. $this->metadata = new TestMemberMetadata(
  25. 'Symfony\Tests\Component\Validator\Fixtures\Entity',
  26. 'getLastName',
  27. 'lastName'
  28. );
  29. }
  30. protected function tearDown()
  31. {
  32. $this->metadata = null;
  33. }
  34. public function testAddValidSetsMemberToCascaded()
  35. {
  36. $result = $this->metadata->addConstraint(new Valid());
  37. $this->assertEquals(array(), $this->metadata->getConstraints());
  38. $this->assertEquals($result, $this->metadata);
  39. $this->assertTrue($this->metadata->isCascaded());
  40. }
  41. public function testAddOtherConstraintDoesNotSetMemberToCascaded()
  42. {
  43. $result = $this->metadata->addConstraint($constraint = new ConstraintA());
  44. $this->assertEquals(array($constraint), $this->metadata->getConstraints());
  45. $this->assertEquals($result, $this->metadata);
  46. $this->assertFalse($this->metadata->isCascaded());
  47. }
  48. public function testAddConstraintRequiresClassConstraints()
  49. {
  50. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  51. $this->metadata->addConstraint(new ClassConstraint());
  52. }
  53. public function testSerialize()
  54. {
  55. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  56. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  57. $metadata = unserialize(serialize($this->metadata));
  58. $this->assertEquals($this->metadata, $metadata);
  59. }
  60. }
  61. class TestMemberMetadata extends MemberMetadata
  62. {
  63. public function getValue($object)
  64. {
  65. }
  66. protected function newReflectionMember()
  67. {
  68. }
  69. }