Skip to content

Commit 092e5f7

Browse files
committed
Fix detectMappingType if the entity contain a package attribute
1 parent 05ae10f commit 092e5f7

File tree

6 files changed

+96
-2
lines changed

6 files changed

+96
-2
lines changed

src/DependencyInjection/DoctrineExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ private function detectMappingType(string $directory, ContainerBuilder $containe
418418
}
419419

420420
if (
421-
preg_match('/^(?: \*|\/\*\*) @.*' . $quotedMappingObjectName . '\b/m', $content)
422-
|| preg_match('/^(?: \*|\/\*\*) @.*Embeddable\b/m', $content)
421+
preg_match('/^(?: \*|\/\*\*) @[a-zA-Z\\\\]*' . $quotedMappingObjectName . '\b/m', $content)
422+
|| preg_match('/^(?: \*|\/\*\*) @[a-zA-Z\\\\]*Embeddable\b/m', $content)
423423
) {
424424
$type = 'annotation';
425425
break;

tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,50 @@ public function testControllerResolver(bool $simpleEntityManagerConfig): void
15071507
$this->assertEquals(new MapEntity(null, null, null, [], null, null, null, true, true), $container->get('controller_resolver_defaults'));
15081508
}
15091509

1510+
#[TestWith(['AnnotationsBundle', 'attribute', 'Vendor'], 'Bundle without anything')]
1511+
#[TestWith(['AttributesBundle', 'attribute'], 'Bundle with attributes')]
1512+
#[TestWith(['RepositoryServiceBundle', 'attribute'], 'Bundle with both')]
1513+
#[TestWith(['AnnotationsBundle', 'annotation'], 'Bundle with annotations')]
1514+
#[TestWith(['AttributesWithPackageBundle', 'attribute'], 'Bundle with attributes and @package')]
1515+
public function testDetectMappingType(string $bundle, string $expectedType, string $vendor = "")
1516+
{
1517+
if (! interface_exists(EntityManagerInterface::class)) {
1518+
self::markTestSkipped('This test requires ORM');
1519+
}
1520+
1521+
$container = $this->getContainer([$bundle], $vendor);
1522+
$extension = new DoctrineExtension();
1523+
1524+
$config = BundleConfigurationBuilder::createBuilder()
1525+
->addBaseConnection()
1526+
->addEntityManager([
1527+
'default_entity_manager' => 'default',
1528+
'entity_managers' => [
1529+
'default' => [
1530+
'mappings' => [
1531+
$bundle => [],
1532+
],
1533+
],
1534+
],
1535+
])
1536+
->build();
1537+
1538+
if (!class_exists(AnnotationDriver::class) && $expectedType === "annotation") {
1539+
$this->expectException(LogicException::class);
1540+
$this->expectExceptionMessage('The annotation driver is only available in doctrine/orm v2.');
1541+
}
1542+
1543+
$extension->load([$config], $container);
1544+
1545+
1546+
$calls = $container->getDefinition('doctrine.orm.default_metadata_driver')->getMethodCalls();
1547+
$this->assertEquals(
1548+
sprintf('doctrine.orm.default_%s_metadata_driver', $expectedType),
1549+
(string) $calls[0][1][0],
1550+
);
1551+
}
1552+
1553+
15101554
/** @param list<string> $bundles */
15111555
private static function getContainer(array $bundles = ['XmlBundle'], string $vendor = ''): ContainerBuilder
15121556
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fixtures\Bundles\AnnotationsBundle;
6+
7+
use Symfony\Component\HttpKernel\Bundle\Bundle;
8+
9+
class AnnotationsBundle extends Bundle
10+
{
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fixtures\Bundles\AnnotationsBundle\Entity;
6+
7+
/** @ORM\Entity() */
8+
class TestAnnotationEntity
9+
{
10+
/**
11+
* @ORM\Id
12+
* @ORM\GeneratedValue(strategy="AUTO")
13+
* @ORM\Column(type="integer")
14+
*/
15+
public int|null $id = null;
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fixtures\Bundles\AttributesWithPackageBundle;
6+
7+
use Symfony\Component\HttpKernel\Bundle\Bundle;
8+
9+
class AttributesWithPackageBundle extends Bundle
10+
{
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fixtures\Bundles\AttributesWithPackageBundle\Entity;
6+
7+
/**
8+
* @package Fixtures\Bundles\AttributesWithPackageBundle\Entity
9+
*/
10+
interface TestInterface
11+
{
12+
}

0 commit comments

Comments
 (0)