1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Persistence\Mapping;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\ClassLocator;
use Doctrine\Persistence\Mapping\Driver\ClassNames;
use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\Entity;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\EntityFixture;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\TestClass;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function sort;
class ColocatedMappingDriverTest extends TestCase
{
public function testAddGetPaths(): void
{
$driver = $this->createDirectoryPathDriver(__DIR__ . '/_files/colocated');
self::assertSame([
__DIR__ . '/_files/colocated',
], $driver->getPaths());
$driver->addPaths(['/test/path1', '/test/path2']);
self::assertSame([
__DIR__ . '/_files/colocated',
'/test/path1',
'/test/path2',
], $driver->getPaths());
}
public function testAddGetExcludePaths(): void
{
$driver = $this->createDirectoryPathDriver(__DIR__ . '/_files/colocated');
self::assertSame([], $driver->getExcludePaths());
$driver->addExcludePaths(['/test/path1', '/test/path2']);
self::assertSame([
'/test/path1',
'/test/path2',
], $driver->getExcludePaths());
}
public function testGetSetFileExtension(): void
{
$driver = $this->createDirectoryPathDriver(__DIR__ . '/_files/colocated');
self::assertSame('.php', $driver->getFileExtension());
$driver->setFileExtension('.php1');
self::assertSame('.php1', $driver->getFileExtension());
}
#[DataProvider('directoryPathProvider')]
public function testGetAllClassNamesForDirectory(string $dirPath): void
{
$driver = $this->createDirectoryPathDriver($dirPath);
$classes = $driver->getAllClassNames();
sort($classes);
self::assertSame([Entity::class, EntityFixture::class], $classes);
}
public function testGetAllClassNamesRemovesTransient(): void
{
$driver = $this->createClassNamesDriver([
// This class is transient, so it should not be returned by getAllClassNames()
// placed before the Entity class to validate that the driver returns an
// array without gaps in the indexes
TestClass::class,
Entity::class,
]);
$classes = $driver->getAllClassNames();
self::assertSame([Entity::class], $classes, 'The driver should only return the class names for the provided file path names, excluding transient class names.');
}
public function testGetAllClassNamesWorksBothForFilePathsAndRetroactivelyAddedDirectoryPaths(): void
{
$driver = $this->createClassNamesDriver([Entity::class]);
$driver->addPaths([__DIR__ . '/_files/colocated/']);
$classes = $driver->getAllClassNames();
sort($classes);
self::assertSame(
[Entity::class, EntityFixture::class],
$classes,
'The driver should return class names from both the provided file path names and the retroactively added directory paths (these should not be ignored).',
);
}
/** @return Generator<string, array{string}> */
public static function directoryPathProvider(): Generator
{
yield 'straigthforward path' => [__DIR__ . '/_files/colocated'];
yield 'winding path' => [__DIR__ . '/../Mapping/_files/colocated'];
}
private function createDirectoryPathDriver(string $dirPath): MyDriver
{
return new MyDriver([$dirPath]);
}
/** @param list<class-string> $classes */
private function createClassNamesDriver(array $classes): MyDriver
{
return new MyDriver(new ClassNames($classes));
}
}
final class MyDriver implements MappingDriver
{
use ColocatedMappingDriver;
/** @param string[]|ClassLocator $paths One or multiple paths where mapping classes can be found. */
public function __construct(array|ClassLocator $paths)
{
if ($paths instanceof ClassLocator) {
$this->classLocator = $paths;
} else {
$this->paths = $paths;
}
}
/**
* {@inheritDoc}
*/
public function loadMetadataForClass($className, ClassMetadata $metadata): void
{
}
public function isTransient(string $className): bool
{
return $className === TestClass::class;
}
}
|