YamlFileLoaderTest.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\YamlFileLoader;
  21. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  22. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  23. class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testLoadClassMetadataReturnsFalseIfEmpty()
  26. {
  27. $loader = new YamlFileLoader(__DIR__.'/empty-mapping.yml');
  28. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  29. $this->assertFalse($loader->loadClassMetadata($metadata));
  30. }
  31. /**
  32. * @expectedException \InvalidArgumentException
  33. */
  34. public function testLoadClassMetadataThrowsExceptionIfNotAnArray()
  35. {
  36. $loader = new YamlFileLoader(__DIR__.'/nonvalid-mapping.yml');
  37. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  38. $loader->loadClassMetadata($metadata);
  39. }
  40. public function testLoadClassMetadataReturnsTrueIfSuccessful()
  41. {
  42. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  43. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  44. $this->assertTrue($loader->loadClassMetadata($metadata));
  45. }
  46. public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
  47. {
  48. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  49. $metadata = new ClassMetadata('\stdClass');
  50. $this->assertFalse($loader->loadClassMetadata($metadata));
  51. }
  52. public function testLoadClassMetadata()
  53. {
  54. $loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
  55. $metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  56. $loader->loadClassMetadata($metadata);
  57. $expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');
  58. $expected->addConstraint(new ConstraintA());
  59. $expected->addConstraint(new ConstraintB());
  60. $expected->addPropertyConstraint('firstName', new NotNull());
  61. $expected->addPropertyConstraint('firstName', new Min(3));
  62. $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
  63. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3))));
  64. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3)))));
  65. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  66. 'foo' => array(new NotNull(), new Min(3)),
  67. 'bar' => array(new Min(5)),
  68. ))));
  69. $expected->addPropertyConstraint('firstName', new Choice(array(
  70. 'message' => 'Must be one of %choices%',
  71. 'choices' => array('A', 'B'),
  72. )));
  73. $expected->addGetterConstraint('lastName', new NotNull());
  74. $this->assertEquals($expected, $metadata);
  75. }
  76. }