ClassMetadataFactoryTest.php 4.2 KB

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