/tests/Symfony/Tests/Component/Validator/ValidatorTest.php

https://github.com/Faianca/symfony · PHP · 163 lines · 126 code · 26 blank · 11 comment · 0 complexity · 453fb1c4a8e9b735155dd3b9db3f43cc MD5 · raw file

  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;
  11. require_once __DIR__.'/Fixtures/Entity.php';
  12. require_once __DIR__.'/Fixtures/FailingConstraint.php';
  13. require_once __DIR__.'/Fixtures/FailingConstraintValidator.php';
  14. require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
  15. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  16. use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
  17. use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
  18. use Symfony\Component\Validator\Validator;
  19. use Symfony\Component\Validator\ConstraintViolation;
  20. use Symfony\Component\Validator\ConstraintViolationList;
  21. use Symfony\Component\Validator\ConstraintValidatorFactory;
  22. use Symfony\Component\Validator\Mapping\ClassMetadata;
  23. class ValidatorTest extends \PHPUnit_Framework_TestCase
  24. {
  25. protected $factory;
  26. protected $validator;
  27. protected function setUp()
  28. {
  29. $this->factory = new FakeClassMetadataFactory();
  30. $this->validator = new Validator($this->factory, new ConstraintValidatorFactory());
  31. }
  32. protected function tearDown()
  33. {
  34. $this->factory = null;
  35. $this->validator = null;
  36. }
  37. public function testValidate_defaultGroup()
  38. {
  39. $entity = new Entity();
  40. $metadata = new ClassMetadata(get_class($entity));
  41. $metadata->addPropertyConstraint('firstName', new FailingConstraint());
  42. $metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
  43. 'groups' => 'Custom',
  44. )));
  45. $this->factory->addClassMetadata($metadata);
  46. // Only the constraint of group "Default" failed
  47. $violations = new ConstraintViolationList();
  48. $violations->add(new ConstraintViolation(
  49. '',
  50. array(),
  51. $entity,
  52. 'firstName',
  53. ''
  54. ));
  55. $this->assertEquals($violations, $this->validator->validate($entity));
  56. }
  57. public function testValidate_oneGroup()
  58. {
  59. $entity = new Entity();
  60. $metadata = new ClassMetadata(get_class($entity));
  61. $metadata->addPropertyConstraint('firstName', new FailingConstraint());
  62. $metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
  63. 'groups' => 'Custom',
  64. )));
  65. $this->factory->addClassMetadata($metadata);
  66. // Only the constraint of group "Custom" failed
  67. $violations = new ConstraintViolationList();
  68. $violations->add(new ConstraintViolation(
  69. '',
  70. array(),
  71. $entity,
  72. 'lastName',
  73. ''
  74. ));
  75. $this->assertEquals($violations, $this->validator->validate($entity, 'Custom'));
  76. }
  77. public function testValidate_multipleGroups()
  78. {
  79. $entity = new Entity();
  80. $metadata = new ClassMetadata(get_class($entity));
  81. $metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
  82. 'groups' => 'First',
  83. )));
  84. $metadata->addPropertyConstraint('lastName', new FailingConstraint(array(
  85. 'groups' => 'Second',
  86. )));
  87. $this->factory->addClassMetadata($metadata);
  88. // The constraints of both groups failed
  89. $violations = new ConstraintViolationList();
  90. $violations->add(new ConstraintViolation(
  91. '',
  92. array(),
  93. $entity,
  94. 'firstName',
  95. ''
  96. ));
  97. $violations->add(new ConstraintViolation(
  98. '',
  99. array(),
  100. $entity,
  101. 'lastName',
  102. ''
  103. ));
  104. $result = $this->validator->validate($entity, array('First', 'Second'));
  105. $this->assertEquals($violations, $result);
  106. }
  107. public function testValidateProperty()
  108. {
  109. $entity = new Entity();
  110. $metadata = new ClassMetadata(get_class($entity));
  111. $metadata->addPropertyConstraint('firstName', new FailingConstraint());
  112. $this->factory->addClassMetadata($metadata);
  113. $result = $this->validator->validateProperty($entity, 'firstName');
  114. $this->assertEquals(1, count($result));
  115. }
  116. public function testValidatePropertyValue()
  117. {
  118. $entity = new Entity();
  119. $metadata = new ClassMetadata(get_class($entity));
  120. $metadata->addPropertyConstraint('firstName', new FailingConstraint());
  121. $this->factory->addClassMetadata($metadata);
  122. $result = $this->validator->validatePropertyValue(get_class($entity), 'firstName', 'Bernhard');
  123. $this->assertEquals(1, count($result));
  124. }
  125. public function testValidateValue()
  126. {
  127. $result = $this->validator->validateValue('Bernhard', new FailingConstraint());
  128. $this->assertEquals(1, count($result));
  129. }
  130. public function testGetMetadataFactory()
  131. {
  132. $this->assertInstanceOf(
  133. 'Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface',
  134. $this->validator->getMetadataFactory()
  135. );
  136. }
  137. }