Skip to content

[Doctrine][DoctrineBridge] Compatibility with ORM 3 and DBAL 4 #51997

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Run high-deps tests with ORM 3 and DBAL 4
  • Loading branch information
derrabus committed Oct 11, 2023
commit 2b77ae300e39687e3b1109b15490c5b2824175fb
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 @@ -46,7 +45,7 @@ public function getProperties(string $class, array $context = []): ?array

$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, fn ($property) => !str_contains($property, '.'));

$properties = array_merge($properties, array_keys($metadata->embeddedClasses));
Expand All @@ -65,7 +64,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 @@ -78,11 +77,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 @@ -94,7 +92,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 @@ -122,7 +119,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 @@ -133,7 +130,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 @@ -219,9 +216,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(array|AssociationMapping $associationMapping): bool
{
if (isset($associationMapping['id']) && $associationMapping['id']) {
return false;
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,13 +13,17 @@

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\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;
Expand All @@ -46,13 +50,11 @@ public function testIdentifierTypeIsIntegerArray()
$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 @@ -63,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 @@ -83,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 @@ -96,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 @@ -119,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 @@ -132,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 @@ -164,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 @@ -177,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 @@ -209,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 @@ -232,9 +228,7 @@ public function testEmbeddedIdentifierName()
{
$em = DoctrineTestHelper::createTestEntityManager();

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

$query
->method('getResult')
Expand All @@ -245,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 @@ -254,40 +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()
{
}

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

protected function _doExecute(): Result|int
{
return $this->createMock($class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\JoinColumnMapping;
use Doctrine\ORM\Mapping\ManyToOneAssociationMapping;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -69,33 +71,49 @@ public function testRequiredGuesserSimpleFieldNullable()

public function testRequiredGuesserOneToOneNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);
$classMetadata = new ClassMetadata('Acme\Entity\Foo');

$mapping = ['joinColumns' => [[]]];
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);
if (class_exists(ManyToOneAssociationMapping::class)) {
$associationMapping = new ManyToOneAssociationMapping('field', 'Acme\Entity\Foo', 'Acme\Entity\Bar');
$associationMapping->joinColumns[] = new JoinColumnMapping('field', 'field');
} else {
$associationMapping = ['joinColumns' => [[]]];
}
$classMetadata->associationMappings['field'] = $associationMapping;

$this->assertEquals(new ValueGuess(false, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

public function testRequiredGuesserOneToOneExplicitNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [['nullable' => true]]];
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);
$classMetadata = new ClassMetadata('Acme\Entity\Foo');

if (class_exists(ManyToOneAssociationMapping::class)) {
$associationMapping = new ManyToOneAssociationMapping('field', 'Acme\Entity\Foo', 'Acme\Entity\Bar');
$joinColumnMapping = new JoinColumnMapping('field', 'field');
$joinColumnMapping->nullable = true;
$associationMapping->joinColumns[] = $joinColumnMapping;
} else {
$associationMapping = ['joinColumns' => [['nullable' => true]]];
}
$classMetadata->associationMappings['field'] = $associationMapping;

$this->assertEquals(new ValueGuess(false, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}

public function testRequiredGuesserOneToOneNotNullable()
{
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);

$mapping = ['joinColumns' => [['nullable' => false]]];
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);
$classMetadata = new ClassMetadata('Acme\Entity\Foo');

if (class_exists(ManyToOneAssociationMapping::class)) {
$associationMapping = new ManyToOneAssociationMapping('field', 'Acme\Entity\Foo', 'Acme\Entity\Bar');
$joinColumnMapping = new JoinColumnMapping('field', 'field');
$joinColumnMapping->nullable = false;
$associationMapping->joinColumns[] = $joinColumnMapping;
} else {
$associationMapping = ['joinColumns' => [['nullable' => false]]];
}
$classMetadata->associationMappings['field'] = $associationMapping;

$this->assertEquals(new ValueGuess(true, Guess::HIGH_CONFIDENCE), $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy