Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 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,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')) {
Comment thread
bobvandevijver marked this conversation as resolved.
$driverArgs[] = $reportFieldsWhereDeclared;
}

$driver = new Definition(AttributeDriver::class, $driverArgs);
Comment thread
bobvandevijver marked this conversation as resolved.

return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/DependencyInjection/Compiler/DoctrineOrmMappingsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(
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')],
Comment thread
bobvandevijver marked this conversation as resolved.
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
Comment thread
GromNaN marked this conversation as resolved.
{
$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