diff --git a/lib/Doctrine/Common/Persistence/Mapping/Driver/SymfonyFileLocator.php b/lib/Doctrine/Common/Persistence/Mapping/Driver/SymfonyFileLocator.php index 49a058422..4588cfec8 100644 --- a/lib/Doctrine/Common/Persistence/Mapping/Driver/SymfonyFileLocator.php +++ b/lib/Doctrine/Common/Persistence/Mapping/Driver/SymfonyFileLocator.php @@ -153,7 +153,9 @@ public function fileExists($className) } $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator).$this->fileExtension; - return is_file($filename); + if (is_file($filename)) { + return true; + } } return false; @@ -230,8 +232,6 @@ public function findMappingFile($className) if (is_file($filename)) { return $filename; } - - throw MappingException::mappingFileNotFound($className, $filename); } throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension); diff --git a/tests/Doctrine/Tests/Common/Persistence/Mapping/SymfonyFileLocatorTest.php b/tests/Doctrine/Tests/Common/Persistence/Mapping/SymfonyFileLocatorTest.php index 48e3835c6..8b5d406f6 100644 --- a/tests/Doctrine/Tests/Common/Persistence/Mapping/SymfonyFileLocatorTest.php +++ b/tests/Doctrine/Tests/Common/Persistence/Mapping/SymfonyFileLocatorTest.php @@ -174,9 +174,37 @@ public function testFindMappingFileNotFound() $locator = new SymfonyFileLocator([$path => $prefix], ".yml"); $this->setExpectedException( - MappingException::class, - "No mapping file found named '".__DIR__."/_files/stdClass2.yml' for class 'Foo\stdClass2'." + "Doctrine\Common\Persistence\Mapping\MappingException", + "No mapping file found named 'stdClass2.yml' for class 'Foo\stdClass2'." ); $locator->findMappingFile("Foo\\stdClass2"); } + + public function testFindMappingFileLeastSpecificNamespaceFirst() + { + // Low -> High + $prefixes = array(); + $prefixes[__DIR__ . "/_match_ns"] = "Foo"; + $prefixes[__DIR__ . "/_match_ns/Bar"] = "Foo\\Bar"; + + $locator = new SymfonyFileLocator($prefixes, ".yml"); + + $this->assertEquals( + __DIR__ . "/_match_ns/Bar/barEntity.yml", + $locator->findMappingFile("Foo\\Bar\\barEntity") + ); + } + + public function testFindMappingFileMostSpecificNamespaceFirst() { + $prefixes = array(); + $prefixes[__DIR__ . "/_match_ns/Bar"] = "Foo\\Bar"; + $prefixes[__DIR__ . "/_match_ns"] = "Foo"; + + $locator = new SymfonyFileLocator($prefixes, ".yml"); + + $this->assertEquals( + __DIR__ . "/_match_ns/Bar/barEntity.yml", + $locator->findMappingFile("Foo\\Bar\\barEntity") + ); + } } diff --git a/tests/Doctrine/Tests/Common/Persistence/Mapping/_match_ns/Bar/barEntity.yml b/tests/Doctrine/Tests/Common/Persistence/Mapping/_match_ns/Bar/barEntity.yml new file mode 100644 index 000000000..9daeafb98 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Persistence/Mapping/_match_ns/Bar/barEntity.yml @@ -0,0 +1 @@ +test diff --git a/tests/Doctrine/Tests/Common/Persistence/Mapping/_match_ns/testEntity.yml b/tests/Doctrine/Tests/Common/Persistence/Mapping/_match_ns/testEntity.yml new file mode 100644 index 000000000..9daeafb98 --- /dev/null +++ b/tests/Doctrine/Tests/Common/Persistence/Mapping/_match_ns/testEntity.yml @@ -0,0 +1 @@ +test