ElementMetadataTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Symfony\Tests\Component\Validator\Mapping;
  3. require_once __DIR__.'/../Fixtures/ConstraintA.php';
  4. require_once __DIR__.'/../Fixtures/ConstraintB.php';
  5. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  6. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  7. use Symfony\Component\Validator\Mapping\ElementMetadata;
  8. class ElementMetadataTest extends \PHPUnit_Framework_TestCase
  9. {
  10. protected $metadata;
  11. public function setUp()
  12. {
  13. $this->metadata = new TestElementMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  14. }
  15. public function testAddConstraints()
  16. {
  17. $this->metadata->addConstraint($constraint1 = new ConstraintA());
  18. $this->metadata->addConstraint($constraint2 = new ConstraintA());
  19. $this->assertEquals(array($constraint1, $constraint2), $this->metadata->getConstraints());
  20. }
  21. public function testMultipleConstraintsOfTheSameType()
  22. {
  23. $constraint1 = new ConstraintA(array('property1' => 'A'));
  24. $constraint2 = new ConstraintA(array('property1' => 'B'));
  25. $this->metadata->addConstraint($constraint1);
  26. $this->metadata->addConstraint($constraint2);
  27. $this->assertEquals(array($constraint1, $constraint2), $this->metadata->getConstraints());
  28. }
  29. public function testFindConstraintsByGroup()
  30. {
  31. $constraint1 = new ConstraintA(array('groups' => 'TestGroup'));
  32. $constraint2 = new ConstraintB();
  33. $this->metadata->addConstraint($constraint1);
  34. $this->metadata->addConstraint($constraint2);
  35. $this->assertEquals(array($constraint1), $this->metadata->findConstraints('TestGroup'));
  36. }
  37. public function testSerialize()
  38. {
  39. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  40. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  41. $metadata = unserialize(serialize($this->metadata));
  42. $this->assertEquals($this->metadata, $metadata);
  43. }
  44. }
  45. class TestElementMetadata extends ElementMetadata {}