Skip to content
Merged
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
12 changes: 4 additions & 8 deletions src/Persistence/Mapping/Driver/ColocatedMappingDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
*/
trait ColocatedMappingDriver
{
/** @var iterable<string> */
private iterable $sourceFilePathNames;

/**
* The paths where to look for mapping files.
*
Expand All @@ -54,7 +51,7 @@ trait ColocatedMappingDriver
protected string $fileExtension = '.php';

/**
* Cache for {@see getAllClassNames()}.
* Cache for getAllClassNames().
*
* @var array<int, string>|null
* @phpstan-var list<class-string>|null
Expand Down Expand Up @@ -82,7 +79,7 @@ public function getPaths(): array
}

/**
* Append exclude lookup paths to a metadata driver.
* Append exclude lookup paths to metadata driver.
*
* @param string[] $paths
*/
Expand Down Expand Up @@ -135,7 +132,7 @@ public function getAllClassNames(): array
return $this->classNames;
}

if ($this->paths === [] && ! isset($this->sourceFilePathNames)) {
if ($this->paths === []) {
throw MappingException::pathRequiredForDriver(static::class);
}

Expand All @@ -160,8 +157,7 @@ public function getAllClassNames(): array
$filesIterator->append($iterator);
}

/** @var iterable<string> $sourceFilePathNames */
$sourceFilePathNames = $this->sourceFilePathNames ?? $this->pathNameIterator($filesIterator);
$sourceFilePathNames = $this->pathNameIterator($filesIterator);
$includedFiles = [];

foreach ($sourceFilePathNames as $sourceFile) {
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function classNotFoundInNamespaces(
public static function pathRequiredForDriver(string $driverClassName): self
{
return new self(sprintf(
'Specifying source file paths to your entities is required when using %s to retrieve all class names.',
'Specifying the paths to your entities is required when using %s to retrieve all class names.',
$driverClassName,
));
}
Expand Down
44 changes: 9 additions & 35 deletions tests/Persistence/Mapping/ColocatedMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
use Generator;
use PHPUnit\Framework\TestCase;

use function assert;
use function is_array;
use function sort;

class ColocatedMappingDriverTest extends TestCase
{
public function testAddGetPaths(): void
{
$driver = $this->createPathDriver(__DIR__ . '/_files/colocated');
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
self::assertSame([
__DIR__ . '/_files/colocated',
], $driver->getPaths());
Expand All @@ -37,7 +35,7 @@ public function testAddGetPaths(): void

public function testAddGetExcludePaths(): void
{
$driver = $this->createPathDriver(__DIR__ . '/_files/colocated');
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
self::assertSame([], $driver->getExcludePaths());

$driver->addExcludePaths(['/test/path1', '/test/path2']);
Expand All @@ -50,7 +48,7 @@ public function testAddGetExcludePaths(): void

public function testGetSetFileExtension(): void
{
$driver = $this->createPathDriver(__DIR__ . '/_files/colocated');
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
self::assertSame('.php', $driver->getFileExtension());

$driver->setFileExtension('.php1');
Expand All @@ -59,61 +57,37 @@ public function testGetSetFileExtension(): void
}

/** @dataProvider pathProvider */
public function testGetAllClassNamesForPath(string $path): void
public function testGetAllClassNames(string $path): void
{
$driver = $this->createPathDriver($path);
$driver = $this->createDriver($path);

$classes = $driver->getAllClassNames();

sort($classes);
self::assertSame([Entity::class, EntityFixture::class], $classes);
}

public function testGetAllClassNamesForIterableFilePathNames(): void
{
$driver = $this->createFilePathNamesDriver([
__DIR__ . '/_files/colocated/Entity.php',
__DIR__ . '/_files/colocated/TestClass.php',
]);

$classes = $driver->getAllClassNames();

self::assertSame([Entity::class], $classes, 'The driver should only return the class names from the provided file path names, excluding transient class names.');
}

/** @return Generator<string, array{string}> */
public static function pathProvider(): Generator
{
yield 'straigthforward path' => [__DIR__ . '/_files/colocated'];
yield 'winding path' => [__DIR__ . '/../Mapping/_files/colocated'];
}

private function createPathDriver(string $path): MyDriver
private function createDriver(string $path): MyDriver
{
return new MyDriver([$path]);
}

/** @param list<string> $paths */
private function createFilePathNamesDriver(array $paths): MyDriver
{
return new MyDriver($paths, true);
}
}

final class MyDriver implements MappingDriver
{
use ColocatedMappingDriver;

/** @param iterable<string> $paths Source file path names */
public function __construct(iterable $paths, bool $sourceFilePathNames = false)
/** @param non-empty-list<string> $paths One or multiple paths where mapping classes can be found. */
public function __construct(array $paths)
{
if (! $sourceFilePathNames) {
assert(is_array($paths));

$this->addPaths($paths);
} else {
$this->sourceFilePathNames = $paths;
}
$this->addPaths($paths);
}

/**
Expand Down