Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make:entity Use the namespace instead of the full class name for MappingDriver #1054

Merged

Conversation

michaelphillpotts
Copy link
Contributor

This fixes #1053

Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

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

Awesome! Could you rebase this PR please. We've fixed a number of broken tests since you've opened the PR. Thanks in advanced.

@michaelphillpotts
Copy link
Contributor Author

Rebased the PR

if (1 < \count($classNameComponents)) {
array_pop($classNameComponents);
}
$classNamespace = implode('\\', $classNameComponents);
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment about which situation this is addressing? A test would be ideal, though I don't know how easy that would be to add.

Thanks!

@jrushlow jrushlow added the Status: Needs Work Additional work is needed label Feb 23, 2022
@michaelphillpotts
Copy link
Contributor Author

Hi

I have added a comment to the new logic.

Regarding the tests I have had a try, but not sure of where they should be placed (I was using EntityRegeneratorTest), the correctness of them, and how useful they are?
The test data set mixed_config_similar_namespace fails on current branch and passes with this pull request.

The 2 functions I came up with are:

   /**
     * @dataProvider getMappingDriverTests
     */
    public function testMappingDriverSelection(string $driverClass, ?array $annotatedPrefixes, bool $doesUseMappingDriver): void
    {
        $compatUtil = $this->createMock(PhpCompatUtil::class);

        $registry = $this->createMock(ManagerRegistry::class);
        $registry->expects($this->any())
            ->method('getManagerForClass')
            ->willThrowException(new \ReflectionException());

        $helper = new DoctrineHelper('', $compatUtil, $registry, true, $annotatedPrefixes);

        $this->assertSame($doesUseMappingDriver, $helper->doesClassUseDriver('App\\Entity\\UserProfile', $driverClass),
            sprintf('DoctrineHelper confirms that App\\Entity\\UserProfile %s use the %s driver (based on provided config)', $doesUseMappingDriver ? 'does' : 'does not', $driverClass)
        );

    }

    public function getMappingDriverTests(): \Generator
    {
        yield 'empty_config' => [
            'Doctrine\\ORM\\Mapping\\Driver\\AttributeDriver',
            null,
            false,
        ];

        $hasAttributeDriver = class_exists(\Doctrine\ORM\Mapping\Driver\AttributeDriver::class);
        $hasAnnotationDriver = class_exists(\Doctrine\ORM\Mapping\Driver\AnnotationDriver::class);

        if ($hasAttributeDriver) {
            $attributeDriver = $this->createMock(\Doctrine\ORM\Mapping\Driver\AttributeDriver::class);
            yield 'basic_config_attributes' => [
                \Doctrine\ORM\Mapping\Driver\AttributeDriver::class,
                [
                    'default' => [
                        [
                            'App\\Entity',
                            $attributeDriver
                        ]
                    ]
                ],
                true,
            ];
        }

        if ($hasAnnotationDriver) {
            $annotationDriver = $this->createMock(\Doctrine\ORM\Mapping\Driver\AnnotationDriver::class);
            yield 'basic_config_annotations' => [
                \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
                [
                    'default' => [
                        [
                            'App\\Entity',
                            $annotationDriver
                        ]
                    ]
                ],
                true,
            ];
        }

        if ($hasAttributeDriver && $hasAnnotationDriver) {
            $attributeDriver = $this->createMock(\Doctrine\ORM\Mapping\Driver\AttributeDriver::class);
            $annotationDriver = $this->createMock(\Doctrine\ORM\Mapping\Driver\AnnotationDriver::class);

            // 2 namespaces - namespaces start with different characters (A / B)
            // New class App\Entity\Profile should be using the AttributeDriver as it belongs to App\Entity namespace
            yield 'mixed_config_different_namespaces' => [
                \Doctrine\ORM\Mapping\Driver\AttributeDriver::class,
                [
                    'default' => [
                        [
                            'App\\Entity',
                            $attributeDriver
                        ],
                        [
                            'BetaCorp\Bundle\BetaBundle\Entity',
                            $annotationDriver
                        ]
                    ]
                ],
                true,
            ];

            // 2 namespaces - namespaces start with same characters (A / A)
            // New class App\Entity\Profile should be using the AttributeDriver as it belongs to App\Entity namespace
            yield 'mixed_config_similar_namespace' => [
                \Doctrine\ORM\Mapping\Driver\AttributeDriver::class,
                [
                    'default' => [
                        [
                            'App\\Entity',
                            $attributeDriver
                        ],
                        [
                            'ABC\Bundle\ABCBundle\Entity',
                            $annotationDriver
                        ]
                    ]
                ],
                true,
            ];
        }

    }

@jrushlow jrushlow added the Bug Bug Fix label Mar 3, 2022
@jrushlow jrushlow force-pushed the bugfix/mapping-driver-selection branch from ff6460a to 2bb6e95 Compare May 4, 2022 15:13
Copy link
Collaborator

@jrushlow jrushlow left a comment

Choose a reason for hiding this comment

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

Thank you @michaelphillpotts!

@jrushlow jrushlow added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Work Additional work is needed labels May 4, 2022
@jrushlow jrushlow merged commit 095599c into symfony:main May 4, 2022
@jrushlow jrushlow mentioned this pull request May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make:entity chooses the incorrect MappingDriver under certain conditions
3 participants