XmlFileLoaderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Constraints\All;
  13. use Symfony\Component\Validator\Constraints\Callback;
  14. use Symfony\Component\Validator\Constraints\Choice;
  15. use Symfony\Component\Validator\Constraints\Collection;
  16. use Symfony\Component\Validator\Constraints\IsTrue;
  17. use Symfony\Component\Validator\Constraints\NotNull;
  18. use Symfony\Component\Validator\Constraints\Range;
  19. use Symfony\Component\Validator\Constraints\Regex;
  20. use Symfony\Component\Validator\Constraints\Traverse;
  21. use Symfony\Component\Validator\Exception\MappingException;
  22. use Symfony\Component\Validator\Mapping\ClassMetadata;
  23. use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
  24. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  25. use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
  26. class XmlFileLoaderTest extends TestCase
  27. {
  28. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  29. {
  30. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  31. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  32. $this->assertTrue($loader->loadClassMetadata($metadata));
  33. }
  34. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  35. {
  36. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  37. $metadata = new ClassMetadata('\stdClass');
  38. $this->assertFalse($loader->loadClassMetadata($metadata));
  39. }
  40. public function testLoadClassMetadata()
  41. {
  42. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  43. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  44. $loader->loadClassMetadata($metadata);
  45. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  46. $expected->setGroupSequence(array('Foo', 'Entity'));
  47. $expected->addConstraint(new ConstraintA());
  48. $expected->addConstraint(new ConstraintB());
  49. $expected->addConstraint(new Callback('validateMe'));
  50. $expected->addConstraint(new Callback('validateMeStatic'));
  51. $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
  52. $expected->addConstraint(new Traverse(false));
  53. $expected->addPropertyConstraint('firstName', new NotNull());
  54. $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
  55. $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
  56. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
  57. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
  58. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  59. 'foo' => array(new NotNull(), new Range(array('min' => 3))),
  60. 'bar' => array(new Range(array('min' => 5))),
  61. ))));
  62. $expected->addPropertyConstraint('firstName', new Choice(array(
  63. 'message' => 'Must be one of %choices%',
  64. 'choices' => array('A', 'B'),
  65. )));
  66. $expected->addGetterConstraint('lastName', new NotNull());
  67. $expected->addGetterConstraint('valid', new IsTrue());
  68. $expected->addGetterConstraint('permissions', new IsTrue());
  69. $this->assertEquals($expected, $metadata);
  70. }
  71. public function testLoadClassMetadataWithNonStrings()
  72. {
  73. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping-non-strings.xml');
  74. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  75. $loader->loadClassMetadata($metadata);
  76. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  77. $expected->addPropertyConstraint('firstName', new Regex(array('pattern' => '/^1/', 'match' => false)));
  78. $properties = $metadata->getPropertyMetadata('firstName');
  79. $constraints = $properties[0]->getConstraints();
  80. $this->assertFalse($constraints[0]->match);
  81. }
  82. public function testLoadGroupSequenceProvider()
  83. {
  84. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  85. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
  86. $loader->loadClassMetadata($metadata);
  87. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
  88. $expected->setGroupSequenceProvider(true);
  89. $this->assertEquals($expected, $metadata);
  90. }
  91. public function testThrowExceptionIfDocTypeIsSet()
  92. {
  93. $loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
  94. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  95. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\Symfony\Component\Validator\Exception\MappingException');
  96. $loader->loadClassMetadata($metadata);
  97. }
  98. /**
  99. * @see https://github.com/symfony/symfony/pull/12158
  100. */
  101. public function testDoNotModifyStateIfExceptionIsThrown()
  102. {
  103. $loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
  104. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  105. try {
  106. $loader->loadClassMetadata($metadata);
  107. } catch (MappingException $e) {
  108. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\Symfony\Component\Validator\Exception\MappingException');
  109. $loader->loadClassMetadata($metadata);
  110. }
  111. }
  112. }