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 149 150 151 152 153
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Persistence\Mapping\Driver;
use DirectoryIterator;
use Doctrine\Persistence\Mapping\Driver\FileClassLocator;
use Doctrine\Persistence\Mapping\MappingException;
use Doctrine\Tests\DoctrineTestCase;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\Entity;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\EntityFixture;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\Foo;
use Doctrine\Tests\Persistence\Mapping\_files\colocated\TestClass;
use EmptyIterator;
use Phar;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use function dirname;
use function ini_get;
use function sort;
use function unlink;
use const SORT_STRING;
final class FileClassLocatorTest extends DoctrineTestCase
{
public function testGetClassNames(): void
{
$locator = new FileClassLocator([
new SplFileInfo(__DIR__ . '/../_files/colocated/Entity.php'),
new SplFileInfo(__DIR__ . '/../_files/colocated/EntityFixture.php'),
]);
$classes = $locator->getClassNames();
sort($classes, SORT_STRING);
self::assertSame([
Entity::class,
EntityFixture::class,
], $classes);
}
public function testGetClassNamesWithEmptyIterator(): void
{
$locator = new FileClassLocator(new EmptyIterator());
self::assertSame([], $locator->getClassNames());
}
public function testCreateFromDirectory(): void
{
$locator = FileClassLocator::createFromDirectories([__DIR__ . '/../_files/colocated']);
$classes = $locator->getClassNames();
sort($classes, SORT_STRING);
self::assertSame([
Entity::class,
EntityFixture::class,
TestClass::class,
], $classes);
}
public function testCreateFromDirectoryWithExtension(): void
{
$locator = FileClassLocator::createFromDirectories([__DIR__ . '/../_files/colocated'], [], '.mphp');
$classes = $locator->getClassNames();
sort($classes, SORT_STRING);
self::assertSame([Foo::class], $classes);
}
public function testCreateFromDirectoryWithNonExistentDirectory(): void
{
$this->expectException(MappingException::class);
$this->expectExceptionMessage('File mapping drivers must have a valid directory path, however the given path [/non/existent/directory] seems to be incorrect!');
FileClassLocator::createFromDirectories(['/non/existent/directory']);
}
public function testCreateFromEmptyDirectory(): void
{
$locator = FileClassLocator::createFromDirectories([__DIR__ . '/../_files/Bar']);
self::assertSame([], $locator->getClassNames());
}
public function testCreateFromDirectoryIterator(): void
{
$locator = new FileClassLocator(new DirectoryIterator(__DIR__ . '/../_files/colocated'));
$classes = $locator->getClassNames();
sort($classes, SORT_STRING);
self::assertSame([
Entity::class,
EntityFixture::class,
Foo::class,
TestClass::class,
], $classes);
}
public function testCreateFromSymfonyFinder(): void
{
$finder = Finder::create()
->in(__DIR__ . '/../_files/colocated')
->name('*.php')
->notName('Test*');
$locator = new FileClassLocator($finder);
$classes = $locator->getClassNames();
sort($classes, SORT_STRING);
self::assertSame([
Entity::class,
EntityFixture::class,
], $classes);
}
public function testWithPharFiles(): void
{
if (ini_get('phar.readonly') === '1') {
self::markTestSkipped('creating archive disabled by the php.ini setting phar.readonly');
}
// Create a temporary Phar file with a PHP class
$pharFile = dirname(__DIR__) . '/_files/colocated.phar';
$phar = new Phar($pharFile);
$phar->startBuffering();
$phar->addFromString('Entity.php', '<?php namespace Doctrine\Phar; class Entity {}');
$phar->addFromString('EntityFixture.php', '<?php namespace Doctrine\Phar; class EntityFixture {}');
// Excludes directory
$phar->addFromString('Excluded/EntityExcluded.php', '<?php namespace Doctrine\Phar\Excluded; class EntityExcluded {}');
// Excluded file extension
$phar->addFromString('Foo.mphp', '<?php namespace Doctrine\Phar; class Foo {}');
$phar->stopBuffering();
$locator = FileClassLocator::createFromDirectories(['phar://' . $pharFile], ['phar://' . $pharFile . '/Excluded']);
$classes = $locator->getClassNames();
sort($classes, SORT_STRING);
unlink($pharFile);
// @phpstan-ignore staticMethod.impossibleType
self::assertSame([
'Doctrine\Phar\Entity',
'Doctrine\Phar\EntityFixture',
], $classes);
}
}
|