[DoctrineBridge] Unique mapping setting is optional
This commit is contained in:
parent
936355e2a2
commit
fde4dc9922
@ -13,9 +13,12 @@ namespace Symfony\Bridge\Doctrine\Test;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Doctrine\Common\Cache\ArrayCache;
|
||||
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
|
||||
use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
|
||||
use Doctrine\ORM\Configuration;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
|
||||
use Doctrine\ORM\Mapping\Driver\XmlDriver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@ -67,6 +70,28 @@ class DoctrineTestHelper
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Configuration
|
||||
*/
|
||||
public static function createTestConfigurationWithXmlLoader()
|
||||
{
|
||||
$config = static::createTestConfiguration();
|
||||
|
||||
$driverChain = new MappingDriverChain();
|
||||
$driverChain->addDriver(
|
||||
new XmlDriver(
|
||||
new SymfonyFileLocator(
|
||||
[__DIR__.'/../Tests/Resources/orm' => 'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures'], '.orm.xml'
|
||||
)
|
||||
),
|
||||
'Symfony\\Bridge\\Doctrine\\Tests\\Fixtures'
|
||||
);
|
||||
|
||||
$config->setMetadataDriverImpl($driverChain);
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class cannot be instantiated.
|
||||
*/
|
||||
|
47
src/Symfony/Bridge/Doctrine/Tests/Fixtures/BaseUser.php
Normal file
47
src/Symfony/Bridge/Doctrine/Tests/Fixtures/BaseUser.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
|
||||
|
||||
/**
|
||||
* Class BaseUser.
|
||||
*/
|
||||
class BaseUser
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* BaseUser constructor.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $username
|
||||
*/
|
||||
public function __construct(int $id, string $username)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
|
||||
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
|
||||
|
||||
<mapped-superclass name="Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser">
|
||||
<field name="username" column="username" type="string" length="120" />
|
||||
</mapped-superclass>
|
||||
|
||||
</doctrine-mapping>
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" ?>
|
||||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
|
||||
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
|
||||
|
||||
<class name="Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser">
|
||||
<property name="username">
|
||||
<constraint name="NotBlank">
|
||||
<option name="groups">Registration</option>
|
||||
</constraint>
|
||||
<constraint name="Length">
|
||||
<option name="min">2</option>
|
||||
<option name="max">120</option>
|
||||
<option name="groups">Registration</option>
|
||||
</constraint>
|
||||
</property>
|
||||
</class>
|
||||
</constraint-mapping>
|
@ -13,6 +13,7 @@ namespace Symfony\Bridge\Doctrine\Tests\Validator;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
|
||||
@ -72,6 +73,30 @@ class DoctrineLoaderTest extends TestCase
|
||||
$this->assertSame(1, $alreadyMappedMaxLengthConstraints[0]->min);
|
||||
}
|
||||
|
||||
public function testFieldMappingsConfiguration()
|
||||
{
|
||||
if (!method_exists(ValidatorBuilder::class, 'addLoader')) {
|
||||
$this->markTestSkipped('Auto-mapping requires symfony/validation 4.2+');
|
||||
}
|
||||
|
||||
$validator = Validation::createValidatorBuilder()
|
||||
->enableAnnotationMapping()
|
||||
->addXmlMappings([__DIR__.'/../Resources/validator/BaseUser.xml'])
|
||||
->addLoader(
|
||||
new DoctrineLoader(
|
||||
DoctrineTestHelper::createTestEntityManager(
|
||||
DoctrineTestHelper::createTestConfigurationWithXmlLoader()
|
||||
), '{}'
|
||||
)
|
||||
)
|
||||
->getValidator();
|
||||
|
||||
$classMetadata = $validator->getMetadataFor(new BaseUser(1, 'DemoUser'));
|
||||
|
||||
$constraints = $classMetadata->getConstraints();
|
||||
$this->assertCount(0, $constraints);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider regexpProvider
|
||||
*/
|
||||
|
@ -68,7 +68,7 @@ final class DoctrineLoader implements LoaderInterface
|
||||
|
||||
// Type and nullable aren't handled here, use the PropertyInfo Loader instead.
|
||||
foreach ($doctrineMetadata->fieldMappings as $mapping) {
|
||||
if (true === $mapping['unique'] && !isset($existingUniqueFields[$mapping['fieldName']])) {
|
||||
if (true === ($mapping['unique'] ?? false) && !isset($existingUniqueFields[$mapping['fieldName']])) {
|
||||
$metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user