ElementMetadataTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  14. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  15. use Symfony\Component\Validator\Mapping\ElementMetadata;
  16. class ElementMetadataTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected $metadata;
  19. protected function setUp()
  20. {
  21. $this->metadata = new TestElementMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  22. }
  23. public function testAddConstraints()
  24. {
  25. $this->metadata->addConstraint($constraint1 = new ConstraintA());
  26. $this->metadata->addConstraint($constraint2 = new ConstraintA());
  27. $this->assertEquals(array($constraint1, $constraint2), $this->metadata->getConstraints());
  28. }
  29. public function testMultipleConstraintsOfTheSameType()
  30. {
  31. $constraint1 = new ConstraintA(array('property1' => 'A'));
  32. $constraint2 = new ConstraintA(array('property1' => 'B'));
  33. $this->metadata->addConstraint($constraint1);
  34. $this->metadata->addConstraint($constraint2);
  35. $this->assertEquals(array($constraint1, $constraint2), $this->metadata->getConstraints());
  36. }
  37. public function testFindConstraintsByGroup()
  38. {
  39. $constraint1 = new ConstraintA(array('groups' => 'TestGroup'));
  40. $constraint2 = new ConstraintB();
  41. $this->metadata->addConstraint($constraint1);
  42. $this->metadata->addConstraint($constraint2);
  43. $this->assertEquals(array($constraint1), $this->metadata->findConstraints('TestGroup'));
  44. }
  45. public function testSerialize()
  46. {
  47. $this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
  48. $this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
  49. $metadata = unserialize(serialize($this->metadata));
  50. $this->assertEquals($this->metadata, $metadata);
  51. }
  52. }
  53. class TestElementMetadata extends ElementMetadata {}