ElementMetadataTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. protected function tearDown()
  24. {
  25. $this->metadata = null;
  26. }
  27. public function testAddConstraints()
  28. {
  29. $this->metadata->addConstraint($constraint1 = new ConstraintA());
  30. $this->metadata->addConstraint($constraint2 = new ConstraintA());
  31. $this->assertEquals(array($constraint1, $constraint2), $this->metadata->getConstraints());
  32. }
  33. public function testMultipleConstraintsOfTheSameType()
  34. {
  35. $constraint1 = new ConstraintA(array('property1' => 'A'));
  36. $constraint2 = new ConstraintA(array('property1' => 'B'));
  37. $this->metadata->addConstraint($constraint1);
  38. $this->metadata->addConstraint($constraint2);
  39. $this->assertEquals(array($constraint1, $constraint2), $this->metadata->getConstraints());
  40. }
  41. public function testFindConstraintsByGroup()
  42. {
  43. $constraint1 = new ConstraintA(array('groups' => 'TestGroup'));
  44. $constraint2 = new ConstraintB();
  45. $this->metadata->addConstraint($constraint1);
  46. $this->metadata->addConstraint($constraint2);
  47. $this->assertEquals(array($constraint1), $this->metadata->findConstraints('TestGroup'));
  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 TestElementMetadata extends ElementMetadata {}