Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 8 additions & 1 deletion src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -177,7 +179,12 @@ 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];
if (method_exists(AttributeDriver::class, 'getReader')) {
$driverArgs[] = $reportFieldsWhereDeclared;
}

$driver = new Definition(AttributeDriver::class, $driverArgs);

return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Doctrine\Bundle\DoctrineBundle\Tests\TestCase;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
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 realpath;

class DoctrineOrmMappingsPassTest extends TestCase
{
Expand All @@ -31,4 +37,24 @@ public function testCreateAnnotationMappingDriverIsDeprecated(): void
['/path/to/entities'],
);
}

public function testAttributeDriverIsRegistered(): void
{
$driverNamespace = 'DoctrineBundle\Entity';
$container = $this->createXmlBundleTestContainer(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-used this method as the compiler pass requires almost everything to be configured, such as the default entity manager.

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);
}
}
7 changes: 6 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Loading