Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit defb99d

Browse files
committed
Run high-deps tests with ORM 3 and DBAL 4
1 parent 7aaa92a commit defb99d

File tree

17 files changed

+183
-95
lines changed

17 files changed

+183
-95
lines changed

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
use Doctrine\Common\Collections\Collection;
1515
use Doctrine\DBAL\Types\Types;
1616
use Doctrine\ORM\EntityManagerInterface;
17+
use Doctrine\ORM\Mapping\AssociationMapping;
1718
use Doctrine\ORM\Mapping\ClassMetadata;
18-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19-
use Doctrine\ORM\Mapping\Embedded;
2019
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
2120
use Doctrine\Persistence\Mapping\MappingException;
2221
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
@@ -49,7 +48,7 @@ public function getProperties(string $class, array $context = [])
4948

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

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

7574
if ($metadata->isSingleValuedAssociation($property)) {
76-
if ($metadata instanceof ClassMetadataInfo) {
75+
if ($metadata instanceof ClassMetadata) {
7776
$associationMapping = $metadata->getAssociationMapping($property);
7877

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

8786
$collectionKeyType = Type::BUILTIN_TYPE_INT;
8887

89-
if ($metadata instanceof ClassMetadataInfo) {
88+
if ($metadata instanceof ClassMetadata) {
9089
$associationMapping = $metadata->getAssociationMapping($property);
9190

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

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

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

@@ -130,7 +127,7 @@ public function getTypes(string $class, string $property, array $context = [])
130127
)];
131128
}
132129

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

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

144-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
141+
$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
145142
$enumType = null;
146143
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
147144
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
@@ -233,9 +230,11 @@ private function getMetadata(string $class): ?ClassMetadata
233230
/**
234231
* Determines whether an association is nullable.
235232
*
233+
* @param array<string, mixed>|AssociationMapping $associationMapping
234+
*
236235
* @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
237236
*/
238-
private function isAssociationNullable(array $associationMapping): bool
237+
private function isAssociationNullable($associationMapping): bool
239238
{
240239
if (isset($associationMapping['id']) && $associationMapping['id']) {
241240
return false;

src/Symfony/Bridge/Doctrine/Test/TestRepositoryFactory.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,51 @@
1212
namespace Symfony\Bridge\Doctrine\Test;
1313

1414
use Doctrine\ORM\EntityManagerInterface;
15+
use Doctrine\ORM\EntityRepository;
1516
use Doctrine\ORM\Mapping\ClassMetadata;
1617
use Doctrine\ORM\Repository\RepositoryFactory;
1718
use Doctrine\Persistence\ObjectRepository;
1819

20+
if ((new \ReflectionMethod(RepositoryFactory::class, 'getRepository'))->hasReturnType()) {
21+
/** @internal */
22+
trait GetRepositoryTrait
23+
{
24+
public function getRepository(EntityManagerInterface $entityManager, string $entityName): EntityRepository
25+
{
26+
return $this->doGetRepository($entityManager, $entityName);
27+
}
28+
}
29+
} else {
30+
/** @internal */
31+
trait GetRepositoryTrait
32+
{
33+
/**
34+
* {@inheritdoc}
35+
*
36+
* @return ObjectRepository
37+
*/
38+
public function getRepository(EntityManagerInterface $entityManager, $entityName)
39+
{
40+
return $this->doGetRepository($entityManager, $entityName);
41+
}
42+
}
43+
}
44+
1945
/**
2046
* @author Andreas Braun <[email protected]>
2147
*
2248
* @deprecated since Symfony 5.3
2349
*/
2450
class TestRepositoryFactory implements RepositoryFactory
2551
{
52+
use GetRepositoryTrait;
53+
2654
/**
2755
* @var ObjectRepository[]
2856
*/
2957
private $repositoryList = [];
3058

31-
/**
32-
* {@inheritdoc}
33-
*
34-
* @return ObjectRepository
35-
*/
36-
public function getRepository(EntityManagerInterface $entityManager, $entityName)
59+
private function doGetRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository
3760
{
3861
if (__CLASS__ === static::class) {
3962
trigger_deprecation('symfony/doctrine-bridge', '5.3', '"%s" is deprecated and will be removed in 6.0.', __CLASS__);

src/Symfony/Bridge/Doctrine/Tests/Form/DoctrineOrmTypeGuesserTest.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Doctrine\DBAL\Types\Types;
1515
use Doctrine\ORM\Mapping\ClassMetadata;
16+
use Doctrine\ORM\Mapping\JoinColumnMapping;
17+
use Doctrine\ORM\Mapping\ManyToOneAssociationMapping;
1618
use Doctrine\Persistence\ManagerRegistry;
1719
use Doctrine\Persistence\ObjectManager;
1820
use PHPUnit\Framework\TestCase;
@@ -69,33 +71,49 @@ public function testRequiredGuesserSimpleFieldNullable()
6971

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

75-
$mapping = ['joinColumns' => [[]]];
76-
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);
76+
if (class_exists(ManyToOneAssociationMapping::class)) {
77+
$associationMapping = new ManyToOneAssociationMapping('field', 'Acme\Entity\Foo', 'Acme\Entity\Bar');
78+
$associationMapping->joinColumns[] = new JoinColumnMapping('field', 'field');
79+
} else {
80+
$associationMapping = ['joinColumns' => [[]]];
81+
}
82+
$classMetadata->associationMappings['field'] = $associationMapping;
7783

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

8187
public function testRequiredGuesserOneToOneExplicitNullable()
8288
{
83-
$classMetadata = $this->createMock(ClassMetadata::class);
84-
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);
85-
86-
$mapping = ['joinColumns' => [['nullable' => true]]];
87-
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);
89+
$classMetadata = new ClassMetadata('Acme\Entity\Foo');
90+
91+
if (class_exists(ManyToOneAssociationMapping::class)) {
92+
$associationMapping = new ManyToOneAssociationMapping('field', 'Acme\Entity\Foo', 'Acme\Entity\Bar');
93+
$joinColumnMapping = new JoinColumnMapping('field', 'field');
94+
$joinColumnMapping->nullable = true;
95+
$associationMapping->joinColumns[] = $joinColumnMapping;
96+
} else {
97+
$associationMapping = ['joinColumns' => [['nullable' => true]]];
98+
}
99+
$classMetadata->associationMappings['field'] = $associationMapping;
88100

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

92104
public function testRequiredGuesserOneToOneNotNullable()
93105
{
94-
$classMetadata = $this->createMock(ClassMetadata::class);
95-
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->willReturn(true);
96-
97-
$mapping = ['joinColumns' => [['nullable' => false]]];
98-
$classMetadata->expects($this->once())->method('getAssociationMapping')->with('field')->willReturn($mapping);
106+
$classMetadata = new ClassMetadata('Acme\Entity\Foo');
107+
108+
if (class_exists(ManyToOneAssociationMapping::class)) {
109+
$associationMapping = new ManyToOneAssociationMapping('field', 'Acme\Entity\Foo', 'Acme\Entity\Bar');
110+
$joinColumnMapping = new JoinColumnMapping('field', 'field');
111+
$joinColumnMapping->nullable = false;
112+
$associationMapping->joinColumns[] = $joinColumnMapping;
113+
} else {
114+
$associationMapping = ['joinColumns' => [['nullable' => false]]];
115+
}
116+
$classMetadata->associationMappings['field'] = $associationMapping;
99117

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

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
use PHPUnit\Framework\TestCase;
2727
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
2828
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy;
29+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable;
2930
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEnum;
3031
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
3132
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
33+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded;
3234
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt;
3335
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString;
3436
use Symfony\Component\PropertyInfo\Type;
@@ -38,7 +40,7 @@
3840
*/
3941
class DoctrineExtractorTest extends TestCase
4042
{
41-
private function createExtractor()
43+
private function createExtractor(): DoctrineExtractor
4244
{
4345
$config = class_exists(ORMSetup::class)
4446
? ORMSetup::createConfiguration(true)
@@ -109,10 +111,6 @@ public function testGetProperties()
109111

110112
public function testTestGetPropertiesWithEmbedded()
111113
{
112-
if (!class_exists(\Doctrine\ORM\Mapping\Embedded::class)) {
113-
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
114-
}
115-
116114
$this->assertEquals(
117115
[
118116
'id',
@@ -125,25 +123,21 @@ public function testTestGetPropertiesWithEmbedded()
125123
/**
126124
* @dataProvider typesProvider
127125
*/
128-
public function testExtract($property, array $type = null)
126+
public function testExtract(string $property, array $type = null)
129127
{
130128
$this->assertEquals($type, $this->createExtractor()->getTypes(DoctrineDummy::class, $property, []));
131129
}
132130

133131
public function testExtractWithEmbedded()
134132
{
135-
if (!class_exists(\Doctrine\ORM\Mapping\Embedded::class)) {
136-
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
137-
}
138-
139133
$expectedTypes = [new Type(
140134
Type::BUILTIN_TYPE_OBJECT,
141135
false,
142-
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
136+
DoctrineEmbeddable::class
143137
)];
144138

145139
$actualTypes = $this->createExtractor()->getTypes(
146-
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
140+
DoctrineWithEmbedded::class,
147141
'embedded',
148142
[]
149143
);
@@ -166,9 +160,9 @@ public function testExtractEnum()
166160
$this->assertNull($this->createExtractor()->getTypes(DoctrineEnum::class, 'enumCustom', []));
167161
}
168162

169-
public static function typesProvider()
163+
public static function typesProvider(): array
170164
{
171-
$provider = [
165+
return [
172166
['id', [new Type(Type::BUILTIN_TYPE_INT)]],
173167
['guid', [new Type(Type::BUILTIN_TYPE_STRING)]],
174168
['bigint', [new Type(Type::BUILTIN_TYPE_STRING)]],
@@ -251,8 +245,6 @@ public static function typesProvider()
251245
)]],
252246
['json', null],
253247
];
254-
255-
return $provider;
256248
}
257249

258250
public function testGetPropertiesCatchException()

src/Symfony/Bridge/Doctrine/Tests/Security/User/EntityUserProviderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Tests\Security\User;
1313

1414
use Doctrine\ORM\EntityManager;
15+
use Doctrine\ORM\EntityRepository;
1516
use Doctrine\ORM\Tools\SchemaTool;
1617
use Doctrine\Persistence\ManagerRegistry;
1718
use Doctrine\Persistence\ObjectManager;
@@ -251,12 +252,12 @@ private function createSchema($em)
251252
}
252253
}
253254

254-
abstract class UserLoaderRepository implements ObjectRepository, UserLoaderInterface
255+
abstract class UserLoaderRepository extends EntityRepository implements UserLoaderInterface
255256
{
256257
abstract public function loadUserByIdentifier(string $identifier): ?UserInterface;
257258
}
258259

259-
abstract class PasswordUpgraderRepository implements ObjectRepository, PasswordUpgraderInterface
260+
abstract class PasswordUpgraderRepository extends EntityRepository implements PasswordUpgraderInterface
260261
{
261262
abstract public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void;
262263
}

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Collections\ArrayCollection;
1515
use Doctrine\DBAL\Types\Type;
1616
use Doctrine\ORM\EntityRepository;
17+
use Doctrine\ORM\Mapping\ClassMetadata;
1718
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1819
use Doctrine\ORM\Tools\SchemaTool;
1920
use Doctrine\Persistence\ManagerRegistry;
@@ -114,7 +115,9 @@ protected function createEntityManagerMock($repositoryMock)
114115
->willReturn($repositoryMock)
115116
;
116117

117-
$classMetadata = $this->createMock(ClassMetadataInfo::class);
118+
$classMetadata = $this->createMock(
119+
class_exists(ClassMetadataInfo::class) ? ClassMetadataInfo::class : ClassMetadata::class
120+
);
118121
$classMetadata
119122
->expects($this->any())
120123
->method('hasField')

0 commit comments

Comments
 (0)