Skip to content

Commit d485469

Browse files
committed
add test for form
1 parent a660d3f commit d485469

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
use Doctrine\ORM\Mapping\Id;
18+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19+
20+
#[Entity]
21+
class UniqueFieldFormValidationEntity
22+
{
23+
public function __construct(
24+
#[Id, Column(type: 'integer')]
25+
protected ?int $id = null,
26+
27+
#[Column(type: 'string', nullable: true)]
28+
public ?string $name = null,
29+
30+
#[Column(type: 'string', nullable: true)]
31+
public ?string $lastname = null,
32+
) {
33+
}
34+
35+
public function __toString(): string
36+
{
37+
return (string) $this->name;
38+
}
39+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)