Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions src/Mapping/Driver/SymfonyFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

use function array_keys;
use function array_merge;
use function assert;
use function is_dir;
use function is_file;
use function is_int;
use function realpath;
use function sprintf;
use function str_replace;
Expand Down Expand Up @@ -244,11 +242,14 @@ public function findMappingFile(string $className)
}

$pos = strrpos($className, '\\');
assert(is_int($pos));

throw MappingException::mappingFileNotFound(
$className,
substr($className, $pos + 1) . $this->fileExtension
// A class in the root namespace has no separator, and then its
// short name is the whole name: DateTime is DateTime, which is a
// perfectly good thing to name in the message rather than a
// reason to fail an assertion.
($pos === false ? $className : substr($className, $pos + 1)) . $this->fileExtension
);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Mapping/SymfonyFileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ public function testFindMappingFileNotFound(): void
$locator->findMappingFile('Foo\\stdClass2');
}

public function testFindMappingFileNotFoundForClassInRootNamespace(): void
{
$path = __DIR__ . '/_files';
$prefix = 'Foo';

$locator = new SymfonyFileLocator([$path => $prefix], '.yml');

// A class with no namespace separator used to fail the assertion that
// computed the short name for this message, so with zend.assertions
// on this threw AssertionError instead of MappingException. Reported
// from \DateTime reaching here through AbstractQuery::setParameter().
$this->expectException(MappingException::class);
$this->expectExceptionMessage("No mapping file found named 'DateTime.yml' for class 'DateTime'.");
$locator->findMappingFile('DateTime');
}

public function testFindMappingFileLeastSpecificNamespaceFirst(): void
{
// Low -> High
Expand Down