MemberMetadataTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. public function testAddValidSetsMemberToCascaded()
  31. {
  32. $result = $this->metadata->addConstraint(new Valid());
  33. $this->assertEquals(array(), $this->metadata->getConstraints());
  34. $this->assertEquals($result, $this->metadata);
  35. $this->assertTrue($this->metadata->isCascaded());
  36. }
  37. public function testAddOtherConstraintDoesNotSetMemberToCascaded()
  38. {
  39. $result = $this->metadata->addConstraint($constraint = new ConstraintA());
  40. $this->assertEquals(array($constraint), $this->metadata->getConstraints());
  41. $this->assertEquals($result, $this->metadata);
  42. $this->assertFalse($this->metadata->isCascaded());
  43. }
  44. public function testAddConstraintRequiresClassConstraints()
  45. {
  46. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  47. $this->metadata->addConstraint(new ClassConstraint());
  48. }
  49. public function testSerialize()
  50. {
  51. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  52. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  53. $metadata = unserialize(serialize($this->metadata));
  54. $this->assertEquals($this->metadata, $metadata);
  55. }
  56. }
  57. class TestMemberMetadata extends MemberMetadata
  58. {
  59. public function getValue($object)
  60. {
  61. }
  62. protected function newReflectionMember()
  63. {
  64. }
  65. }