Skip to content

Commit 30e0f73

Browse files
Fix attribute driver mapping pass compatibility with ORM 3 (#2120)
1 parent beae6d5 commit 30e0f73

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Reference;
1818

19+
use function method_exists;
20+
1921
/**
2022
* Class for Symfony bundles to configure mappings for model classes not in the
2123
* auto-mapped folder.
@@ -177,7 +179,14 @@ public static function createAnnotationMappingDriver(array $namespaces, array $d
177179
*/
178180
public static function createAttributeMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [], bool $reportFieldsWhereDeclared = false)
179181
{
180-
$driver = new Definition(AttributeDriver::class, [$directories, $reportFieldsWhereDeclared]);
182+
$driverArgs = [$directories];
183+
184+
// Add additional args for ORM <3.0
185+
if (method_exists(AttributeDriver::class, 'getReader')) {
186+
$driverArgs[] = $reportFieldsWhereDeclared;
187+
}
188+
189+
$driver = new Definition(AttributeDriver::class, $driverArgs);
181190

182191
return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
183192
}

tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
88
use Doctrine\Bundle\DoctrineBundle\Tests\TestCase;
99
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
10+
use Doctrine\ORM\EntityManagerInterface;
11+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
12+
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
1013
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
16+
use function assert;
17+
use function interface_exists;
18+
use function realpath;
1119

1220
class DoctrineOrmMappingsPassTest extends TestCase
1321
{
@@ -31,4 +39,28 @@ public function testCreateAnnotationMappingDriverIsDeprecated(): void
3139
['/path/to/entities'],
3240
);
3341
}
42+
43+
public function testAttributeDriverIsRegistered(): void
44+
{
45+
if (! interface_exists(EntityManagerInterface::class)) {
46+
self::markTestSkipped('This test requires ORM');
47+
}
48+
49+
$driverNamespace = 'DoctrineBundle\Entity';
50+
$container = $this->createXmlBundleTestContainer(
51+
static function (ContainerBuilder $containerBuilder) use ($driverNamespace): void {
52+
$containerBuilder->addCompilerPass(DoctrineOrmMappingsPass::createAttributeMappingDriver(
53+
[$driverNamespace],
54+
[realpath(__DIR__ . '/Entity')],
55+
reportFieldsWhereDeclared: true,
56+
));
57+
},
58+
);
59+
60+
$metadataDriver = $container->get('doctrine.orm.default_metadata_driver');
61+
assert($metadataDriver instanceof MappingDriverChain);
62+
63+
$driver = $metadataDriver->getDrivers()[$driverNamespace];
64+
$this->assertTrue($driver instanceof AttributeDriver);
65+
}
3466
}

tests/TestCase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class TestCase extends BaseTestCase
2424
{
25-
public function createXmlBundleTestContainer(): ContainerBuilder
25+
public function createXmlBundleTestContainer(callable|null $func = null): ContainerBuilder
2626
{
2727
$container = new ContainerBuilder(new ParameterBag([
2828
'kernel.debug' => false,
@@ -90,6 +90,11 @@ public function createXmlBundleTestContainer(): ContainerBuilder
9090
$compilerPassConfig->addPass(new CacheCompatibilityPass());
9191
// make all Doctrine services public, so we can fetch them in the test
9292
$compilerPassConfig->addPass(new TestCaseAllPublicCompilerPass());
93+
94+
if ($func !== null) {
95+
$func($container);
96+
}
97+
9398
$container->compile();
9499

95100
return $container;

0 commit comments

Comments
 (0)