ClassMetadataFactoryTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/Entity.php';
  12. require_once __DIR__.'/../Fixtures/ConstraintA.php';
  13. require_once __DIR__.'/../Fixtures/ConstraintB.php';
  14. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  15. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  16. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  17. use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
  18. use Symfony\Component\Validator\Mapping\ClassMetadata;
  19. use Symfony\Component\Validator\Mapping\PropertyMetadata;
  20. use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  21. class ClassMetadataFactoryTest extends \PHPUnit_Framework_TestCase
  22. {
  23. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  24. const PARENTCLASS = 'Symfony\Tests\Component\Validator\Fixtures\EntityParent';
  25. public function testLoadClassMetadata()
  26. {
  27. $factory = new ClassMetadataFactory(new TestLoader());
  28. $metadata = $factory->getClassMetadata(self::PARENTCLASS);
  29. $constraints = array(
  30. new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
  31. );
  32. $this->assertEquals($constraints, $metadata->getConstraints());
  33. }
  34. public function testMergeParentConstraints()
  35. {
  36. $factory = new ClassMetadataFactory(new TestLoader());
  37. $metadata = $factory->getClassMetadata(self::CLASSNAME);
  38. $constraints = array(
  39. new ConstraintA(array('groups' => array(
  40. 'Default',
  41. 'EntityParent',
  42. 'Entity',
  43. ))),
  44. new ConstraintA(array('groups' => array(
  45. 'Default',
  46. 'EntityInterface',
  47. 'Entity',
  48. ))),
  49. new ConstraintA(array('groups' => array(
  50. 'Default',
  51. 'Entity',
  52. ))),
  53. );
  54. $this->assertEquals($constraints, $metadata->getConstraints());
  55. }
  56. public function testWriteMetadataToCache()
  57. {
  58. $cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
  59. $factory = new ClassMetadataFactory(new TestLoader(), $cache);
  60. $tester = $this;
  61. $constraints = array(
  62. new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
  63. );
  64. $cache->expects($this->once())
  65. ->method('has')
  66. ->with($this->equalTo(self::PARENTCLASS))
  67. ->will($this->returnValue(false));
  68. $cache->expects($this->once())
  69. ->method('write')
  70. ->will($this->returnCallback(function($metadata) use ($tester, $constraints) {
  71. $tester->assertEquals($constraints, $metadata->getConstraints());
  72. }));
  73. $metadata = $factory->getClassMetadata(self::PARENTCLASS);
  74. $this->assertEquals(self::PARENTCLASS, $metadata->getClassName());
  75. $this->assertEquals($constraints, $metadata->getConstraints());
  76. }
  77. public function testReadMetadataFromCache()
  78. {
  79. $loader = $this->getMock('Symfony\Component\Validator\Mapping\Loader\LoaderInterface');
  80. $cache = $this->getMock('Symfony\Component\Validator\Mapping\Cache\CacheInterface');
  81. $factory = new ClassMetadataFactory($loader, $cache);
  82. $tester = $this;
  83. $metadata = new ClassMetadata(self::PARENTCLASS);
  84. $metadata->addConstraint(new ConstraintA());
  85. $loader->expects($this->never())
  86. ->method('loadClassMetadata');
  87. $cache->expects($this->once())
  88. ->method('has')
  89. ->with($this->equalTo(self::PARENTCLASS))
  90. ->will($this->returnValue(true));
  91. $cache->expects($this->once())
  92. ->method('read')
  93. ->will($this->returnValue($metadata));
  94. $this->assertEquals($metadata,$factory->getClassMetadata(self::PARENTCLASS));
  95. }
  96. }
  97. class TestLoader implements LoaderInterface
  98. {
  99. public function loadClassMetadata(ClassMetadata $metadata)
  100. {
  101. $metadata->addConstraint(new ConstraintA());
  102. }
  103. }