Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ private function detectMappingType(string $directory, ContainerBuilder $containe
}

if (
preg_match('/^(?: \*|\/\*\*) @.*' . $quotedMappingObjectName . '\b/m', $content)
|| preg_match('/^(?: \*|\/\*\*) @.*Embeddable\b/m', $content)
preg_match('/^(?: \*|\/\*\*) @[a-zA-Z\\\\]*' . $quotedMappingObjectName . '\b/m', $content)
Copy link
Member

Choose a reason for hiding this comment

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

a-zA-Z is not the range of characters allowed in PHP namespaces. Your current regex will reject some valid class names.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, my bad and thank for the feedback

|| preg_match('/^(?: \*|\/\*\*) @[a-zA-Z\\\\]*Embeddable\b/m', $content)
) {
$type = 'annotation';
break;
Expand Down
42 changes: 42 additions & 0 deletions tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,48 @@ public function testControllerResolver(bool $simpleEntityManagerConfig): void
$this->assertEquals(new MapEntity(null, null, null, [], null, null, null, true, true), $container->get('controller_resolver_defaults'));
}

#[TestWith(['AnnotationsBundle', 'attribute', 'Vendor'], 'Bundle without anything')]
#[TestWith(['AttributesBundle', 'attribute'], 'Bundle with attributes')]
#[TestWith(['RepositoryServiceBundle', 'attribute'], 'Bundle with both')]
#[TestWith(['AnnotationsBundle', 'annotation'], 'Bundle with annotations')]
#[TestWith(['AttributesWithPackageBundle', 'attribute'], 'Bundle with attributes and @package')]
public function testDetectMappingType(string $bundle, string $expectedType, string $vendor = '')
{
if (! interface_exists(EntityManagerInterface::class)) {
self::markTestSkipped('This test requires ORM');
}

$container = $this->getContainer([$bundle], $vendor);
$extension = new DoctrineExtension();

$config = BundleConfigurationBuilder::createBuilder()
->addBaseConnection()
->addEntityManager([
'default_entity_manager' => 'default',
'entity_managers' => [
'default' => [
'mappings' => [
$bundle => [],
],
],
],
])
->build();

if (! class_exists(AnnotationDriver::class) && $expectedType === 'annotation') {
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The annotation driver is only available in doctrine/orm v2.');
}

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

$calls = $container->getDefinition('doctrine.orm.default_metadata_driver')->getMethodCalls();
$this->assertEquals(
sprintf('doctrine.orm.default_%s_metadata_driver', $expectedType),
(string) $calls[0][1][0],
);
}

/** @param list<string> $bundles */
private static function getContainer(array $bundles = ['XmlBundle'], string $vendor = ''): ContainerBuilder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Fixtures\Bundles\AnnotationsBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AnnotationsBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Fixtures\Bundles\AnnotationsBundle\Entity;

/** @ORM\Entity() */
class TestAnnotationEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
public int|null $id = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Fixtures\Bundles\AttributesWithPackageBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AttributesWithPackageBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Fixtures\Bundles\AttributesWithPackageBundle\Entity;

/** @testUnrelatedAnnotation Fixtures\Bundles\AttributesWithPackageBundle\Entity */
interface TestInterface
{
}