diff --git a/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php b/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php index 4ec6bd378..2ec6cb7ad 100644 --- a/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php +++ b/src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php @@ -16,6 +16,8 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; +use function method_exists; + /** * Class for Symfony bundles to configure mappings for model classes not in the * auto-mapped folder. @@ -177,7 +179,14 @@ public static function createAnnotationMappingDriver(array $namespaces, array $d */ public static function createAttributeMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [], bool $reportFieldsWhereDeclared = false) { - $driver = new Definition(AttributeDriver::class, [$directories, $reportFieldsWhereDeclared]); + $driverArgs = [$directories]; + + // Add additional args for ORM <3.0 + if (method_exists(AttributeDriver::class, 'getReader')) { + $driverArgs[] = $reportFieldsWhereDeclared; + } + + $driver = new Definition(AttributeDriver::class, $driverArgs); return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap); } diff --git a/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php b/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php index 1736cef2a..ae992a50b 100644 --- a/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php +++ b/tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php @@ -7,7 +7,15 @@ use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; use Doctrine\Bundle\DoctrineBundle\Tests\TestCase; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\Driver\AttributeDriver; +use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use PHPUnit\Framework\Attributes\IgnoreDeprecations; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +use function assert; +use function interface_exists; +use function realpath; class DoctrineOrmMappingsPassTest extends TestCase { @@ -31,4 +39,28 @@ public function testCreateAnnotationMappingDriverIsDeprecated(): void ['/path/to/entities'], ); } + + public function testAttributeDriverIsRegistered(): void + { + if (! interface_exists(EntityManagerInterface::class)) { + self::markTestSkipped('This test requires ORM'); + } + + $driverNamespace = 'DoctrineBundle\Entity'; + $container = $this->createXmlBundleTestContainer( + static function (ContainerBuilder $containerBuilder) use ($driverNamespace): void { + $containerBuilder->addCompilerPass(DoctrineOrmMappingsPass::createAttributeMappingDriver( + [$driverNamespace], + [realpath(__DIR__ . '/Entity')], + reportFieldsWhereDeclared: true, + )); + }, + ); + + $metadataDriver = $container->get('doctrine.orm.default_metadata_driver'); + assert($metadataDriver instanceof MappingDriverChain); + + $driver = $metadataDriver->getDrivers()[$driverNamespace]; + $this->assertTrue($driver instanceof AttributeDriver); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 137460c26..962e05730 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,7 +22,7 @@ class TestCase extends BaseTestCase { - public function createXmlBundleTestContainer(): ContainerBuilder + public function createXmlBundleTestContainer(callable|null $func = null): ContainerBuilder { $container = new ContainerBuilder(new ParameterBag([ 'kernel.debug' => false, @@ -90,6 +90,11 @@ public function createXmlBundleTestContainer(): ContainerBuilder $compilerPassConfig->addPass(new CacheCompatibilityPass()); // make all Doctrine services public, so we can fetch them in the test $compilerPassConfig->addPass(new TestCaseAllPublicCompilerPass()); + + if ($func !== null) { + $func($container); + } + $container->compile(); return $container;