forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/Symfony/Bridge/Doctrine/Tests/Fixtures/UniqueFieldFormValidationEntity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Fixtures; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
|
||
#[Entity] | ||
class UniqueFieldFormValidationEntity | ||
{ | ||
public function __construct( | ||
#[Id, Column(type: 'integer')] | ||
protected ?int $id = null, | ||
|
||
#[Column(type: 'string', nullable: true)] | ||
public ?string $name = null, | ||
|
||
#[Column(type: 'string', nullable: true)] | ||
public ?string $lastname = null, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return (string) $this->name; | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/Symfony/Bridge/Doctrine/Tests/Form/Validation/UniqueFieldEntityFormValidationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Form\Validation; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension; | ||
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper; | ||
use Symfony\Bridge\Doctrine\Tests\Fixtures\UniqueFieldFormValidationEntity; | ||
use Symfony\Bridge\Doctrine\Tests\Fixtures\UniqueGroupFieldsEntity; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; | ||
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; | ||
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; | ||
use Symfony\Component\Form\Forms; | ||
use Symfony\Component\Form\Tests\Extension\Core\Type\BaseTypeTestCase; | ||
use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest; | ||
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest; | ||
use Symfony\Component\Validator\ConstraintValidatorFactory; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\Validation; | ||
use Symfony\Component\Form\Test\TypeTestCase; | ||
|
||
class UniqueFieldEntityFormValidationTest extends TypeTestCase | ||
{ | ||
private EntityManager $em; | ||
private MockObject&ManagerRegistry $emRegistry; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->em = DoctrineTestHelper::createTestEntityManager(); | ||
$this->emRegistry = $this->createRegistryMock('default', $this->em); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
protected function createRegistryMock($name, $em): MockObject&ManagerRegistry | ||
{ | ||
$registry = $this->createMock(ManagerRegistry::class); | ||
$registry->expects($this->any()) | ||
->method('getManager') | ||
->with($this->equalTo($name)) | ||
->willReturn($em); | ||
|
||
return $registry; | ||
} | ||
protected function getExtensions(): array | ||
{ | ||
$factory = new ConstraintValidatorFactory([ | ||
'doctrine.orm.validator.unique' => new UniqueEntityValidator($this->emRegistry) | ||
]); | ||
|
||
$validator = Validation::createValidatorBuilder() | ||
->setConstraintValidatorFactory($factory) | ||
->enableAttributeMapping() | ||
->getValidator(); | ||
|
||
return [ | ||
new ValidatorExtension($validator), | ||
]; | ||
} | ||
|
||
public function testFormValidationForEntityWithUniqueFieldNotValid() | ||
{ | ||
$entity1 = new UniqueFieldFormValidationEntity(1, 'Foo'); | ||
|
||
$form = $this->factory | ||
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, ['data_class' => UniqueFieldFormValidationEntity::class]) | ||
->add('name', TextTypeTest::TESTED_TYPE) | ||
->add('token', TextTypeTest::TESTED_TYPE) | ||
->getForm(); | ||
|
||
$constraintViolation = new ConstraintViolation( | ||
'This value should not be used.', | ||
'This value should not be used.', | ||
[ | ||
'{{ value }}' => 'myNameValue', | ||
'{{ name value }}' => '"myNameValue"', | ||
], | ||
$form, | ||
'data.name', | ||
'myNameValue', | ||
null, | ||
'code', | ||
new UniqueEntity( | ||
['name'] | ||
), | ||
$entity1 | ||
); | ||
|
||
$violationMapper = new ViolationMapper(); | ||
$violationMapper->mapViolation($constraintViolation, $form, true); | ||
|
||
$this->assertCount(0, $form->getErrors()); | ||
$this->assertCount(1, $form->get('name')->getErrors()); | ||
$this->assertSame('This value should not be used.', $form->get('name')->getErrors()[0]->getMessage()); | ||
} | ||
|
||
public function testFormValidationForEntityWithUniqueGroupFieldsNotValid() | ||
{ | ||
$entity1 = new UniqueFieldFormValidationEntity(1, 'Foo'); | ||
|
||
$form = $this->factory | ||
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, ['data_class' => UniqueFieldFormValidationEntity::class]) | ||
->add('name', TextTypeTest::TESTED_TYPE) | ||
->add('token', TextTypeTest::TESTED_TYPE) | ||
->getForm(); | ||
|
||
$constraintViolation = new ConstraintViolation( | ||
'This value should not be used.', | ||
'This value should not be used.', | ||
[ | ||
'{{ value }}' => 'myTokenValue, myNameValue', | ||
'{{ token value }}' => '"myTokenValue"', | ||
'{{ name value }}' => '"myNameValue"', | ||
], | ||
$form, | ||
'data.name, token', | ||
'myTokenValue, myNameValue', | ||
null, | ||
'code', | ||
new UniqueEntity( | ||
['name', 'token'] | ||
), | ||
$entity1 | ||
); | ||
|
||
$violationMapper = new ViolationMapper(); | ||
$violationMapper->mapViolation($constraintViolation, $form, true); | ||
|
||
$this->assertCount(1, $form->getErrors()); | ||
$this->assertCount(0, $form->get('name')->getErrors()); | ||
$this->assertSame('This value should not be used.', $form->getErrors()[0]->getMessage()); | ||
} | ||
} |