YamlFileLoaderTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Mapping\ClassMetadata;
  20. use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
  21. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  22. use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
  23. class YamlFileLoaderTest extends TestCase
  24. {
  25. public function testLoadClassMetadataReturnsFalseIfEmpty()
  26. {
  27. $loader = new YamlFileLoader(__DIR__.'/empty-mapping.yml');
  28. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  29. $this->assertFalse($loader->loadClassMetadata($metadata));
  30. $r = new \ReflectionProperty($loader, 'classes');
  31. $r->setAccessible(true);
  32. $this->assertSame(array(), $r->getValue($loader));
  33. }
  34. /**
  35. * @dataProvider provideInvalidYamlFiles
  36. * @expectedException \InvalidArgumentException
  37. */
  38. public function testInvalidYamlFiles($path)
  39. {
  40. $loader = new YamlFileLoader(__DIR__.'/'.$path);
  41. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  42. $loader->loadClassMetadata($metadata);
  43. }
  44. public function provideInvalidYamlFiles()
  45. {
  46. return array(
  47. array('nonvalid-mapping.yml'),
  48. array('bad-format.yml'),
  49. );
  50. }
  51. /**
  52. * @see https://github.com/symfony/symfony/pull/12158
  53. */
  54. public function testDoNotModifyStateIfExceptionIsThrown()
  55. {
  56. $loader = new YamlFileLoader(__DIR__.'/nonvalid-mapping.yml');
  57. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  58. try {
  59. $loader->loadClassMetadata($metadata);
  60. } catch (\InvalidArgumentException $e) {
  61. // Call again. Again an exception should be thrown
  62. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\InvalidArgumentException');
  63. $loader->loadClassMetadata($metadata);
  64. }
  65. }
  66. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  67. {
  68. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  69. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  70. $this->assertTrue($loader->loadClassMetadata($metadata));
  71. }
  72. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  73. {
  74. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  75. $metadata = new ClassMetadata('\stdClass');
  76. $this->assertFalse($loader->loadClassMetadata($metadata));
  77. }
  78. public function testLoadClassMetadata()
  79. {
  80. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  81. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  82. $loader->loadClassMetadata($metadata);
  83. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
  84. $expected->setGroupSequence(array('Foo', 'Entity'));
  85. $expected->addConstraint(new ConstraintA());
  86. $expected->addConstraint(new ConstraintB());
  87. $expected->addConstraint(new Callback('validateMe'));
  88. $expected->addConstraint(new Callback('validateMeStatic'));
  89. $expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
  90. $expected->addPropertyConstraint('firstName', new NotNull());
  91. $expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
  92. $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
  93. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
  94. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
  95. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  96. 'foo' => array(new NotNull(), new Range(array('min' => 3))),
  97. 'bar' => array(new Range(array('min' => 5))),
  98. ))));
  99. $expected->addPropertyConstraint('firstName', new Choice(array(
  100. 'message' => 'Must be one of %choices%',
  101. 'choices' => array('A', 'B'),
  102. )));
  103. $expected->addGetterConstraint('lastName', new NotNull());
  104. $expected->addGetterConstraint('valid', new IsTrue());
  105. $expected->addGetterConstraint('permissions', new IsTrue());
  106. $this->assertEquals($expected, $metadata);
  107. }
  108. public function testLoadGroupSequenceProvider()
  109. {
  110. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  111. $metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
  112. $loader->loadClassMetadata($metadata);
  113. $expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
  114. $expected->setGroupSequenceProvider(true);
  115. $this->assertEquals($expected, $metadata);
  116. }
  117. }