8000 [Cache][Doctrine][DoctrineBridge][Lock][Messenger] Compatibility with ORM 3 and DBAL 4 by derrabus · Pull Request #51947 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache][Doctrine][DoctrineBridge][Lock][Messenger] Compatibility with ORM 3 and DBAL 4 #51947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\Embedded;
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
use Doctrine\Persistence\Mapping\MappingException;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
Expand Down Expand Up @@ -49,7 +48,7 @@ public function getProperties(string $class, array $context = [])

$properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());

if ($metadata instanceof ClassMetadataInfo && class_exists(Embedded::class) && $metadata->embeddedClasses) {
if ($metadata instanceof ClassMetadata && $metadata->embeddedClasses) {
$properties = array_filter($properties, function ($property) {
return !str_contains($property, '.');
});
Expand All @@ -73,7 +72,7 @@ public function getTypes(string $class, string $property, array $context = [])
$class = $metadata->getAssociationTargetClass($property);

if ($metadata->isSingleValuedAssociation($property)) {
if ($metadata instanceof ClassMetadataInfo) {
if ($metadata instanceof ClassMetadata) {
$associationMapping = $metadata->getAssociationMapping($property);

$nullable = $this->isAssociationNullable($associationMapping);
Expand All @@ -86,11 +85,10 @@ public function getTypes(string $class, string $property, array $context = [])

$collectionKeyType = Type::BUILTIN_TYPE_INT;

if ($metadata instanceof ClassMetadataInfo) {
if ($metadata instanceof ClassMetadata) {
$associationMapping = $metadata->getAssociationMapping($property);

if (isset($associationMapping['indexBy'])) {
/** @var ClassMetadataInfo $subMetadata */
$subMetadata = $this->entityManager->getClassMetadata($associationMapping['targetEntity']);

// Check if indexBy value is a property
Expand All @@ -102,7 +100,6 @@ public function getTypes(string $class, string $property, array $context = [])
// Maybe the column name is the association join column?
$associationMapping = $subMetadata->getAssociationMapping($fieldName);

/** @var ClassMetadataInfo $subMetadata */
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
$subMetadata = $this->entityManager->getClassMetadata($associationMapping['targetEntity']);

Expand Down Expand Up @@ -130,7 +127,7 @@ public function getTypes(string $class, string $property, array $context = [])
)];
}

if ($metadata instanceof ClassMetadataInfo && class_exists(Embedded::class) && isset($metadata->embeddedClasses[$property])) {
if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class'])];
}

Expand All @@ -141,7 +138,7 @@ public function getTypes(string $class, string $property, array $context = [])
return null;
}

$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
$enumType = null;
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
Expand Down Expand Up @@ -233,9 +230,11 @@ private function getMetadata(string $class): ?ClassMetadata
/**
* Determines whether an association is nullable.
*
* @param array<string, mixed>|AssociationMapping $associationMapping
*
* @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
*/
private function isAssociationNullable(array $associationMapping): bool
private function isAssociationNullable($associationMapping): bool
{
if (isset($associationMapping['id']) && $associationMapping['id']) {
return false;
Expand Down
35 changes: 29 additions & 6 deletions src/Symfony/Bridge/Doctrine/Test/TestRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,51 @@
namespace Symfony\Bridge\Doctrine\Test;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\Persistence\ObjectRepository;

if ((new \ReflectionMethod(RepositoryFactory::class, 'getRepository'))->hasReturnType()) {
/** @internal */
trait GetRepositoryTrait
{
public function getRepository(EntityManagerInterface $entityManager, string $entityName): EntityRepository
{
return $this->doGetRepository($entityManager, $entityName);
}
}
} else {
/** @internal */
trait GetRepositoryTrait
{
/**
* {@inheritdoc}
*
* @return ObjectRepository
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
return $this->doGetRepository($entityManager, $entityName);
}
}
}

/**
* @author Andreas Braun <alcaeus@alcaeus.org>
*
* @deprecated since Symfony 5.3
*/
class TestRepositoryFactory implements RepositoryFactory
{
use GetRepositoryTrait;

/**
* @var ObjectRepository[]
*/
private $repositoryList = [];

/**
* {@inheritdoc}
*
* @return ObjectRepository
*/
public function getRepository(EntityManagerInterface $entityManager, $entityName)
private function doGetRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository
{
if (__CLASS__ === static::class) {
trigger_deprecation('symfony/doctrine-bridge', '5.3', '"%s" is deprecated and will be removed in 6.0.', __CLASS__);
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/LegacyQueryMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Result;
use Doctrine\ORM\AbstractQuery;

class LegacyQueryMock extends AbstractQuery
{
public function __construct()
{
}

/**
* @return array|string
*/
public function getSQL()
{
}

/**
* @return Result|int
*/
protected function _doExecute()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Version;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\EmbeddedIdentifierEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\LegacyQueryMock;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Form\Exception\TransformationFailedException;
Expand All @@ -37,21 +42,19 @@ protected function tearDown(): void

public function testIdentifierTypeIsStringArray()
{
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity', class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY);
$this->checkIdentifierType(SingleStringIdEntity::class, class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY);
}

public function testIdentifierTypeIsIntegerArray()
{
$this->checkIdentifierType('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity', class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY);
$this->checkIdentifierType(SingleIntIdEntity::class, class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY);
}

protected function checkIdentifierType($classname, $expectedType)
protected function checkIdentifierType(string $classname, $expectedType)
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();
$query = $this->getQueryMock();

$query
->method('getResult')
Expand All @@ -62,7 +65,7 @@ protected function checkIdentifierType($classname, $expectedType)
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', [1, 2], $expectedType)
->willReturn($query);

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
$qb = $this->getMockBuilder(QueryBuilder::class)
->setConstructorArgs([$em])
->onlyMethods(['getQuery'])
->getMock();
Expand All @@ -82,9 +85,7 @@ public function testFilterNonIntegerValues()
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();
$query = $this->getQueryMock();

$query
->method('getResult')
Expand All @@ -95,7 +96,7 @@ public function testFilterNonIntegerValues()
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', [1, 2, 3, '9223372036854775808'], class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY)
->willReturn($query);

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
$qb = $this->getMockBuilder(QueryBuilder::class)
->setConstructorArgs([$em])
->onlyMethods(['getQuery'])
->getMock();
Expand All @@ -118,9 +119,7 @@ public function testFilterEmptyUuids($entityClass)
{
$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();
$query = $this->getQueryMock();

$query
->method('getResult')
Expand All @@ -131,7 +130,7 @@ public function testFilterEmptyUuids($entityClass)
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', ['71c5fd46-3f16-4abb-bad7-90ac1e654a2d', 'b98e8e11-2897-44df-ad24-d2627eb7f499'], class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY)
->willReturn($query);

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
$qb = $this->getMockBuilder(QueryBuilder::class)
->setConstructorArgs([$em])
->onlyMethods(['getQuery'])
->getMock();
Expand Down Expand Up @@ -163,9 +162,7 @@ public function testFilterUid($entityClass)

$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();
$query = $this->getQueryMock();

$query
->method('getResult')
Expand All @@ -176,7 +173,7 @@ public function testFilterUid($entityClass)
->with('ORMQueryBuilderLoader_getEntitiesByIds_id', [Uuid::fromString('71c5fd46-3f16-4abb-bad7-90ac1e654a2d')->toBinary(), Uuid::fromString('b98e8e11-2897-44df-ad24-d2627eb7f499')->toBinary()], class_exists(ArrayParameterType::class) ? ArrayParameterType::STRING : Connection::PARAM_STR_ARRAY)
->willReturn($query);

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
$qb = $this->getMockBuilder(QueryBuilder::class)
->setConstructorArgs([$em])
->onlyMethods(['getQuery'])
->getMock();
Expand Down Expand Up @@ -208,7 +205,7 @@ public function testUidThrowProperException($entityClass)

$em = DoctrineTestHelper::createTestEntityManager();

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
$qb = $this->getMockBuilder(QueryBuilder::class)
->setConstructorArgs([$em])
->onlyMethods(['getQuery'])
->getMock();
Expand All @@ -229,17 +226,9 @@ public function testUidThrowProperException($entityClass)

public function testEmbeddedIdentifierName()
{
if (Version::compare('2.5.0') > 0) {
$this->markTestSkipped('Applicable only for Doctrine >= 2.5.0');

return;
}

$em = DoctrineTestHelper::createTestEntityManager();

$query = $this->getMockBuilder(QueryMock::class)
->onlyMethods(['setParameter', 'getResult', 'getSql', '_doExecute'])
->getMock();
$query = $this->getQueryMock();

$query
->method('getResult')
Expand All @@ -250,7 +239,7 @@ public function testEmbeddedIdentifierName()
->with('ORMQueryBuilderLoader_getEntitiesByIds_id_value', [1, 2, 3], class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY)
->willReturn($query);

$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
$qb = $this->getMockBuilder(QueryBuilder::class)
->setConstructorArgs([$em])
->onlyMethods(['getQuery'])
->getMock();
Expand All @@ -259,46 +248,35 @@ public function testEmbeddedIdentifierName()
->willReturn($query);

$qb->select('e')
->from('Symfony\Bridge\Doctrine\Tests\Fixtures\EmbeddedIdentifierEntity', 'e');
->from(EmbeddedIdentifierEntity::class, 'e');

$loader = new ORMQueryBuilderLoader($qb);
$loader->getEntitiesByIds('id.value', [1, '', 2, 3, 'foo']);
}

public static function provideGuidEntityClasses()
public static function provideGuidEntityClasses(): array
{
return [
['Symfony\Bridge\Doctrine\Tests\Fixtures\GuidIdEntity'],
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
];
}

public static function provideUidEntityClasses()
public static function provideUidEntityClasses(): array
{
return [
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
['Symfony\Bridge\Doctrine\Tests\Fixtures\UlidIdEntity'],
];
}
}

class QueryMock extends AbstractQuery
{
public function __construct()
{
}

/**
* @return array|string
* @return (LegacyQueryMock&MockObject)|(Query&MockObject)
*/
public function getSQL()
private function getQueryMock(): AbstractQuery
{
}
$class = ((new \ReflectionClass(Query::class))->isFinal()) ? LegacyQueryMock::class : Query::class;

/**
* @return Result|int
*/
protected function _doExecute()
{
return $this->createMock($class);
}
}
Loading
0