StaticMethodLoaderTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Component\Validator\Tests\Mapping\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Mapping\ClassMetadata;
  13. use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
  14. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  15. class StaticMethodLoaderTest extends TestCase
  16. {
  17. private $errorLevel;
  18. protected function setUp()
  19. {
  20. $this->errorLevel = error_reporting();
  21. }
  22. protected function tearDown()
  23. {
  24. error_reporting($this->errorLevel);
  25. }
  26. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  27. {
  28. $loader = new StaticMethodLoader('loadMetadata');
  29. $metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderEntity');
  30. $this->assertTrue($loader->loadClassMetadata($metadata));
  31. }
  32. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  33. {
  34. $loader = new StaticMethodLoader('loadMetadata');
  35. $metadata = new ClassMetadata('\stdClass');
  36. $this->assertFalse($loader->loadClassMetadata($metadata));
  37. }
  38. public function testLoadClassMetadata()
  39. {
  40. $loader = new StaticMethodLoader('loadMetadata');
  41. $metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderEntity');
  42. $loader->loadClassMetadata($metadata);
  43. $this->assertEquals(StaticLoaderEntity::$invokedWith, $metadata);
  44. }
  45. public function testLoadClassMetadataDoesNotRepeatLoadWithParentClasses()
  46. {
  47. $loader = new StaticMethodLoader('loadMetadata');
  48. $metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderDocument');
  49. $loader->loadClassMetadata($metadata);
  50. $this->assertCount(0, $metadata->getConstraints());
  51. $loader = new StaticMethodLoader('loadMetadata');
  52. $metadata = new ClassMetadata(__NAMESPACE__.'\BaseStaticLoaderDocument');
  53. $loader->loadClassMetadata($metadata);
  54. $this->assertCount(1, $metadata->getConstraints());
  55. }
  56. public function testLoadClassMetadataIgnoresInterfaces()
  57. {
  58. $loader = new StaticMethodLoader('loadMetadata');
  59. $metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderInterface');
  60. $loader->loadClassMetadata($metadata);
  61. $this->assertCount(0, $metadata->getConstraints());
  62. }
  63. public function testLoadClassMetadataInAbstractClasses()
  64. {
  65. $loader = new StaticMethodLoader('loadMetadata');
  66. $metadata = new ClassMetadata(__NAMESPACE__.'\AbstractStaticLoader');
  67. $loader->loadClassMetadata($metadata);
  68. $this->assertCount(1, $metadata->getConstraints());
  69. }
  70. public function testLoadClassMetadataIgnoresAbstractMethods()
  71. {
  72. // Disable error reporting, as AbstractStaticMethodLoader produces a
  73. // strict standards error
  74. error_reporting(0);
  75. $metadata = new ClassMetadata(__NAMESPACE__.'\AbstractStaticMethodLoader');
  76. $loader = new StaticMethodLoader('loadMetadata');
  77. $loader->loadClassMetadata($metadata);
  78. $this->assertCount(0, $metadata->getConstraints());
  79. }
  80. }
  81. interface StaticLoaderInterface
  82. {
  83. public static function loadMetadata(ClassMetadata $metadata);
  84. }
  85. abstract class AbstractStaticLoader
  86. {
  87. public static function loadMetadata(ClassMetadata $metadata)
  88. {
  89. $metadata->addConstraint(new ConstraintA());
  90. }
  91. }
  92. class StaticLoaderEntity
  93. {
  94. public static $invokedWith = null;
  95. public static function loadMetadata(ClassMetadata $metadata)
  96. {
  97. self::$invokedWith = $metadata;
  98. }
  99. }
  100. class StaticLoaderDocument extends BaseStaticLoaderDocument
  101. {
  102. }
  103. class BaseStaticLoaderDocument
  104. {
  105. public static function loadMetadata(ClassMetadata $metadata)
  106. {
  107. $metadata->addConstraint(new ConstraintA());
  108. }
  109. }