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

Skip to content

Commit c4a3066

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

File tree

19 files changed

+247
-145
lines changed

19 files changed

+247
-145
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__);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\DBAL\Result;
15+
use Doctrine\ORM\AbstractQuery;
16+
17+
class LegacyQueryMock extends AbstractQuery
18+
{
19+
public function __construct()
20+
{
21+
}
22+
23+
/**
24+
* @return array|string
25+
*/
26+
public function getSQL()
27+
{
28+
}
29+
30+
/**
31+
* @return Result|int
32+
*/
33+
protected function _doExecute()
34+
{
35+
}
36+
}

src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/ORMQueryBuilderLoaderTest.php

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@
1313

1414
use Doctrine\DBAL\ArrayParameterType;
1515
use Doctrine\DBAL\Connection;
16-
use Doctrine\DBAL\Result;
1716
use Doctrine\DBAL\Types\GuidType;
1817
use Doctrine\DBAL\Types\Type;
1918
use Doctrine\ORM\AbstractQuery;
20-
use Doctrine\ORM\Version;
19+
use Doctrine\ORM\Query;
20+
use Doctrine\ORM\QueryBuilder;
21+
use PHPUnit\Framework\MockObject\MockObject;
2122
use PHPUnit\Framework\TestCase;
2223
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
2324
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper;
25+
use Symfony\Bridge\Doctrine\Tests\Fixtures\EmbeddedIdentifierEntity;
26+
use Symfony\Bridge\Doctrine\Tests\Fixtures\LegacyQueryMock;
27+
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
28+
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity;
2429
use Symfony\Bridge\Doctrine\Types\UlidType;
2530
use Symfony\Bridge\Doctrine\Types\UuidType;
2631
use Symfony\Component\Form\Exception\TransformationFailedException;
@@ -37,21 +42,19 @@ protected function tearDown(): void
3742

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

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

48-
protected function checkIdentifierType($classname, $expectedType)
53+
protected function checkIdentifierType(string $classname, $expectedType)
4954
{
5055
$em = DoctrineTestHelper::createTestEntityManager();
5156

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

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

65-
$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
68+
$qb = $this->getMockBuilder(QueryBuilder::class)
6669
->setConstructorArgs([$em])
6770
->onlyMethods(['getQuery'])
6871
->getMock();
@@ -82,9 +85,7 @@ public function testFilterNonIntegerValues()
8285
{
8386
$em = DoctrineTestHelper::createTestEntityManager();
8487

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

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

98-
$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
99+
$qb = $this->getMockBuilder(QueryBuilder::class)
99100
->setConstructorArgs([$em])
100101
->onlyMethods(['getQuery'])
101102
->getMock();
@@ -118,9 +119,7 @@ public function testFilterEmptyUuids($entityClass)
118119
{
119120
$em = DoctrineTestHelper::createTestEntityManager();
120121

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

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

134-
$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
133+
$qb = $this->getMockBuilder(QueryBuilder::class)
135134
->setConstructorArgs([$em])
136135
->onlyMethods(['getQuery'])
137136
->getMock();
@@ -163,9 +162,7 @@ public function testFilterUid($entityClass)
163162

164163
$em = DoctrineTestHelper::createTestEntityManager();
165164

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

170167
$query
171168
->method('getResult')
@@ -176,7 +173,7 @@ public function testFilterUid($entityClass)
176173
->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)
177174
->willReturn($query);
178175

179-
$qb = $this->getMockBuilder(\Doctrine\ORM\QueryBuilder::class)
176+
$qb = $this->getMockBuilder(QueryBuilder::class)
180177
->setConstructorArgs([$em])
181178
->onlyMethods(['getQuery'])
182179
->getMock();
@@ -208,7 +205,7 @@ public function testUidThrowProperException($entityClass)
208205

209206
$em = DoctrineTestHelper::createTestEntityManager();
210207

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

230227
public function testEmbeddedIdentifierName()
231228
{
232-
if (Version::compare('2.5.0') > 0) {
233-
$this->markTestSkipped('Applicable only for Doctrine >= 2.5.0');
234-
235-
return;
236-
}
237-
238229
$em = DoctrineTestHelper::createTestEntityManager();
239230

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

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

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

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

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

268-
public static function provideGuidEntityClasses()
257+
public static function provideGuidEntityClasses(): array
269258
{
270259
return [
271260
['Symfony\Bridge\Doctrine\Tests\Fixtures\GuidIdEntity'],
272261
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
273262
];
274263
}
275264

276-
public static function provideUidEntityClasses()
265+
public static function provideUidEntityClasses(): array
277266
{
278267
return [
279268
['Symfony\Bridge\Doctrine\Tests\Fixtures\UuidIdEntity'],
280269
['Symfony\Bridge\Doctrine\Tests\Fixtures\UlidIdEntity'],
281270
];
282271
}
283-
}
284-
285-
class QueryMock extends AbstractQuery
286-
{
287-
public function __construct()
288-
{
289-
}
290272

291273
/**
292-
* @return array|string
274+
* @return (LegacyQueryMock&MockObject)|(Query&MockObject)
293275
*/
294-
public function getSQL()
276+
private function getQueryMock(): AbstractQuery
295277
{
296-
}
278+
$class = ((new \ReflectionClass(Query::class))->isFinal()) ? LegacyQueryMock::class : Query::class;
297279

298-
/**
299-
* @return Result|int
300-
*/
301-
protected function _doExecute()
302-
{
280+
return $this->createMock($class);
303281
}
304282
}

0 commit comments

Comments
 (0)