|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Bridge\Doctrine\Tests\Form\Validation; |
| 4 | + |
| 5 | +use Doctrine\ORM\EntityManager; |
| 6 | +use Doctrine\ORM\Tools\SchemaTool; |
| 7 | +use Doctrine\Persistence\ManagerRegistry; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; |
| 10 | +use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper; |
| 11 | +use Symfony\Bridge\Doctrine\Tests\Fixtures\UniqueFieldFormValidationEntity; |
| 12 | +use Symfony\Bridge\Doctrine\Tests\Fixtures\UniqueGroupFieldsEntity; |
| 13 | +use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
| 14 | +use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; |
| 15 | +use Symfony\Component\Form\Extension\Validator\ValidatorExtension; |
| 16 | +use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; |
| 17 | +use Symfony\Component\Form\Forms; |
| 18 | +use Symfony\Component\Form\Tests\Extension\Core\Type\BaseTypeTestCase; |
| 19 | +use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest; |
| 20 | +use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest; |
| 21 | +use Symfony\Component\Validator\ConstraintValidatorFactory; |
| 22 | +use Symfony\Component\Validator\ConstraintViolation; |
| 23 | +use Symfony\Component\Validator\Validation; |
| 24 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 25 | + |
| 26 | +class UniqueFieldEntityFormValidationTest extends TypeTestCase |
| 27 | +{ |
| 28 | + private EntityManager $em; |
| 29 | + private MockObject&ManagerRegistry $emRegistry; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->em = DoctrineTestHelper::createTestEntityManager(); |
| 34 | + $this->emRegistry = $this->createRegistryMock('default', $this->em); |
| 35 | + |
| 36 | + parent::setUp(); |
| 37 | + } |
| 38 | + |
| 39 | + protected function createRegistryMock($name, $em): MockObject&ManagerRegistry |
| 40 | + { |
| 41 | + $registry = $this->createMock(ManagerRegistry::class); |
| 42 | + $registry->expects($this->any()) |
| 43 | + ->method('getManager') |
| 44 | + ->with($this->equalTo($name)) |
| 45 | + ->willReturn($em); |
| 46 | + |
| 47 | + return $registry; |
| 48 | + } |
| 49 | + protected function getExtensions(): array |
| 50 | + { |
| 51 | + $factory = new ConstraintValidatorFactory([ |
| 52 | + 'doctrine.orm.validator.unique' => new UniqueEntityValidator($this->emRegistry) |
| 53 | + ]); |
| 54 | + |
| 55 | + $validator = Validation::createValidatorBuilder() |
| 56 | + ->setConstraintValidatorFactory($factory) |
| 57 | + ->enableAttributeMapping() |
| 58 | + ->getValidator(); |
| 59 | + |
| 60 | + return [ |
| 61 | + new ValidatorExtension($validator), |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + public function testFormValidationForEntityWithUniqueFieldNotValid() |
| 66 | + { |
| 67 | + $entity1 = new UniqueFieldFormValidationEntity(1, 'Foo'); |
| 68 | + |
| 69 | + $form = $this->factory |
| 70 | + ->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, ['data_class' => UniqueFieldFormValidationEntity::class]) |
| 71 | + ->add('name', TextTypeTest::TESTED_TYPE) |
| 72 | + ->add('token', TextTypeTest::TESTED_TYPE) |
| 73 | + ->getForm(); |
| 74 | + |
| 75 | + $constraintViolation = new ConstraintViolation( |
| 76 | + 'This value should not be used.', |
| 77 | + 'This value should not be used.', |
| 78 | + [ |
| 79 | + '{{ value }}' => 'myNameValue', |
| 80 | + '{{ name value }}' => '"myNameValue"', |
| 81 | + ], |
| 82 | + $form, |
| 83 | + 'data.name', |
| 84 | + 'myNameValue', |
| 85 | + null, |
| 86 | + 'code', |
| 87 | + new UniqueEntity( |
| 88 | + ['name'] |
| 89 | + ), |
| 90 | + $entity1 |
| 91 | + ); |
| 92 | + |
| 93 | + $violationMapper = new ViolationMapper(); |
| 94 | + $violationMapper->mapViolation($constraintViolation, $form, true); |
| 95 | + |
| 96 | + $this->assertCount(0, $form->getErrors()); |
| 97 | + $this->assertCount(1, $form->get('name')->getErrors()); |
| 98 | + $this->assertSame('This value should not be used.', $form->get('name')->getErrors()[0]->getMessage()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testFormValidationForEntityWithUniqueGroupFieldsNotValid() |
| 102 | + { |
| 103 | + $entity1 = new UniqueFieldFormValidationEntity(1, 'Foo'); |
| 104 | + |
| 105 | + $form = $this->factory |
| 106 | + ->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, ['data_class' => UniqueFieldFormValidationEntity::class]) |
| 107 | + ->add('name', TextTypeTest::TESTED_TYPE) |
| 108 | + ->add('token', TextTypeTest::TESTED_TYPE) |
| 109 | + ->getForm(); |
| 110 | + |
| 111 | + $constraintViolation = new ConstraintViolation( |
| 112 | + 'This value should not be used.', |
| 113 | + 'This value should not be used.', |
| 114 | + [ |
| 115 | + '{{ value }}' => 'myTokenValue, myNameValue', |
| 116 | + '{{ token value }}' => '"myTokenValue"', |
| 117 | + '{{ name value }}' => '"myNameValue"', |
| 118 | + ], |
| 119 | + $form, |
| 120 | + 'data.name, token', |
| 121 | + 'myTokenValue, myNameValue', |
| 122 | + null, |
| 123 | + 'code', |
| 124 | + new UniqueEntity( |
| 125 | + ['name', 'token'] |
| 126 | + ), |
| 127 | + $entity1 |
| 128 | + ); |
| 129 | + |
| 130 | + $violationMapper = new ViolationMapper(); |
| 131 | + $violationMapper->mapViolation($constraintViolation, $form, true); |
| 132 | + |
| 133 | + $this->assertCount(1, $form->getErrors()); |
| 134 | + $this->assertCount(0, $form->get('name')->getErrors()); |
| 135 | + $this->assertSame('This value should not be used.', $form->getErrors()[0]->getMessage()); |
| 136 | + } |
| 137 | +} |
0 commit comments