Skip to content

Commit

Permalink
Ensure classes using the #[Entity] attribute are not declared as serv…
Browse files Browse the repository at this point in the history
…ices
  • Loading branch information
GromNaN committed Feb 10, 2025
1 parent d9ea81d commit c33b49d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver as LegacyStaticPHPDriver;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Proxy\Autoloader;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
Expand Down Expand Up @@ -643,6 +644,9 @@ protected function ormLoad(array $config, ContainerBuilder $container)
'connection' => $attribute->connection,
]);
});
$container->registerAttributeForAutoconfiguration(Entity::class, static function (ChildDefinition $definition) {
$definition->setAbstract(true)->addTag('container.excluded', ['source' => sprintf('with #[%s] attribute', Entity::class)]);
});

/** @see DoctrineBundle::boot() */
$container->getDefinition($defaultEntityManagerDefinitionId)
Expand Down
28 changes: 28 additions & 0 deletions tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Fixtures\Bundles\AttributesBundle\Entity\TestCustomIdGeneratorEntity;

Check failure on line 37 in tests/DependencyInjection/DoctrineExtensionTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.3)

Type Fixtures\Bundles\AttributesBundle\Entity\TestCustomIdGeneratorEntity is not used in this file.
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -1165,6 +1167,32 @@ public static function cacheConfigurationProvider(): array
];
}

public function testEntityAttributeExcludesFromContainer()
{
if (! interface_exists(EntityManagerInterface::class)) {
self::markTestSkipped('This test requires ORM');
}

$container = $this->getContainer();
$extension = new DoctrineExtension();

$config = BundleConfigurationBuilder::createBuilder()
->addBaseConnection()
->addBaseEntityManager()
->build();

$extension->load([$config], $container);

$attributes = $container->getAutoconfiguredAttributes();
$this->assertInstanceOf(Closure::class, $attributes[Entity::class]);

$definition = new ChildDefinition('');
$attributes[Entity::class]($definition);

$this->assertSame([['source' => 'with #[Doctrine\ORM\Mapping\Entity] attribute']], $definition->getTag('container.excluded'));
$this->assertTrue($definition->isAbstract());
}

public function testAsEntityListenerAttribute()
{
if (! interface_exists(EntityManagerInterface::class)) {
Expand Down

0 comments on commit c33b49d

Please sign in to comment.