YamlFileLoaderTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\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->setGroupSequence(array('Foo', 'Entity'));
  59. $expected->addConstraint(new ConstraintA());
  60. $expected->addConstraint(new ConstraintB());
  61. $expected->addPropertyConstraint('firstName', new NotNull());
  62. $expected->addPropertyConstraint('firstName', new Min(3));
  63. $expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
  64. $expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Min(3))));
  65. $expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Min(3)))));
  66. $expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
  67. 'foo' => array(new NotNull(), new Min(3)),
  68. 'bar' => array(new Min(5)),
  69. ))));
  70. $expected->addPropertyConstraint('firstName', new Choice(array(
  71. 'message' => 'Must be one of %choices%',
  72. 'choices' => array('A', 'B'),
  73. )));
  74. $expected->addGetterConstraint('lastName', new NotNull());
  75. $this->assertEquals($expected, $metadata);
  76. }
  77. }