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

Fix bug in namespace check in MappingDriverChain #341

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions src/Persistence/Mapping/Driver/MappingDriverChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getDrivers()
public function loadMetadataForClass(string $className, ClassMetadata $metadata)
{
foreach ($this->drivers as $namespace => $driver) {
if (strpos($className, $namespace) === 0) {
if (strpos($className, $namespace . '\\') === 0) {
Copy link
Member

@malarzm malarzm Dec 17, 2023

Choose a reason for hiding this comment

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

The $namespace can already contain a \ at the end. It'd be better to ensure there's always a single \ in the addDriver method

$driver->loadMetadataForClass($className, $metadata);

return;
Expand Down Expand Up @@ -105,7 +105,7 @@ public function getAllClassNames()
}

foreach ($driverClasses[$oid] as $className) {
if (strpos($className, $namespace) !== 0) {
if (strpos($className, $namespace . '\\') !== 0) {
continue;
}

Expand All @@ -128,7 +128,7 @@ public function getAllClassNames()
public function isTransient(string $className)
{
foreach ($this->drivers as $namespace => $driver) {
if (strpos($className, $namespace) === 0) {
if (strpos($className, $namespace . '\\') === 0) {
return $driver->isTransient($className);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Persistence/Mapping/DriverChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testDelegateToMatchingNamespaceDriver(): void
->with(self::equalTo($className))
->willReturn(true);

$chain->addDriver($driver1, 'Doctrine\Tests\Models\Company');
$chain->addDriver($driver1, 'Doctrine\Tests\Persistence\Map');
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for this change?

$chain->addDriver($driver2, 'Doctrine\Tests\Persistence\Mapping');

$chain->loadMetadataForClass($className, $classMetadata);
Expand All @@ -63,7 +63,7 @@ public function testGatherAllClassNames(): void
$driver1 = $this->createMock(MappingDriver::class);
$driver1->expects(self::once())
->method('getAllClassNames')
->will(self::returnValue(['Doctrine\Tests\Models\Company\Foo']));
->will(self::returnValue(['Doctrine\Tests\Models\Company\Foo', 'Doctrine\Tests\Models\Company2\Foo']));
Copy link
Member

Choose a reason for hiding this comment

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

Same question, why did this test need an adjustment?


$driver2 = $this->createMock(MappingDriver::class);
$driver2->expects(self::once())
Expand Down