XmlFileLoaderTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Loader;
  11. require_once __DIR__.'/../../Fixtures/Entity.php';
  12. require_once __DIR__.'/../../Fixtures/ConstraintA.php';
  13. require_once __DIR__.'/../../Fixtures/ConstraintB.php';
  14. use Symfony\Component\Validator\Constraints\All;
  15. use Symfony\Component\Validator\Constraints\Collection;
  16. use Symfony\Component\Validator\Constraints\NotNull;
  17. use Symfony\Component\Validator\Constraints\Min;
  18. use Symfony\Component\Validator\Constraints\Choice;
  19. use Symfony\Component\Validator\Mapping\ClassMetadata;
  20. use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
  21. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  22. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  23. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  26. {
  27. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  28. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  29. $this->assertTrue($loader->loadClassMetadata($metadata));
  30. }
  31. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  32. {
  33. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  34. $metadata = new ClassMetadata('\stdClass');
  35. $this->assertFalse($loader->loadClassMetadata($metadata));
  36. }
  37. public function testLoadClassMetadata()
  38. {
  39. $loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
  40. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  41. $loader->loadClassMetadata($metadata);
  42. $expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  43. $expected->setGroupSequence(array('Foo', 'Entity'));
  44. $expected->addConstraint(new ConstraintA());
  45. $expected->addConstraint(new ConstraintB());
  46. $expected->addPropertyConstraint('firstName', new NotNull());
  47. $expected->addPropertyConstraint('firstName', new Min(3));
  48. $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
  49. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3))));
  50. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3)))));
  51. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  52. 'foo' => array(new NotNull(), new Min(3)),
  53. 'bar' => array(new Min(5)),
  54. ))));
  55. $expected->addPropertyConstraint('firstName', new Choice(array(
  56. 'message' => 'Must be one of %choices%',
  57. 'choices' => array('A', 'B'),
  58. )));
  59. $expected->addGetterConstraint('lastName', new NotNull());
  60. $this->assertEquals($expected, $metadata);
  61. }
  62. /**
  63. * @expectedException Symfony\Component\Validator\Exception\MappingException
  64. * @expectedExceptionMessage Document types are not allowed.
  65. */
  66. public function testDocTypeIsNotAllowed()
  67. {
  68. $loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
  69. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  70. $loader->loadClassMetadata($metadata);
  71. }
  72. }